GET示例

#include <afxinet.h>
#include <iostream>
#include <vector>
#ifdef _UNICODE
#define COUT wcout
#else
#define COUT cout
#endif
using namespace std;
int main()
{
CInternetSession session(TEXT("MfcHttp"));
CHttpConnection *connection = session.GetHttpConnection(TEXT("example.com"), (INTERNET_PORT));
CHttpFile *file = connection->OpenRequest(CHttpConnection::HTTP_VERB_GET, TEXT(""));
try
{
file->SendRequest();
DWORD statusCode;
file->QueryInfoStatusCode(statusCode);
if (HTTP_STATUS_OK == statusCode)
{
CString contentLength;
file->QueryInfo(HTTP_QUERY_CONTENT_LENGTH, contentLength);
int fileSize = _ttoi(contentLength);
vector<char> buffer(fileSize + );
UINT numberOfBytesRead = file->Read(&buffer[], fileSize);
buffer[fileSize] = '\0';
COUT << &buffer[] << endl;
}
else
{
COUT << "statusCode" << statusCode << endl;
}
}
catch (CInternetException *ex)
{
TCHAR error[];
ex->GetErrorMessage(error, );
COUT << error << endl;
ex->Delete();
}
delete file;
delete connection;
system("pause");
return ;
}

若要访问HTTPS链接,应将上述代码中的

CHttpConnection *connection = session.GetHttpConnection(TEXT("example.com"), (INTERNET_PORT)80);
CHttpFile *file = connection->OpenRequest(CHttpConnection::HTTP_VERB_GET, TEXT(""));

更改为

CHttpConnection *connection = session.GetHttpConnection(TEXT("www.example.com"), INTERNET_FLAG_SECURE, (INTERNET_PORT)443);
CHttpFile *file = connection->OpenRequest(CHttpConnection::HTTP_VERB_GET, TEXT(""), nullptr, 1, nullptr, nullptr, INTERNET_FLAG_SECURE);

参考博文:MFC使用HttpGet和HttpPost方法与服务器通信

最新文章

  1. 给钛度产品的一些建议(Note)
  2. Java 网络爬虫获取页面源代码
  3. LVS集群之DR模式 实现
  4. windbg学习----.process
  5. 深入理解Java虚拟机(二)、Java对象的创建,内存布局和访问定位
  6. HTML5 game engines
  7. WisDom.Net 框架设计(二) 服务总线
  8. java并发包分析之———Deque和LinkedBlockingDeque
  9. 小米平板6.0系统如何无ROOT激活xposed框架的步骤
  10. for循环的beak continue用法
  11. Matplotlib(used in paper)
  12. Confluence 6 通过 SSL 或 HTTPS 运行 - 修改你 Confluence 的 server.xml 文件
  13. Centos7.2/7.3集群安装Kubernetes 1.8.4 + Dashboard(转)
  14. 从浏览器输入参数,到后台处理的vertx程序
  15. PTA (Advanced Level) 1020 Tree Traversals
  16. 16JavaScript for循环
  17. Simple上网导航--静态版
  18. bzoj 1067 分情况讨论
  19. poj3299 - Humidex
  20. sqlopsstudio 微软开源跨平台sql server 连接工具

热门文章

  1. 反汇编角度->C++ const
  2. C、C++变量auto,static,register,extern类型
  3. WEB学习-CSS中Margin塌陷
  4. Oracle PL/SQL块 多表查询(emp员工表、dept部门表、salgrade工资等级表)
  5. Deep learning with PyTorch: A 60 minute blitz _note(1) Tensors
  6. Leetcode总结之Graph
  7. abp ueditor 多图以及文件无法上传
  8. 配置laravel的nginx站点
  9. andriod 获得应用程序名称
  10. sublime快捷键设置