C++发送HTTP请求---亲测可行(转)

摘要:
hConnect)转到Ret0;hRequest=(HINSTANCE)HttpOpenRequest(hConnect,lpMethod,lpUrl,“HTTP/1.1”,空,空,INTERNET_FLAG_RELOAD,0);如果(!hRequest)转到Ret0;//bRet=HttpAddRequestHeaders(hRequest,“Content-Type:application/x-www-form-urlencoded”,Len(FORMHEADERS),HTTP_ ADDREQ_ FLAG_ REPLACE | HTTP_ ADDREQ_ FLAG _ ADD);//如果(!bRet)//转到Ret0;bRet=HttpSendRequest(hRequest,NULL,0,lpPostData,nPostDataLen);while(TRUE){charcReadBuffer[4096];unsignedlonglNumberOfBytesRead;bRet=InternetReadFile(hRequest,cReadBudger,sizeof(cReadBuff)-1,&lNumberOfBytesRead);if(!bRet||!

转自:http://hi.baidu.com/benbearlove/item/1671c23017575825b3c0c53f

环境:xp sp3,vs2008,在静态库中使用 MFC

    1. #include<afxwin.h>
    2. #include<stdio.h>
    3. #include<windows.h>
    4. #include<string>
    5. #include"Wininet.h"
    6. #pragmacomment(lib,"Wininet.lib")
    7. //模拟浏览器发送HTTP请求函数
    8. std::stringHttpRequest(char*lpHostName,shortsPort,char*lpUrl,char*lpMethod,char*lpPostData,intnPostDataLen)
    9. {
    10. HINTERNEThInternet,hConnect,hRequest;
    11. BOOLbRet;
    12. std::stringstrResponse;
    13. hInternet=(HINSTANCE)InternetOpen("User-Agent",INTERNET_OPEN_TYPE_PRECONFIG,NULL,NULL,0);
    14. if(!hInternet)
    15. gotoRet0;
    16. hConnect=(HINSTANCE)InternetConnect(hInternet,lpHostName,sPort,NULL,"HTTP/1.1",INTERNET_SERVICE_HTTP,0,0);
    17. if(!hConnect)
    18. gotoRet0;
    19. hRequest=(HINSTANCE)HttpOpenRequest(hConnect,lpMethod,lpUrl,"HTTP/1.1",NULL,NULL,INTERNET_FLAG_RELOAD,0);
    20. if(!hRequest)
    21. gotoRet0;
    22. //bRet=HttpAddRequestHeaders(hRequest,"Content-Type:application/x-www-form-urlencoded",Len(FORMHEADERS),HTTP_ADDREQ_FLAG_REPLACE|HTTP_ADDREQ_FLAG_ADD);
    23. //if(!bRet)
    24. //gotoRet0;
    25. bRet=HttpSendRequest(hRequest,NULL,0,lpPostData,nPostDataLen);
    26. while(TRUE)
    27. {
    28. charcReadBuffer[4096];
    29. unsignedlonglNumberOfBytesRead;
    30. bRet=InternetReadFile(hRequest,cReadBuffer,sizeof(cReadBuffer)-1,&lNumberOfBytesRead);
    31. if(!bRet||!lNumberOfBytesRead)
    32. break;
    33. cReadBuffer[lNumberOfBytesRead]=0;
    34. strResponse=strResponse+cReadBuffer;
    35. }
    36. Ret0:
    37. if(hRequest)
    38. InternetCloseHandle(hRequest);
    39. if(hConnect)
    40. InternetCloseHandle(hConnect);
    41. if(hInternet)
    42. InternetCloseHandle(hInternet);
    43. returnstrResponse;
    44. }
    45. voidmain()
    46. {
    47. //CStringstrResponse=HttpRequest("translate.google.com",80,"/translate_t?langpair=en|zh-CN","POST","hl=zh-CN&ie=UTF-8&text=thisisme&langpair=en|zh-CN",strlen("hl=zh-CN&ie=UTF-8&text=thisisme&langpair=en|zh-CN"));
    48. std::stringstrResponse=HttpRequest("www.hao123.com",80,NULL,"GET",NULL,0);
    49. FILE*fp=fopen("C:\123.htm","wb");
    50. fwrite(strResponse.c_str(),strResponse.length(),1,fp);
    51. fclose(fp);
    52. }

免责声明:文章转载自《C++发送HTTP请求---亲测可行(转)》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇【idea】用IDEA把自己的工具类打jar包,并引入到springboot项目中jsp语法与标签下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

c#实现http请求并解析返回之json

   C#是通过HttpWebRequest类和HttpWebResponseL类来实现http请求的发出和http响应的接收的,由于本人刚用这两个类,不是太熟悉,所以属性和方法就不在这里给大家讲解了。        代码如下: using System; using System.Collections.Generic; using System.Lin...

C#语言Windows服务程序测试网站发送HTTP请求超时解决办法

      我在用C#做Windows服务程序,一个每隔一定时间测试网站链接的程序,可是做的时候发现,发送的HTTP请求,每次都是只有2个是返回200,其他的都是Opretion over time[具体英文我忘了,反正就是操作超时]。弄了很久,由于对C# 不熟悉,以为是函数了解的不清楚的错,试了很多次,依然不成功。最后才想到百度“HTTPWebReque...

HAProxy安装配置详解

简介 HAProxy提供高可用性、负载均衡以及基于TCP和HTTP应用的代理,支持虚拟主机,它是免费、快速并且可靠的一种解决方案。 HAProxy特别适用于那些负载特大的web站点,这些站点通常又需要会话保持或七层处理。 HAProxy运行在当前的硬件上,完全可以支持数以万计的并发连接。并且它的运行模式使得它可以很简单安全的整合进您当前的架构中, 同时可...

Java接口自动化测试实战001----get、post方法实现与封装

一、接口测试 1、接口测试流程 根据接口文档编写测试用例 准备测试数据 准备工具(测试工具or接口测试代码) 填写接口信息(接口地址、请求方式等) 准备请求头数据(如果有必要,比如:cookies,Content-Type等) 发起请求,获取接口的相应信息(状态码、响应报文、或者某些特殊的响应头数据) 根据报文判断实际与预期结果是否一致 2、HTTP请...

loadrunner---&amp;lt;二&amp;gt;---菜鸟对cookie的思考

http://www.cnblogs.com/Pierre-de-Ronsard/archive/2012/11/19/2772630.html loadrunner---<二>---菜鸟对cookie的思考 lr是怎么将cookie添加到录制的脚本中的?lr中cookie是做什么的? 首先将解决两个疑问: 1--什么是cookie?-- C...

haproxy 配置详解

OPTION 选项: option httpclose :HAProxy会针对客户端的第一条请求的返回添加cookie并返回给客户端,客户端发送后续请求时会发送                              此cookie到HAProxy,HAProxy会针对此cookie分发到上次处理此请求的服务器上,如果服务器不能忽略           ...