MFC HTTP(S)请求笔记
2024-10-19 12:37:53
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方法与服务器通信
最新文章
- 给钛度产品的一些建议(Note)
- Java 网络爬虫获取页面源代码
- LVS集群之DR模式 实现
- windbg学习----.process
- 深入理解Java虚拟机(二)、Java对象的创建,内存布局和访问定位
- HTML5 game engines
- WisDom.Net 框架设计(二) 服务总线
- java并发包分析之———Deque和LinkedBlockingDeque
- 小米平板6.0系统如何无ROOT激活xposed框架的步骤
- for循环的beak continue用法
- Matplotlib(used in paper)
- Confluence 6 通过 SSL 或 HTTPS 运行 - 修改你 Confluence 的 server.xml 文件
- Centos7.2/7.3集群安装Kubernetes 1.8.4 + Dashboard(转)
- 从浏览器输入参数,到后台处理的vertx程序
- PTA (Advanced Level) 1020 Tree Traversals
- 16JavaScript for循环
- Simple上网导航--静态版
- bzoj 1067 分情况讨论
- poj3299 - Humidex
- sqlopsstudio 微软开源跨平台sql server 连接工具
热门文章
- 反汇编角度->C++ const
- C、C++变量auto,static,register,extern类型
- WEB学习-CSS中Margin塌陷
- Oracle PL/SQL块 多表查询(emp员工表、dept部门表、salgrade工资等级表)
- Deep learning with PyTorch: A 60 minute blitz _note(1) Tensors
- Leetcode总结之Graph
- abp ueditor 多图以及文件无法上传
- 配置laravel的nginx站点
- andriod 获得应用程序名称
- sublime快捷键设置