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();
相关推荐
Mysticbinary1 小时前
BurpSuite 代理原理 和 证书钉扎检测技术
http·https·网络代理·代理·网络流量·websockets·证书钉扎
油泼辣子多加1 小时前
HTTP 请求体格式详解
网络·网络协议·http
阿昭L14 小时前
HTTP原理
网络·网络协议·http
zhao32668575115 小时前
2025年代理IP三强横评:LoongProxy、神龙海外动态IP代理、全民HTTP怎么选?看完这篇不踩坑
网络协议·tcp/ip·http
on the way 12315 小时前
多线程之HardCodedTarget(type=OssFileClient, name=file, url=http://file)异常
网络·网络协议·http
一拳一个呆瓜18 小时前
【MFC】对话框节点属性:Language(语言)
c++·mfc
想睡hhh20 小时前
HTTPS协议——对于HTTP的协议的加密
http·https
福大大架构师每日一题1 天前
go 1.25.1发布:重点修复net/http跨域保护安全漏洞(CVE-2025-47910)
开发语言·http·golang
Chan161 天前
消息推送的三种常见方式:轮询、SSE、WebSocket
java·网络·websocket·网络协议·http·sse
司徒小夜2 天前
HTTP与HTTPS杂谈-HTTPS防御了什么
网络·http·https