我有这个代码用于从我的网站读取数据文件:
CInternetSession iSession;
CHttpFile *pWebFile = nullptr;
DWORD dwStatusCode;
CString strError, strTargetZIP, strDownloadURL;
strTargetZIP = theApp.GetWorkingPath() + _T("AutoUpdate\\MWBDataUpdate.zip");
strDownloadURL = _T("http://www.publictalksoftware.co.uk/mwbdata/MWBDataUpdate.zip");
// ask user to go online
if (InternetGoOnline((LPTSTR)(LPCTSTR)strDownloadURL, hWnd, 0))
{
TRY
{
// our session should already be open
// try to open up internet session to my URL
// Use flag INTERNET_FLAG_RELOAD
pWebFile = (CHttpFile*)iSession.OpenURL(strDownloadURL, 1,
INTERNET_FLAG_TRANSFER_BINARY | INTERNET_FLAG_DONT_CACHE | INTERNET_FLAG_RELOAD);
if (pWebFile != nullptr)
{
if (pWebFile->QueryInfoStatusCode(dwStatusCode))
{
// 20x codes mean success
if ((dwStatusCode / 100) == 2)
{
}
else
{
// There was a problem!
strError.Format(IDS_TPL_INVALID_URL, dwStatusCode);
AfxMessageBox(strError, MB_OK | MB_ICONERROR);
}
}
}
else
{
// Note, there is no error log. Use new error message?
AfxMessageBox(IDS_STR_UPDATE_CHECK_ERR, MB_OK | MB_ICONERROR);
}
}
CATCH(CException, e)
{
e->GetErrorMessage(szError, _MAX_PATH);
AfxMessageBox(szError, MB_OK | MB_ICONERROR);
}
END_CATCH
// Tidy up
if (pWebFile != nullptr)
{
pWebFile->Close();
delete pWebFile;
}
iSession.Close();
}
我最近更改了我的网站以使用HTTPS
,我试图找到我需要做的只是在我的代码中更改URL
。我查看了CHttpFile
的最新文档,但只提到了HTTP
。
感谢您的澄清。
答案 0 :(得分:2)
CInternetSession::OpenURL
调用InternetOpenUrl
API。对于安全网站,InternetOpenUrl
需要INTERNET_FLAG_SECURE
。
将网址从http://
更改为https://
将INTERNET_FLAG_SECURE
添加到OpenURL
选项。
示例:
CInternetSession isession;
CString url = _T("https://www.google.com");
CString filename = _T("c:\\test\\test.html");
CHttpFile *httpfile = (CHttpFile*)isession.OpenURL(url, 1, INTERNET_FLAG_SECURE |
INTERNET_FLAG_TRANSFER_BINARY | INTERNET_FLAG_RELOAD);
if (httpfile)
{
DWORD dwStatusCode;
httpfile->QueryInfoStatusCode(dwStatusCode);
if(dwStatusCode == 200)
{
char buf[0x1000] = { 0 };
DWORD read = 0;
CFile file;
if (file.Open(filename, CFile::modeCreate | CFile::modeWrite))
while(InternetReadFile(*httpfile, buf, sizeof(buf), &read) && read)
file.Write(buf, read);
}
httpfile->Close();
delete httpfile;
}