• MFC发送http https以及json解析


    域名解析成IP

    		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
    • 2
    • 3
    • 4
    • 5

    请求三部曲:

    1、CInternetSession session;

    		CInternetSession session;
    		session.SetOption(INTERNET_OPTION_CONNECT_TIMEOUT, 1000 * 20);
    
    • 1
    • 2

    2、CHttpConnection* pConnection;

    		CHttpConnection* pConnection;
    		pConnection = session.GetHttpConnection(strServer, wPort);
    
    • 1
    • 2

    3、pConnection->OpenRequest

    		//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"));
    
    • 1
    • 2
    • 3
    • 4
    • 5

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

    		pFile->SendRequest(szHeaders, (LPVOID)reqchar, strlen(reqchar));
    
    • 1

    5、pFile->QueryInfoStatusCode(dwRet)

    https请求

    			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);
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15

    http请求

    			CString dnsUrl = ipStr + ":80";
    			pConnection = session.GetHttpConnection(url);
    			pFile = pConnection->OpenRequest(CHttpConnection::HTTP_VERB_POST, ("/post.do"));
    
    • 1
    • 2
    • 3

    json解析

    			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();
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
  • 相关阅读:
    java汉字转拼音pinyin4j-2.5.0.jar用法
    vant实现Select效果--单选和多选
    SpringMVC学习(1)
    Flask 学习-47.Flask-RESTX 自定义响应内容marshal_with
    深入探讨AJAX接口进度监控:实现步骤、代码示例与技术原理
    SQL注入漏洞及五大手法
    聚类与分类的区别
    信创势不可挡,数据传输软件怎样国产化替代?
    虹科分享 | 为工业机器人解绑,IO-Link wireless无线通讯技术可实现更加轻量灵活的机器人协作
    QT之QProcess
  • 原文地址:https://blog.csdn.net/shyrainxy/article/details/134211118