似乎写代码发HTTP请求需要自己把完整的协议帧写出来?而不是单纯填个URL就行了,那既然完整协议帧都写出来了,那我直接TCP发不就可以了?还是说不能这样。
基本你百度搜 c++发送http请求 第一页出来的博客文章都是这么写的 把完整的HTTP报文写出来,放到一个字符串里面,再发。基本都是这样
我看了下,应该他们实际还是用的TCP通信的代码,把HTTP完整协议帧写出来然后再用TCP发送。
从这张图来看,这么弄也应该是可以的。
拍自《图解HTTP》 P10
转载自:C++:C语言实现HTTP的GET和POST请求 - 方方和圆圆 - 博客园
阅读目录
回到顶部
所谓的HTTP协议是基于IP/TCP协议的, 所以要获取远端的html数据只要创建socket对象就足够了;
HTTP是基于IP/TCP加上了网络请求的固定格式, 比如:
运行下面代码
method URL Content-host Content-Type Content-Length
回到顶部
把组合成的数据按照固定的格式发送给服务器, 服务器解析相应的格式, 然后返回服务器数据:
运行下面代码
#include#include #include #include #include #include #include #include #include #include #include #include #include #define IPSTR "42.121.252.58" //服务器IP地址; #define PORT 80 #define BUFSIZE 1024 int main(int argc, char **argv) { int sockfd, ret, i, h; struct sockaddr_in servaddr; char str1[4096], str2[4096], buf[BUFSIZE], *str; socklen_t len; fd_set t_set1; struct timeval tv; //创建套接字 if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0 ) { printf("创建网络连接失败,本线程即将终止---socket error!\n"); exit(0); }; bzero(&servaddr, sizeof(servaddr)); servaddr.sin_family = AF_INET; servaddr.sin_port = htons(PORT); if (inet_pton(AF_INET, IPSTR, &servaddr.sin_addr) <= 0 ){ printf("创建网络连接失败,本线程即将终止--inet_pton error!\n"); exit(0); }; if (connect(sockfd, (struct sockaddr *)&servaddr, sizeof(servaddr)) < 0){ printf("连接到服务器失败,connect error!\n"); exit(0); } printf("与远端建立了连接\n"); memset(str2, 0, 4096); strcat(str2, "theDataToPost"); str=(char *)malloc(128); len = strlen(str2); sprintf(str, "%d", len); memset(str1, 0, 4096); strcat(str1, "GET /ymnets/p/6255674.html HTTP/1.1\n"); strcat(str1, "Host: www.cnblogs.com\n"); strcat(str1, "Content-Type: text/html\n"); strcat(str1, "Content-Length: "); strcat(str1, str); strcat(str1, "\n\n"); strcat(str1, str2); strcat(str1, "\r\n\r\n"); printf("%s\n",str1); ret = write(sockfd,str1,strlen(str1)); if (ret < 0) { printf("发送失败!错误代码是%d,错误信息是'%s'\n",errno, strerror(errno)); exit(0); }else{ printf("消息发送成功,共发送了%d个字节!\n\n", ret); } FD_ZERO(&t_set1); FD_SET(sockfd, &t_set1); while(1){ sleep(2); tv.tv_sec= 0; tv.tv_usec= 0; h= 0; printf("--------------->1"); h= select(sockfd +1, &t_set1, NULL, NULL, &tv); printf("--------------->2"); //if (h == 0) continue; if (h < 0) { close(sockfd); printf("在读取数据报文时SELECT检测到异常,该异常导致线程终止!\n"); return -1; }; if (h > 0){ memset(buf, 0, 4096); i= read(sockfd, buf, 4095); if (i==0){ close(sockfd); printf("读取数据报文时发现远端关闭,该线程终止!\n"); return -1; } printf("%s\n", buf); } } close(sockfd); return 0; }
回到顶部
C实现POST请求参考代码, 只要把请求类型从GET改为POST, 然后把请求的数据代入到请求头中即可:
运行下面代码
#include#include #include #include #include #include #include #include #include #include #include #include #include #define IPSTR "61.147.124.120" #define PORT 80 #define BUFSIZE 1024 int main(int argc, char **argv) { int sockfd, ret, i, h; struct sockaddr_in servaddr; char str1[4096], str2[4096], buf[BUFSIZE], *str; socklen_t len; fd_set t_set1; struct timeval tv; if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0 ) { printf("创建网络连接失败,本线程即将终止---socket error!\n"); exit(0); }; bzero(&servaddr, sizeof(servaddr)); servaddr.sin_family = AF_INET; servaddr.sin_port = htons(PORT); if (inet_pton(AF_INET, IPSTR, &servaddr.sin_addr) <= 0 ){ printf("创建网络连接失败,本线程即将终止--inet_pton error!\n"); exit(0); }; if (connect(sockfd, (struct sockaddr *)&servaddr, sizeof(servaddr)) < 0){ printf("连接到服务器失败,connect error!\n"); exit(0); } printf("与远端建立了连接\n"); memset(str2, 0, 4096); strcat(str2, "qqCode=287101329"); str=(char *)malloc(128); len = strlen(str2); sprintf(str, "%d", len); memset(str1, 0, 4096); strcat(str1, "POST /webservices/qqOnlineWebService.asmx/qqCheckOnline HTTP/1.1\n"); strcat(str1, "Host: www.webxml.com.cn\n"); strcat(str1, "Content-Type: application/x-www-form-urlencoded\n"); strcat(str1, "Content-Length: "); strcat(str1, str); strcat(str1, "\n\n"); //str2的值为post的数据 strcat(str1, str2); strcat(str1, "\r\n\r\n"); printf("%s\n",str1); ret = write(sockfd,str1,strlen(str1)); if (ret < 0) { printf("发送失败!错误代码是%d,错误信息是'%s'\n",errno, strerror(errno)); exit(0); }else{ printf("消息发送成功,共发送了%d个字节!\n\n", ret); } FD_ZERO(&t_set1); FD_SET(sockfd, &t_set1); while(1){ sleep(2); tv.tv_sec= 0; tv.tv_usec= 0; h= 0; printf("--------------->1"); h= select(sockfd +1, &t_set1, NULL, NULL, &tv); printf("--------------->2"); //if (h == 0) continue; if (h < 0) { close(sockfd); printf("在读取数据报文时SELECT检测到异常,该异常导致线程终止!\n"); return -1; }; if (h > 0){ memset(buf, 0, 4096); i= read(sockfd, buf, 4095); if (i==0){ close(sockfd); printf("读取数据报文时发现远端关闭,该线程终止!\n"); return -1; } printf("%s\n", buf); } } close(sockfd); return 0; }
POST请求的方式可以再改改:
运行下面代码
#include#include #include #include #include #include #include #include #include #include #include #include #include #define IPSTR "61.147.124.120" #define PORT 80 #define BUFSIZE 1024 int main(int argc, char **argv) { int sockfd, ret, i, h; struct sockaddr_in servaddr; char str1[4096], str2[4096], buf[BUFSIZE], *str; socklen_t len; fd_set t_set1; struct timeval tv; if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0 ) { printf("创建网络连接失败,本线程即将终止---socket error!\n"); exit(0); }; bzero(&servaddr, sizeof(servaddr)); servaddr.sin_family = AF_INET; servaddr.sin_port = htons(PORT); if (inet_pton(AF_INET, IPSTR, &servaddr.sin_addr) <= 0 ){ printf("创建网络连接失败,本线程即将终止--inet_pton error!\n"); exit(0); }; if (connect(sockfd, (struct sockaddr *)&servaddr, sizeof(servaddr)) < 0){ printf("连接到服务器失败,connect error!\n"); exit(0); } printf("与远端建立了连接\n"); memset(str2, 0, 4096); strcat(str2, "qqCode=287101329"); str=(char *)malloc(128); len = strlen(str2); sprintf(str, "%d", len); memset(str1, 0, 4096); strcat(str1, "POST /webservices/qqOnlineWebService.asmx/qqCheckOnline HTTP/1.1\n"); strcat(str1, "Host: www.webxml.com.cn\n"); strcat(str1, "Content-Type: application/x-www-form-urlencoded\n"); strcat(str1, "Content-Length: "); strcat(str1, str); strcat(str1, "\n\n"); //str2的值为post的数据 strcat(str1, str2); strcat(str1, "\r\n\r\n"); printf("%s\n",str1); ret = write(sockfd,str1,strlen(str1)); if (ret < 0) { printf("发送失败!错误代码是%d,错误信息是'%s'\n",errno, strerror(errno)); exit(0); }else{ printf("消息发送成功,共发送了%d个字节!\n\n", ret); } FD_ZERO(&t_set1); FD_SET(sockfd, &t_set1); tv.tv_sec= 0; tv.tv_usec= 0; h= 0; while(1){ sleep(2); printf("--------------->1"); h = select(sockfd +1, &t_set1, NULL, NULL, &tv); printf("--------------->2"); //if (h == 0) continue; if (h == -1) { close(sockfd); printf("在读取数据报文时SELECT检测到异常,该异常导致线程终止!\n"); return -1; }; if ( FD_ISSET(sockfd, &t_set1) ){ memset(buf, 0, 4096); i= read(sockfd, buf, 4095); if (i==0){ close(sockfd); printf("读取数据报文时发现远端关闭,该线程终止!\n"); return -1; } printf("%s\n", buf); } } close(sockfd); return 0; }
请求一个HTTP服务器的数据, 接收一个参数为请求的服务器IP:
运行下面代码
#include#include #include #include #include #include #include #include int main(int argc, char *argv[]){ if(argc!=2){ printf("error usage %s ip\n",argv[0]); return 1; } int sockfd; int len; struct sockaddr_in address; int result; char httpstring[100]; sprintf(httpstring,"GET / HTTP/1.1\r\n" "Host: %s\r\n" "Connection: Close\r\n\r\n",argv[1]); char ch; sockfd = socket(AF_INET, SOCK_STREAM, 0); address.sin_family = AF_INET; address.sin_addr.s_addr = inet_addr(argv[1]); address.sin_port = htons(80); len = sizeof(address); result = connect(sockfd,(struct sockaddr *)&address,len); if(result == -1){ perror("oops: client"); return 1; } write(sockfd,httpstring,strlen(httpstring)); while(read(sockfd,&ch,1)){ printf("%c", ch); } close(sockfd); printf("\n"); return 0; }
使用gcc编译以后执行: sudo ./req 42.121.252.58
Linux下用c语言实现发送http请求 方式可以Get或者Post:http://www.oschina.net/code/snippet_176076_5908/
本文作者:方方和圆圆
本文链接:https://www.cnblogs.com/diligenceday/p/6255788.html
版权声明:本作品采用知识共享署名-非商业性使用-禁止演绎 2.5 中国大陆许可协议进行许可。