Https数据读取C++实现
(2016-08-29 16:30:16)
标签:
https数据读取c实现 |
分类: VC |
找了一圈http://blog.csdn.net/sxyaxy/article/details/20569939
BOOL CUpdateDlg::GetData(CString *webData)
{
CString strServerName= "php-hoper.rhcloud.com";
CString strTarget = "/ver/ver.txt";
DWORD dwSeviceType= AFX_INET_SERVICE_HTTPS;
CString Url ="https://php-hoper.rhcloud.com/ver/ver.txt";
INTERNET_PORT nPort =
INTERNET_DEFAULT_HTTPS_PORT;
CString strHeaders = _T("Content-Type:
application/x-www-form-urlencoded");
AfxParseURL(Url,dwSeviceType,strServerName,strTarget,nPort);
CInternetSession sess;
sess.SetOption(INTERNET_OPTION_CONNECT_TIMEOUT,1000*20);
sess.EnableStatusCallback(TRUE);
CHttpConnection* pHttpConnect =
sess.GetHttpConnection(strServerName,nPort);
CHttpFile* pHttpFile =
pHttpConnect->OpenRequest(CHttpConnection::HTTP_VERB_GET,
strTarget,NULL,1,NULL,NULL,INTERNET_FLAG_SECURE);
try
{
BOOL bResult =pHttpFile->SendRequest(strHeaders);
}
catch (CInternetException* pException)
{
pException->m_dwError;
pException->Delete();
}
pHttpFile->SetReadBufferSize(2048);
CString str;
CString strGetData;
while(pHttpFile->ReadString(strGetData))
{
str +="\r\n";
str +=strGetData;
}
CFile file("https.data",CFile::modeCreate | CFile::modeWrite);
file.Write(str,str.GetLength());
file.Close();
pHttpFile->Close();
delete pHttpFile;
pHttpConnect->Close();
delete pHttpConnect;
sess.Close();
return TRUE;
}
应用:
CUpdateDlg Data;
CString strData;
Data.GetData(&strData);