Linux下用c语言实现发送http请求 方式可以Get或者Post
1. [代码]Linux下用c语言实现发送http请求 方式可以Get或者Post 001#include002#include003#include004#include
·
1. [代码]Linux下用c语言实现发送http请求 方式可以Get或者Post
001 |
#include <stdio.h> |
002 |
#include <sys/socket.h> |
003 |
#include <sys/types.h> |
004 |
#include <time.h> |
005 |
#include <errno.h> |
006 |
#include <signal.h> |
007 |
#include <stdlib.h> |
008 |
#include <string.h> |
009 |
#include <unistd.h> |
010 |
#include <sys/wait.h> |
011 |
#include <sys/time.h> |
012 |
#include <netinet/in.h> |
013 |
#include <arpa/inet.h> |
014 |
015 |
#define IPSTR "61.147.124.120" |
016 |
#define PORT 80 |
017 |
#define BUFSIZE 1024 |
018 |
019 |
int main(int argc, char **argv) |
020 |
{ |
021 |
int sockfd, ret, i, h; |
022 |
struct sockaddr_in servaddr; |
023 |
char str1[4096], str2[4096], buf[BUFSIZE], *str; |
024 |
socklen_t len; |
025 |
fd_set t_set1; |
026 |
struct timeval tv; |
027 |
028 |
if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0 ) { |
029 |
printf("创建网络连接失败,本线程即将终止---socket error!\n"); |
030 |
exit(0); |
031 |
}; |
032 |
033 |
bzero(&servaddr, sizeof(servaddr)); |
034 |
servaddr.sin_family = AF_INET; |
035 |
servaddr.sin_port = htons(PORT); |
036 |
if (inet_pton(AF_INET, IPSTR, &servaddr.sin_addr) <= 0 ){ |
037 |
printf("创建网络连接失败,本线程即将终止--inet_pton error!\n"); |
038 |
exit(0); |
039 |
}; |
040 |
041 |
if (connect(sockfd, (struct sockaddr *)&servaddr, sizeof(servaddr)) < 0){ |
042 |
printf("连接到服务器失败,connect error!\n"); |
043 |
exit(0); |
044 |
} |
045 |
printf("与远端建立了连接\n"); |
046 |
047 |
//发送数据 |
048 |
memset(str2, 0, 4096); |
049 |
strcat(str2, "qqCode=474497857"); |
050 |
str=(char *)malloc(128); |
051 |
len = strlen(str2); |
052 |
sprintf(str, "%d", len); |
053 |
054 |
memset(str1, 0, 4096); |
055 |
strcat(str1, "POST /webservices/qqOnlineWebService.asmx/qqCheckOnline HTTP/1.1\n"); |
056 |
strcat(str1, "Host: www.webxml.com.cn\n"); |
057 |
strcat(str1, "Content-Type: application/x-www-form-urlencoded\n"); |
058 |
strcat(str1, "Content-Length: "); |
059 |
strcat(str1, str); |
060 |
strcat(str1, "\n\n"); |
061 |
062 |
strcat(str1, str2); |
063 |
strcat(str1, "\r\n\r\n"); |
064 |
printf("%s\n",str1); |
065 |
066 |
ret = write(sockfd,str1,strlen(str1)); |
067 |
if (ret < 0) { |
068 |
printf("发送失败!错误代码是%d,错误信息是'%s'\n",errno,strerror(errno)); |
069 |
exit(0); |
070 |
}else{ |
071 |
printf("消息发送成功,共发送了%d个字节!\n\n", ret); |
072 |
} |
073 |
074 |
FD_ZERO(&t_set1); |
075 |
FD_SET(sockfd, &t_set1); |
076 |
077 |
while(1){ |
078 |
sleep(2); |
079 |
tv.tv_sec= 0; |
080 |
tv.tv_usec= 0; |
081 |
h= 0; |
082 |
printf("--------------->1"); |
083 |
h= select(sockfd +1, &t_set1, NULL, NULL, &tv); |
084 |
printf("--------------->2"); |
085 |
086 |
//if (h == 0) continue; |
087 |
if (h < 0) { |
088 |
close(sockfd); |
089 |
printf("在读取数据报文时SELECT检测到异常,该异常导致线程终止!\n"); |
090 |
return -1; |
091 |
}; |
092 |
093 |
if (h > 0){ |
094 |
memset(buf, 0, 4096); |
095 |
i= read(sockfd, buf, 4095); |
096 |
if (i==0){ |
097 |
close(sockfd); |
098 |
printf("读取数据报文时发现远端关闭,该线程终止!\n"); |
099 |
return -1; |
100 |
} |
101 |
102 |
printf("%s\n", buf); |
103 |
} |
104 |
} |
105 |
close(sockfd); |
106 |
107 |
108 |
return 0; |
109 |
} |
更多推荐



所有评论(0)