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 天前
【IoT死磕系列】Day 7:只传8字节怎么控机械臂?学习工业控制 CANopen 的“对象字典”(附企业级源码)
数据结构·stm32·物联网·http
赤月奇3 天前
https改为http
数据挖掘·https·ssl
dengzhenyue3 天前
MFC 学习笔记
笔记·学习·mfc
21号 13 天前
Http粘包问题回顾
网络·网络协议·http
A懿轩A3 天前
【SpringBoot 快速开发】面向后端开发的 HTTP 协议详解:请求报文、响应码与常见设计规范
spring boot·http·设计规范
吧啦蹦吧3 天前
http-SNI
网络·网络协议·http
~kiss~3 天前
HTTP 429
网络·网络协议·http
Olive4 天前
深入理解 HTTP 请求重试:不只是指数退避那么简单
http
AI-小柒4 天前
Seedance 2.0(即梦 2.0)深度解析:AI 视频进入「导演级」可控时代
大数据·人工智能·网络协议·tcp/ip·http·音视频
弹简特4 天前
【JavaEE10-后端部分】SpringMVC05-综合案例1-从加法计算器看前后端交互:接口文档与HTTP通信详解
java·spring boot·spring·http