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, ...)