MFC发送http https以及json解析

域名解析成IP

cpp 复制代码
		char szWeb[128] = "www.baidu.com";
		struct hostent *pHost = NULL;
		pHost = gethostbyname(szWeb);//完成主机名到域名的解析
		char *IP = inet_ntoa(*((struct in_addr *)pHost->h_addr));
		CString ipStr = IP;

请求三部曲:

1、CInternetSession session;

cpp 复制代码
		CInternetSession session;
		session.SetOption(INTERNET_OPTION_CONNECT_TIMEOUT, 1000 * 20);

2、CHttpConnection* pConnection;

cpp 复制代码
		CHttpConnection* pConnection;
		pConnection = session.GetHttpConnection(strServer, wPort);

3、pConnection->OpenRequest

cpp 复制代码
		//https
		pConnection->OpenRequest(CHttpConnection::HTTP_VERB_POST, ("post.do"), NULL, 1, NULL, NULL, INTERNET_FLAG_SECURE
				| INTERNET_FLAG_DONT_CACHE | INTERNET_FLAG_RELOAD | INTERNET_FLAG_IGNORE_CERT_CN_INVALID | INTERNET_FLAG_IGNORE_CERT_DATE_INVALID);
		//http
		pConnection->OpenRequest(CHttpConnection::HTTP_VERB_POST, ("post.do"));

4、pFile->SendRequest(szHeaders, (LPVOID)reqchar, strlen(reqchar));

cpp 复制代码
		pFile->SendRequest(szHeaders, (LPVOID)reqchar, strlen(reqchar));

5、pFile->QueryInfoStatusCode(dwRet)

https请求

cpp 复制代码
			CString strURL = "https://209.144.91.204:443/";
			CString strServer, strObject;
			INTERNET_PORT wPort;
			DWORD dwType;
			if (!AfxParseURL(strURL, dwType, strServer, strObject, wPort))
			{
				return false;//URL解析错误
			}
			pConnection = session.GetHttpConnection(strServer, wPort);						//二、连接到Http服务器:
			if (NULL == pConnection)
			{
				return false;
			}
			pFile = pConnection->OpenRequest(CHttpConnection::HTTP_VERB_POST, ("/post.do"), NULL, 1, NULL, NULL, INTERNET_FLAG_SECURE
				| INTERNET_FLAG_DONT_CACHE | INTERNET_FLAG_RELOAD | INTERNET_FLAG_IGNORE_CERT_CN_INVALID | INTERNET_FLAG_IGNORE_CERT_DATE_INVALID);

http请求

cpp 复制代码
			CString dnsUrl = ipStr + ":80";
			pConnection = session.GetHttpConnection(url);
			pFile = pConnection->OpenRequest(CHttpConnection::HTTP_VERB_POST, ("/post.do"));

json解析

cpp 复制代码
			int len = pFile->GetLength();
			char buf[2048];
			int numread;
			Json::Reader jsonReader;
			Json::Value root;
			while ((numread = pFile->Read(buf, sizeof(buf) - 1)) > 0)
			{
				buf[numread] = '\0';
				strFile += buf;
			}
			if (log)
				AfxMessageBox(strFile);
			if (jsonReader.parse(buf, root)) {
				int retCode = root["retcode"].asInt();
相关推荐
yanlou23317 小时前
[C++/Linux HTTP项目] HTTP服务器基于muduo高性能服务器搭载【深入详解】
运维·服务器·http·muduo库·http高性能服务器
2501_9159184117 小时前
HTTPS 代理失效,启用双向认证(mTLS)的 iOS 应用网络怎么抓包调试
android·网络·ios·小程序·https·uni-app·iphone
不许哈哈哈19 小时前
HTTP协议基础(运维开发面试版)
http·面试·运维开发
王锋(oxwangfeng)20 小时前
Nginx 四层 TCP 与七层 HTTP 转发实战指南
tcp/ip·nginx·http
2501_9151063220 小时前
混合应用(Hybrid)安全加固,不依赖源码对成品 IPA 混淆
android·安全·小程序·https·uni-app·iphone·webview
码农水水20 小时前
传音Java面试被问:HTTP/2的多路复用和头部压缩实现
java·开发语言·spring boot·后端·http·面试·职场和发展
00后程序员张21 小时前
无需越狱,来对 iOS 设备进行调试、管理与分析
android·ios·小程序·https·uni-app·iphone·webview
00后程序员张21 小时前
在 iOS 上架中如何批量方便快捷管理 Bundle ID
android·ios·小程序·https·uni-app·iphone·webview
小白电脑技术21 小时前
飞牛绑定免费二级域名ccwu.cc后部署SSL证书教程
网络协议·https·ssl
她说..21 小时前
Java Web 开发:请求头、请求体、响应体 数据获取全攻略(附实战示例)
java·开发语言·spring boot·spring·http·spring mvc