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.1n"); |
056 | strcat(str1, "Host: www.webxml.com.cnn"); |
057 | strcat(str1, "Content-Type: application/x-www-form-urlencodedn"); |
058 | strcat(str1, "Content-Length: "); |
059 | strcat(str1, str); |
060 | strcat(str1, "nn"); |
061 |
062 | strcat(str1, str2); |
063 | strcat(str1, "rnrn"); |
064 | printf("%sn",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个字节!nn", 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("%sn", buf); |
103 | } |
104 | } |
105 | close(sockfd); |
106 |
107 |
108 | return 0; |
109 | } |
最后
以上就是精明星星最近收集整理的关于Linux下用c语言实现发送http请求 方式可以Get或者Post的全部内容,更多相关Linux下用c语言实现发送http请求内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复