什么会导致 InternetQueryOption 出现 ERROR_INTERNET_INCORRECT_HANDLE_TYPE (12018)?
我正在开发一个简单的小函数,使用 WinInet 函数(即 InternetOpen 和 InternetOpenURL)从启用 SSL 的网站下载文件。 我最初因 ERROR_INTERNET_INVALID_CA
(12045) 对 InternetOpenURL 的调用失败,因为我在测试服务器上使用自签名证书,并发现 (http://support.microsoft.com/kb/q182888/) 修复似乎是使用 InternetQueryOption/InternetSetOption 组合将各种标志传递给 INTERNET_OPTION_SECURITY_FLAGS 选项。 但现在,InternetQueryOption 失败,并返回来自 GetLastError() 的 ERROR_INTERNET_INCORRECT_HANDLE
(12018) 响应。 有什么想法为什么会出现这种情况吗? 我使用的是直接来自 InternetOpen 的句柄,该句柄以前在非 SSL InternetOpenURL 上运行良好。 这不应该是正确的句柄吗?
我没有实际的代码(不同的计算机),但它与以下内容非常相似,并且在 InternetGetOption 上失败,并显示 ERROR_INTERNET_INCORRECT_HANDLE
:
HINTERNET hReq = InternetOpen(...)
if (!hReq) { printf("InternetOpen Error: %d", GetLastError()); }
DWORD dwFlags = 0;
DWORD dwBuffLen = sizeof(dwFlags);
BOOL ret = false;
ret = InternetQueryOption(hReq, INTERNET_OPTION_SECURITY_FLAGS,
(LPVOID)&dwFlags, &dwBuffLen);
if (!ret) { printf("InternetQueryOption Error: %d", GetLastError()); }
dwFlags |= SECURITY_FLAG_IGNORE_UNKNOWN_CA;
ret = InternetSetOption(hReq, INTERNET_OPTION_SECURITY_FLAGS,
&dwFlags, sizeof (dwFlags) );
if (!ret) { printf("InternetSetOption Error: %d", GetLastError()); }
InternetOpenURL(hReq, ...)
I'm working on a simple little function to download a file from an SSL-enabled website using the WinInet functions, namely InternetOpen and InternetOpenURL. I had was initially failing the call to InternetOpenURL with a ERROR_INTERNET_INVALID_CA
(12045) because I was using a self-signed certificate on my test server, and found out (http://support.microsoft.com/kb/q182888/) that the fix seemed to be to use the InternetQueryOption/InternetSetOption combination to pass various flags to INTERNET_OPTION_SECURITY_FLAGS
option. Now, however, InternetQueryOption fails with a ERROR_INTERNET_INCORRECT_HANDLE
(12018) response from GetLastError(). Any ideas why this would be the case? I'm using the handle that came directly from InternetOpen, which previously worked fine with a non-SSL InternetOpenURL. Shouldn't this be the correct handle?
I don't have the actual code (different computer), but it is very similar to the following, and fails on InternetGetOption with ERROR_INTERNET_INCORRECT_HANDLE
:
HINTERNET hReq = InternetOpen(...)
if (!hReq) { printf("InternetOpen Error: %d", GetLastError()); }
DWORD dwFlags = 0;
DWORD dwBuffLen = sizeof(dwFlags);
BOOL ret = false;
ret = InternetQueryOption(hReq, INTERNET_OPTION_SECURITY_FLAGS,
(LPVOID)&dwFlags, &dwBuffLen);
if (!ret) { printf("InternetQueryOption Error: %d", GetLastError()); }
dwFlags |= SECURITY_FLAG_IGNORE_UNKNOWN_CA;
ret = InternetSetOption(hReq, INTERNET_OPTION_SECURITY_FLAGS,
&dwFlags, sizeof (dwFlags) );
if (!ret) { printf("InternetSetOption Error: %d", GetLastError()); }
InternetOpenURL(hReq, ...)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我曾经收到类似的错误。 然后我将 HttpOpenRequest(...) 返回的句柄传递给 InternetQueryOption,它工作得很好。 试试看。
I used to receive similar error. I then passed the handle returned by a HttpOpenRequest(...) to InternetQueryOption and it worked just fine. Try it out.
来自 INTERNET_OPTION_SECURITY_FLAGS 的 MSDN 文档:
您的用户名上尚未发生任何交易。 InternetOpen 为您提供根 HINTERNET 句柄。 这可能适用于 HTTP、FTP 或 Gopher 连接 - 而安全选项相当特定于 HTTPS,并且在很大程度上也取决于远程服务器支持的内容。 因此,您向 Wininet 询问它无法提供的信息。
From the MSDN docs for INTERNET_OPTION_SECURITY_FLAGS:
No transaction has occurred on your handle yet. InternetOpen gives you the root HINTERNET handle. This could apply to a HTTP, FTP or Gopher connection - whereas the security options are fairly specific to HTTPS and largely also dependent on what the remote server supports. As such, you're asking Wininet for information that it can't give you.
我发现您没有检查从
InternetOpen
返回的hReq
。 也许这就是你问题的根源。 如果您在调用InternetOpen
之后立即添加它,看看这会告诉您什么:I see you're not checking the
hReq
you get back fromInternetOpen
. Perhaps that is the root of your problem. See what this tells you if you add it right after the call toInternetOpen
: