• 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
  • 相关阅读:
    MybatisPlus 从零开始 全面学习!
    原子物理中的组合常数
    请简要说明 Mysql 中 MyISAM 和 InnoDB 引擎的区别
    选择排序.
    <多态>——《C++高阶》
    MySQL 字段的基本操作:添加、修改和删除字段(详解)
    MySQL数据库UDF提权学习
    Vertica 向 GBase8a 迁移指南之VARBINARY/BYTEA/RAW 数据类型
    光缆老化实例分析
    类与对象(三) 拷贝构造与赋值运算符重载
  • 原文地址:https://blog.csdn.net/shyrainxy/article/details/134211118