免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 16026 | 回复: 3

如何用c实现post请求来login [复制链接]

论坛徽章:
0
发表于 2007-11-07 09:32 |显示全部楼层
如何在C语言中实现HTTPS POST 的请求,内容类似于
        POST /accounts/ClientLogin HTTP/1.0
        Content-type: application/x-www-form-urlencoded
Email=johndoe@gmail.com&Passwd=north23AZ&service=cl&source=Gulp-CalGulp-1.05

具体怎么实现,或者用什么样的方式可以实现,请大家指点一二,谢谢

论坛徽章:
0
发表于 2007-11-07 09:41 |显示全部楼层

  1. /*
  2. *                                 http_post.cpp
  3. *
  4. * by Uday Chitragar - 2004/Dec/01
  5. *
  6. * This software is provided 'as-is', without any express or implied
  7. * warranty. In no event will the authors be held liable for any
  8. * damages arising from the use of this software.
  9. *
  10. * Permission is granted to anyone to use this software for any
  11. * purpose, including commercial applications, and to alter it and
  12. * redistribute it freely, subject to the following restrictions:
  13. *
  14. * 1. The origin of this software must not be misrepresented; you must
  15. * not claim that you wrote the original software. If you use this
  16. * software in a product, an acknowledgment in the product documentation
  17. * would be appreciated but is not required.
  18. *
  19. * 2. Altered source versions must be plainly marked as such, and
  20. * must not be misrepresented as being the original software.
  21. *
  22. * 3. This notice may not be removed or altered from any source
  23. * distribution.
  24. *
  25. * */

  26. /*
  27. * Notes:
  28. * This source demonstrates sending HTTP POST request to webserver from C++
  29. * This uses sockets hence can be compiled on Linux, UNIX, Win
  30. */

  31. #define LINUX_OS
  32. // #define WIN_OS
  33. #define _DEBUG_PRINT(X)   /* X */

  34. //For commn
  35. #include <iostream>
  36. #include <string>
  37. #include <stdlib.h>
  38. #include <assert.h>

  39. #ifdef LINUX_OS
  40. #include <netdb.h>
  41. #endif

  42. #ifdef WIN_OS
  43. #include <Winsock2.h>
  44. #endif


  45. #define SEND_RQ(MSG) \
  46.                 /*cout<<send_str;*/ \
  47.   send(sock,MSG,strlen(MSG),0);


  48. using namespace std;
  49. //<exe> hostname api parameters
  50. int request (char* hostname, char* api, char* parameters, string& message)
  51. {

  52.         #ifdef WIN_OS
  53.         {
  54.                 WSADATA        WsaData;
  55.                 WSAStartup (0x0101, &WsaData);
  56.         }
  57.         #endif

  58.     sockaddr_in       sin;
  59.     int sock = socket (AF_INET, SOCK_STREAM, 0);
  60.     if (sock == -1) {
  61.                 return -100;
  62.         }
  63.     sin.sin_family = AF_INET;
  64.     sin.sin_port = htons( (unsigned short)80);

  65.     struct hostent * host_addr = gethostbyname(hostname);
  66.     if(host_addr==NULL) {
  67.       _DEBUG_PRINT( cout<<"Unable to locate host"<<endl );
  68.       return -103;
  69.     }
  70.     sin.sin_addr.s_addr = *((int*)*host_addr->h_addr_list) ;
  71.     _DEBUG_PRINT( cout<<"Port :"<<sin.sin_port<<", Address : "<< sin.sin_addr.s_addr<<endl);

  72.     if( connect (sock,(const struct sockaddr *)&sin, sizeof(sockaddr_in) ) == -1 ) {
  73.      _DEBUG_PRINT( cout<<"connect failed"<<endl ) ;
  74.      return -101;
  75.     }

  76. string send_str;

  77. SEND_RQ("POST ");
  78. SEND_RQ(api);
  79. SEND_RQ(" HTTP/1.0\r\n");
  80. SEND_RQ("Accept: */*\r\n");
  81. SEND_RQ("User-Agent: Mozilla/4.0\r\n");

  82. char content_header[100];
  83. sprintf(content_header,"Content-Length: %d\r\n",strlen(parameters));
  84. SEND_RQ(content_header);
  85. SEND_RQ("Accept-Language: en-us\r\n");
  86. SEND_RQ("Accept-Encoding: gzip, deflate\r\n");
  87. SEND_RQ("Host: ");
  88. SEND_RQ("hostname");
  89. SEND_RQ("\r\n");
  90. SEND_RQ("Content-Type: application/x-www-form-urlencoded\r\n");

  91. //If you need to send a basic authorization
  92. //string Auth        = "username:password";
  93. //Figureout a way to encode test into base64 !
  94. //string AuthInfo    = base64_encode(reinterpret_cast<const unsigned char*>(Auth.c_str()),Auth.length());
  95. //string sPassReq    = "Authorization: Basic " + AuthInfo;
  96. //SEND_RQ(sPassReq.c_str());

  97. SEND_RQ("\r\n");
  98. SEND_RQ("\r\n");
  99. SEND_RQ(parameters);
  100. SEND_RQ("\r\n");

  101. _DEBUG_PRINT(cout<<"####HEADER####"<<endl);
  102. char c1[1];
  103. int l,line_length;
  104. bool loop = true;
  105. bool bHeader = false;

  106. while(loop) {
  107.    l = recv(sock, c1, 1, 0);
  108.    if(l<0) loop = false;
  109.    if(c1[0]=='\n') {
  110.        if(line_length == 0) loop = false;

  111.        line_length = 0;
  112.        if(message.find("200") != string::npos)
  113.                bHeader = true;

  114.    }
  115.    else if(c1[0]!='\r') line_length++;
  116.    _DEBUG_PRINT( cout<<c1[0]);
  117.    message += c1[0];
  118. }

  119. message="";
  120. if(bHeader) {

  121.      _DEBUG_PRINT( cout<<"####BODY####"<<endl) ;
  122.      char p[1024];
  123.      while((l = recv(sock,p,1023,0)) > 0)  {
  124.          _DEBUG_PRINT( cout.write(p,l)) ;
  125.              p[l] = '\0';
  126.              message += p;
  127.      }

  128.      _DEBUG_PRINT( cout << message.c_str());
  129. } else {
  130.          return -102;
  131. }


  132. #ifdef WIN_OS
  133.    WSACleanup( );
  134. #endif

  135. return 0;
  136. }


  137. int main(){
  138.   string message;
  139.   int request ("www.somesite.com", "/post_url.pl", "search=hello&date=todat", string& message);
  140.   // message contains response!

  141. }

复制代码


出处:http://www.codeguru.com/Cpp/I-N/internet/http/article.php/c8813/

论坛徽章:
0
发表于 2007-11-07 09:43 |显示全部楼层
原帖由 lanclot 于 2007-11-7 09:32 发表
如何在C语言中实现HTTPS POST 的请求,内容类似于
        POST /accounts/ClientLogin HTTP/1.0
        Content-type: application/x-www-form-urlencoded
Email=johndoe@gmail.com&Passwd=north23AZ&service=cl ...


用socket按照http protocol收发数据。
在你发post之前,你需要创建socket, connect Server

论坛徽章:
0
发表于 2007-11-07 09:58 |显示全部楼层
谢谢
我自己试试
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP