linux下c语言socket用post发送表单 【模拟网页提交表单】-凯发app官方网站

凯发app官方网站-凯发k8官网下载客户端中心 | | 凯发app官方网站-凯发k8官网下载客户端中心
  • 博客访问: 383701
  • 博文数量: 85
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 1707
  • 用 户 组: 普通用户
  • 注册时间: 2013-08-27 11:18
个人简介

学无止境……

文章分类

(85)

  • (29)
  • (16)
  • (8)
  • (16)
  • (0)
  • (12)
  • (0)
  • (3)
  • (1)
相关博文
  • ·
  • ·
  • ·
  • ·
  • ·
  • ·
  • ·
  • ·
  • ·
  • ·

分类: web开发

2014-08-04 17:27:54

/
  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,目录显示等)

阅读(4229) | 评论(0) | 转发(0) |
0

上一篇:

下一篇:

给主人留下些什么吧!~~
")); function link(t){ var href= $(t).attr('href'); href ="?url=" encodeuricomponent(location.href); $(t).attr('href',href); //setcookie("returnouturl", location.href, 60, "/"); }
网站地图