我正在使用带有 MFC 应用程序的 Visual Studio 2005,该应用程序具有访问网站以获取小文件的功能。我正在使用WinINet 函数,see the WinINet API reference in Microsoft Windows Dev Center,它提供了一组简单的函数,使用 URL 访问网站,该 URL 指定协议(HTTP、HTTPS、FTP 等)以下载一个小文件。
在libcurl 站点上查找此 Microsoft 技术说明 Article Id 238425 - INFO: WinInet Not Supported for Use in Services 的引用,该说明已于 2015 年 8 月 12 日标记为已停用。文章摘要是:
Microsoft Win32 Internet 函数(从 WinInet.dll 导出)是
从服务或 Internet 信息运行时不受支持
服务器 (IIS) 应用程序(也是一项服务)。本文讨论
在服务或 Internet 信息服务器中使用 WinInet.dll
应用程序。
我在 MFC 应用程序中使用的适用源代码有一个对话框,该对话框使用 HTTPS 类型的 URL,向其附加附加信息以构建完整的 URI,然后转到网站以提取完整的小文件:
int GetFile (HINTERNET hOpen, TCHAR * szURL, BYTE szTemp[4096])
{
DWORD dwSize;
TCHAR szHead[15];
HINTERNET hConnect;
szHead[0] = '\0';
szTemp[0] = 0;
// Opens a resource specified by a complete HTTP URL.
if ( !(hConnect = InternetOpenUrl( hOpen, szURL, szHead, 15, INTERNET_FLAG_DONT_CACHE, 0)))
{
DWORD dwlasterror = GetLastError();
if (dwlasterror == ERROR_INTERNET_NAME_NOT_RESOLVED) {
AfxMessageBox (_T("Error: ERROR_INTERNET_NAME_NOT_RESOLVED - check LAN connectivity."));
} else if (dwlasterror == ERROR_INTERNET_TIMEOUT) {
AfxMessageBox (_T("Error: ERROR_INTERNET_TIMEOUT - check LAN connectivity."));
} else if (dwlasterror == ERROR_INTERNET_SERVER_UNREACHABLE) {
AfxMessageBox (_T("Error: ERROR_INTERNET_SERVER_UNREACHABLE - check LAN connectivity."));
} else if (dwlasterror == ERROR_INTERNET_OPERATION_CANCELLED) {
AfxMessageBox (_T("Error: ERROR_INTERNET_OPERATION_CANCELLED - check LAN connectivity."));
} else {
CString msg;
msg.Format (_T("Error: GetLastError() returned %d."), dwlasterror);
AfxMessageBox (msg);
}
return -2;
}
// Reads data from a handle opened by the InternetOpenUrl, FtpOpenFile, or HttpOpenRequest function.
if (InternetReadFile (hConnect, szTemp, 4096, &dwSize) )
{
if (dwSize) {
return dwSize;
}
return -3;
}
return -4;
}
int DownloadURLImage (TCHAR * szURL, BYTE szTemp[4096])
{
int result = -1;
HINTERNET hInternet;
// Initializes an application's use of the WinINet functions.
hInternet= InternetOpen (_T("DeviceConfig"), INTERNET_OPEN_TYPE_DIRECT, NULL, NULL, NULL);
if (hInternet) {
// if open succeeded then get the file and close the handle as we be done.
result = GetFile (hInternet, szURL, szTemp) ;
InternetCloseHandle(hInternet);
}
return result ;
}