域名解析成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();