当前位置:首页 > 编程笔记 > 正文
已解决

MFC发送http https以及json解析

来自网友在路上 145845提问 提问时间:2023-11-06 11:15:09阅读次数: 45

最佳答案 问答题库458位专家为你答疑解惑

域名解析成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、CInternetSession session;

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

2、CHttpConnection* pConnection;

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

3、pConnection->OpenRequest

		//httpspConnection->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);//httppConnection->OpenRequest(CHttpConnection::HTTP_VERB_POST, ("post.do"));

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

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

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);

http请求

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

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();
查看全文

99%的人还看了

猜你感兴趣

版权申明

本文"MFC发送http https以及json解析":http://eshow365.cn/6-33561-0.html 内容来自互联网,请自行判断内容的正确性。如有侵权请联系我们,立即删除!