找到的一個http_client 源代碼

 

  
  
  
  
  1. #include "stdafx.h" 
  2. #include <stdio.h> 
  3. #include <stdlib.h> 
  4. #include "http_client.h" 
  5. #define BUFFER_SIZE 1024 
  6. int http_client_create(http_client *pclient,const char *host, int port) 
  7. struct hostent *he; 
  8. if(pclient == NULL) 
  9. return -1; 
  10. memset(pclient,0,sizeof(http_client)); 
  11. #ifdef WIN32 
  12. WSADATA wsaData; 
  13. WSAStartup(MAKEWORD(2, 2), &wsaData); 
  14. #endif  /*  WIN32  */ 
  15. if((he = gethostbyname(host))==NULL) 
  16. int err = WSAGetLastError(); 
  17. printf("%d",err); 
  18. return -2; 
  19. pclient->remote_port = port; 
  20. strcpy(pclient->remote_ip,inet_ntoa( *((struct in_addr *)he->h_addr))); 
  21. pclient->_addr.sin_family = AF_INET; 
  22. pclient->_addr.sin_port = htons(pclient->remote_port); 
  23. pclient->_addr.sin_addr = *((struct in_addr *)he->h_addr); 
  24. if((pclient->socket = socket(AF_INET,SOCK_STREAM,0))==-1) 
  25. return -3; 
  26. /*TODO:是否應該釋放內存呢?*/ 
  27. return 0; 
  28. int http_client_conn(http_client *pclient) 
  29. if(pclient->connected) 
  30. return 1; 
  31. if(connect(pclient->socket, (struct sockaddr *)&pclient->_addr,sizeof(struct sockaddr))==-1) 
  32. #ifdef WIN32 
  33. WSACleanup(); 
  34. #endif 
  35. return -1; 
  36. pclient->connected = 1; 
  37. return 0; 
  38.  
  39. int http_client_recv(http_client *pclient,char **lpbuff,int size) 
  40. int recvnum=0,tmpres=0; 
  41. char buff[BUFFER_SIZE]; 
  42. *lpbuff = NULL; 
  43. while(recvnum < size || size==0) 
  44. tmpres = recv(pclient->socket, buff,BUFFER_SIZE,0); 
  45. if(tmpres <= 0) 
  46. break
  47. recvnum += tmpres; 
  48. if(*lpbuff == NULL) 
  49. *lpbuff = (char*)malloc(recvnum); 
  50. if(*lpbuff == NULL) 
  51. return -2; 
  52. else 
  53. *lpbuff = (char*)realloc(*lpbuff,recvnum); 
  54. if(*lpbuff == NULL) 
  55. return -2; 
  56. memcpy(*lpbuff+recvnum-tmpres,buff,tmpres); 
  57. return recvnum; 
  58. int http_client_send(http_client *pclient,char *buff,int size) 
  59. int sent=0,tmpres=0; 
  60. while(sent < size) 
  61. tmpres = send(pclient->socket,buff+sent,size-sent,0); 
  62. if(tmpres == -1) 
  63. return -1; 
  64. sent += tmpres; 
  65. return sent; 
  66. int http_client_close(http_client *pclient) 
  67. closesocket(pclient->socket); 
  68. pclient->connected = 0; 
  69. return 0; 
  70. int http_post(http_client *pclient,char *page,char *request,char **response) 
  71. char post[300],host[100],content_len[100]; 
  72. char *lpbuf,*ptmp; 
  73. int len=0; 
  74. lpbuf = NULL; 
  75. const char *header2="User-Agent: linux_http_client Http 1.0\r\nCache-Control: no-cache\r\nContent-Type: application/x-www-form-urlencoded\r\nAccept: */*\r\n"
  76. sprintf(post,"GET %s HTTP/1.0\r\n",page); 
  77. sprintf(host,"HOST: %s:%d\r\n",pclient->remote_ip,pclient->remote_port); 
  78. sprintf(content_len,"Content-Length: %d\r\n\r\n",strlen(request)); 
  79. len = strlen(post)+strlen(host)+strlen(header2)+strlen(content_len)+strlen(request); 
  80. lpbuf = (char*)malloc(len); 
  81. if(lpbuf==NULL) 
  82. #ifdef WIN32 
  83. WSACleanup(); 
  84. #endif 
  85. return -1; 
  86. strcpy(lpbuf,post); 
  87. strcat(lpbuf,host); 
  88. strcat(lpbuf,header2); 
  89. strcat(lpbuf,content_len); 
  90. strcat(lpbuf,request); 
  91. if(!pclient->connected) 
  92. http_client_conn(pclient); 
  93. if(http_client_send(pclient,lpbuf,len)<0) 
  94. return -1; 
  95. printf("發送請求:\n%s\n",lpbuf); 
  96. /*釋放內存*/ 
  97. /*it's time to recv from server*/ 
  98. memset(lpbuf,0,len); 
  99. if(http_client_recv(pclient,&lpbuf,0) <= 0) 
  100. if(lpbuf) free(lpbuf); 
  101. return -2; 
  102. memset(post,0,sizeof(post)); 
  103. strncpy(post,lpbuf+9,3); 
  104. printf("返回碼: %d\n",atoi(post)); 
  105. printf("接收響應:\n%s\n",lpbuf); 
  106. /*響應代碼,|HTTP/1.0 200 OK| 
  107. *從第10個字符開始,第3位 
  108. * */ 
  109. ptmp = (char*)strstr(lpbuf,"\r\n\r\n"); 
  110. if(ptmp == NULL) 
  111. free(lpbuf); 
  112. return -3; 
  113. ptmp += 4;/*跳過\r\n*/ 
  114. len = strlen(ptmp)+1; 
  115. *response=(char*)malloc(len); 
  116. if(*response == NULL) 
  117. if(lpbuf) free(lpbuf); 
  118. return -1; 
  119. memset(*response,0,len); 
  120. memcpy(*response,ptmp,len-1); 
  121. /*從頭域找到內容長度,若是沒有找到則不處理*/ 
  122. ptmp = (char*)strstr(lpbuf,"Content-Length:"); 
  123. if(ptmp != NULL) 
  124. char *ptmp2; 
  125. ptmp += 15; 
  126. ptmp2 = (char*)strstr(ptmp,"\r\n"); 
  127. if(ptmp2 != NULL) 
  128. memset(post,0,sizeof(post)); 
  129. strncpy(post,ptmp,ptmp2-ptmp); 
  130. if(atoi(post)<len) 
  131. (*response)[atoi(post)] = '\0'
  132. if(lpbuf) free(lpbuf); 
  133. return 0; 
  134. int http_get(http_client *pclient,char *page,char **response) 
  135. char post[300],host[100],header2[1024]; 
  136. char *lpbuf,*ptmp; 
  137. int len=0; 
  138. lpbuf = NULL; 
  139. memset(header2,0,1024); 
  140. sprintf(header2,"Connection: keep-alive\r\nAccept: application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,p_w_picpath/png,*/*;q=0.5\r\nUser-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US) AppleWebKit/534.3 (KHTML, like Gecko) Chrome/6.0.472.33 Safari/534.3 SE 2.X MetaSr 1.0\r\nAccept-Encoding: gzip,deflate\r\nAccept-Language: zh-CN,zh;q=0.8\r\nAccept-Charset: GBK,utf-8;q=0.7,*;q=0.3\r\nCookie: prov=86; city=other; weather_city=bj; userid=1306844325031_5716; ilocid=1000; ilocidflag=1; vjuids=60f761c9e.13045fde8be.0.427b2991; iCast2_15963_2105hO=0_2_20160; _iCast2_15963_2105hO=2; Q37_sid=U4Bb45; ExpendBuoywwwpacx2011619=1; vjlast=1306844326.1308468360.11; Q37_visitedfid=442D284\r\nIf-Modified-Since: Sun, 19 Jun 2011 11:27:13 GMT\r\n\r\n"); 
  141. sprintf(post,"GET %s HTTP/1.1\r\n",page); 
  142. sprintf(host,"Host: %s:%d\r\n",pclient->remote_ip,pclient->remote_port); 
  143. len = strlen(post)+strlen(host)+strlen(header2); 
  144. lpbuf = (char*)malloc(len); 
  145. if(lpbuf==NULL) 
  146. #ifdef WIN32 
  147. WSACleanup(); 
  148. #endif 
  149. return -1; 
  150. strcpy(lpbuf,post); 
  151. strcat(lpbuf,host); 
  152. strcat(lpbuf,header2); 
  153. if(!pclient->connected) 
  154. http_client_conn(pclient); 
  155. if(http_client_send(pclient,lpbuf,len)<0) 
  156. return -1; 
  157. printf("發送請求:\n%s\n",lpbuf); 
  158. /*釋放內存*/ 
  159. /*it's time to recv from server*/ 
  160. memset(lpbuf,0,len); 
  161. if(http_client_recv(pclient,&lpbuf,0) <= 0) 
  162. if(lpbuf) free(lpbuf); 
  163. return -2; 
  164. memset(post,0,sizeof(post)); 
  165. strncpy(post,lpbuf+9,3); 
  166. printf("返回碼: %d\n",atoi(post)); 
  167. printf("接收響應:\n%s\n",lpbuf); 
  168. /*響應代碼,|HTTP/1.0 200 OK| 
  169. *從第10個字符開始,第3位 
  170. * */ 
  171. ptmp = (char*)strstr(lpbuf,"\r\n\r\n"); 
  172. if(ptmp == NULL) 
  173. free(lpbuf); 
  174. return -3; 
  175. ptmp += 4;/*跳過\r\n*/ 
  176. len = strlen(ptmp)+1; 
  177. *response=(char*)malloc(len); 
  178. if(*response == NULL) 
  179. if(lpbuf) free(lpbuf); 
  180. return -1; 
  181. memset(*response,0,len); 
  182. memcpy(*response,ptmp,len-1); 
  183. /*從頭域找到內容長度,若是沒有找到則不處理*/ 
  184. ptmp = (char*)strstr(lpbuf,"Content-Length:"); 
  185. if(ptmp != NULL) 
  186. char *ptmp2; 
  187. ptmp += 15; 
  188. ptmp2 = (char*)strstr(ptmp,"\r\n"); 
  189. if(ptmp2 != NULL) 
  190. memset(post,0,sizeof(post)); 
  191. strncpy(post,ptmp,ptmp2-ptmp); 
  192. if(atoi(post)<len) 
  193. (*response)[atoi(post)] = '\0'
  194. if(lpbuf) free(lpbuf); 
  195. return 0; 
  196. int main() 
  197. http_client client; 
  198. char *response = NULL; 
  199. char url[256]; 
  200. printf("Input the URL:"); 
  201. scanf("%s",url); 
  202. printf("開始組包\n"); 
  203. http_client_create(&client,url,80); 
  204. if(http_post(&client,"/"," ",&response)) 
  205. printf("失敗!\n"); 
  206. exit(2); 
  207. /*    if(http_get(&client,"/",&response)) 
  208. { 
  209. printf("失敗!\n"); 
  210. exit(2); 
  211. } 
  212. */ 
  213. printf("響應:\n%d:%s\n",strlen(response),response); 
  214. FILE *fp; 
  215. fp = fopen("index.html","w"); 
  216. fwrite(response,strlen(response),1,fp); 
  217. fclose(fp); 
  218. free(response); 
  219. #ifdef WIN32 
  220. WSACleanup(); 
  221. #endif 
  222. return 0; 




 html

   
   
   
   
  1. http_client.h 
  2.  
  3. #ifndef _LINUX_HTTP_CLIENT_ 
  4. #define _LINUX_HTTP_CLIENT_ 
  5.  
  6. #ifdef WIN32 
  7. #include <Winsock2.h> 
  8. #else 
  9. #include <sys/types.h> 
  10. #include <sys/socket.h> 
  11. #define closesocket close 
  12. #endif 
  13. typedef struct linux_http_client{ 
  14. int socket; 
  15. int remote_port; 
  16. char remote_ip[16]; 
  17. struct sockaddr_in _addr; 
  18. int connected; 
  19. } http_client; 
  20. int http_client_create(http_client *,const char *host, int port); 
  21. int http_client_conn(http_client *); 
  22. int http_client_recv(http_client *,char **lpbuff,int size); 
  23. int http_client_send(http_client *,char *buff,int size); 
  24. int http_client_close(http_client *); 
  25. #endif
相關文章
相關標籤/搜索