Chinaunix首页 | 论坛 | 博客
  • 博客访问: 393902
  • 博文数量: 85
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 1707
  • 用 户 组: 普通用户
  • 注册时间: 2013-08-27 11:18
个人简介

学无止境……

文章分类

全部博文(85)

分类: Web开发

2014-08-04 17:27:54


http://blog.163.com/liguiqin_500/blog/static/763064200912693950579
/

  1. #include <stdlib.h>
  2. #include <errno.h>
  3. #include <string.h>
  4. #include <unistd.h>
  5. #include <sys/socket.h>
  6. #include <netinet/in.h>
  7. #include <arpa/inet.h>
  8. #include <netdb.h>
  9. #include <stdio.h>

  10. void posturl()
  11. {
  12. int sockfd;
  13. struct sockaddr_in addr;
  14. struct hostent *pURL;

  15. char *pHost = 0, *pPOST = 0;
  16. static char text[BUFSIZ];
  17. char header[BUFSIZ] = "";
  18. int i;

  19. /*
  20. * 设定socket参数,并未真正初始化
  21. */
  22. sockfd = socket(AF_INET, SOCK_STREAM, 0);

  23. addr.sin_family = AF_INET;
  24. inet_aton("192.168.10.200",&addr.sin_addr);
  25. addr.sin_port = htons(80);

  26. /*
  27. * 组织发送到web服务器的信息
  28. * 为何要发送下面的信息请参考HTTP协议的约定
  29. */
  30. strcat(header, "POST ");
  31. strcat(header, "/pass");
  32. strcat(header, " HTTP/1.1\r\n");
  33. strcat(header, "*/*\r\n");
  34. strcat(header, "Referer: \r\n");
  35. strcat(header, "Accept-Language: zh-cn\r\n");
  36. strcat(header,"Content-Type: application/x-www-form-urlencoded\r\n");
  37. strcat(header, "HOST: ");
  38. strcat(header, "192.168.10.200\r\n");
  39. strcat(header, "Content-Length: 26\r\n");
  40. strcat(header, "Connection: Keep-Alive\r\n");
  41. strcat(header, "Cache-Control: no-cache\r\n\r\n");
  42. strcat(header, "password=1234&submit=Apply");
  43. strcat(header, "\r\n");

  44. /*
  45. * 连接到服务器,发送请求header,并接受反馈(即网页源代码)
  46. */
  47. connect(sockfd,(struct sockaddr *)&addr,sizeof(struct sockaddr_in));

  48. i = send(sockfd, header, strlen(header), 0);

  49. printf("%d\n",i);

  50. while ( recv(sockfd, text, BUFSIZ, 0) > 0)
  51. {
  52. printf("%s", text);
  53. for(i = 0; i < BUFSIZ; i++)
  54. text[i]='\0';
  55. //strnset(text, '\0', BUFSIZ);
  56. }

  57. close(sockfd);


  58. }

  59. int main()
  60. {
  61. posturl();
  62. return 0;
  63. }



  1. //*****************************************//
  2. //*********利用C实现自动发包***************//
  3. //*********作者:马 犇******************//
  4. //*********时间:2011-6-25*****************//
  5. //*****************************************//

  6. #include "stdafx.h"
  7. #include "stdio.h"
  8. #include "stdlib.h"
  9. #include "winsock2.h"
  10. #pragma comment(lib,"ws2_32.lib")
  11. int main(int argc, char* argv[])
  12. {

  13.   SOCKET hsocket;
  14.   SOCKADDR_IN saServer;
  15.   WSADATA wsadata;
  16.   LPHOSTENT lphostent;
  17.   int nRet;
  18.   char* host_name="www.********.com";
  19.   char* reqHead="POST ********* HTTP/1.1\r\n" //此为要发送的数据包
  20.         "Accept: */*\r\n"
  21.         "Referer: *********\r\n"
  22.         "Accept-Language: zh-CN\r\n"
  23.         "User-Agent: Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Trident/5.0; BOIE9;ZHCN; twchrome)\r\n"
  24.         "Content-Type: application/x-www-form-urlencoded\r\n"
  25.         "Accept-Encoding: gzip, deflate\r\n"
  26.         "Host: www.*******.com\r\n"
  27.         "Content-Length: 375\r\n"
  28.         "Connection: Keep-Alive\r\n"
  29.         "Cache-Control: no-cache\r\n"
  30.         "Cookie: ************** \r\n\r\n"
  31.         "user=test&pass=123";
  32.         
  33.  // 初始化套接字
  34.     if(WSAStartup(MAKEWORD(2,2),&wsadata))
  35.         printf("初始化SOCKET出错!");
  36.     lphostent=gethostbyname(host_name);
  37.     if(lphostent==NULL)
  38.         printf("lphostent为空!");
  39.     hsocket = socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);
  40.     saServer.sin_family = AF_INET;
  41.     saServer.sin_port = htons(80);
  42.     saServer.sin_addr = *((LPIN_ADDR)*lphostent->h_addr_list);
  43.     // 利用SOCKET连接
  44.     nRet = connect(hsocket,(LPSOCKADDR)&saServer,sizeof(SOCKADDR_IN));
  45.     if(nRet == SOCKET_ERROR)
  46.     {
  47.         printf("建立连接时出错!");
  48.         closesocket(hsocket);
  49.         return 0;
  50.     }
  51.     // 利用SOCKET发送

  52.     nRet = send(hsocket,req,strlen(req),0);
  53.     if(nRet==SOCKET_ERROR)
  54.     {
  55.         printf("发送数据包时出错!");
  56.         closesocket(hsocket);
  57.     }
  58.     char Dest[3000];
  59.     nRet=1;
  60.     while(nRet>0)
  61.     {
  62.         // 接收返回数据包
  63.         nRet=recv(hsocket,(LPSTR)Dest,sizeof(Dest),0);
  64.         if(nRet>0)
  65.             Dest[nRet]=0;
  66.         else
  67.             Dest[0]=0;
  68.         // 显示返回数据包的大小、内容
  69.         printf("\nReceived bytes:%d\n",nRet);
  70.         printf("Result:\n%s",Dest);
  71.     }
  72.  }
  73.     return 0;
  74. }

C语言开发Linux下web服务器(支持GET/POST,SSL,目录显示等)
http://blog.csdn.net/yueguanghaidao/article/details/8450938/
阅读(4370) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~