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();
相关推荐
杜子不疼.2 小时前
基于ATVC模板库的Ascend C Vector算子快速开发指南
c语言·开发语言·mfc
范纹杉想快点毕业15 小时前
实战级ZYNQ中断状态机FIFO设计
java·开发语言·驱动开发·设计模式·架构·mfc
范纹杉想快点毕业18 小时前
状态机设计与嵌入式系统开发完整指南从面向过程到面向对象,从理论到实践的全面解析
linux·服务器·数据库·c++·算法·mongodb·mfc
yixvxi1 天前
RFC 8659:DNS CAA资源记录
服务器·https·ssl
三水不滴1 天前
计算机网络核心网络模型
经验分享·笔记·tcp/ip·计算机网络·http·https
SunflowerCoder1 天前
基于插件化 + Scriban 模板引擎的高效 HTTP 协议中心设计
http·c#
Remember_9931 天前
MySQL 索引详解:从原理到实战优化
java·数据库·mysql·spring·http·adb·面试
404Clukay1 天前
Windows Server 配置 Let‘s Encrypt 免费 HTTPS 证书(WACS + Nginx 自动化方案)
windows·nginx·https
2501_915918411 天前
在 iOS 环境下查看 App 详细信息与文件目录
android·ios·小程序·https·uni-app·iphone·webview
Zach_yuan1 天前
从零理解 HTTP:协议原理、URL 结构与简易服务器实现
linux·服务器·网络协议·http