域名解析成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;
请求三部曲:文章来源地址https://www.toymoban.com/news/detail-743499.html
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
//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));
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();
文章来源:https://www.toymoban.com/news/detail-743499.html
到了这里,关于MFC发送http https以及json解析的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!