0

I am attempting to connect to a proxy using WinHTTP. However, the output says "Error 122 in WinHttpQueryOption." I'm not sure why this is. "proxy" is just an std::string like "21.169.73.65:34679". The problematic code is below.

    std::wstring stemp = std::wstring(proxy.begin(), proxy.end());
    const wchar_t* sw = stemp.c_str();

    HINTERNET hSession2=WinHttpOpen(L"WinHTTP Example/1.0", WINHTTP_ACCESS_TYPE_NAMED_PROXY, L"http://"+*sw, WINHTTP_NO_PROXY_BYPASS, 0);
    if (hSession2)
        {


            // Use WinHttpQueryOption to retrieve internet options.
            if (WinHttpQueryOption( hSession2,
                                    WINHTTP_OPTION_CONNECT_TIMEOUT,
                                    &data, &dwSize))
            {
                printf("Connection timeout: %u ms\n\n",data);
            }
            else
            {
                printf( "Error %u in WinHttpQueryOption.\n",
                GetLastError());
            }

            // When finished, release the HINTERNET handle.
            WinHttpCloseHandle(hSession2);
        }
        else
        {
            printf("Error %u in WinHttpOpen.\n", GetLastError());
        }

I read some other posts that had to do with this error, but they were not specifically about WinHTTP so I was not sure what should be done about it.

Bruhman
  • 33
  • 6
  • How are data and dwSize initialized? – nevilad Mar 06 '21 at 16:51
  • 1
    [122](https://learn.microsoft.com/en-us/windows/win32/debug/system-error-codes--0-499-) is `ERROR_INSUFFICIENT_BUFFER`. The documentation for [WinHttpQueryOption](https://learn.microsoft.com/en-us/windows/win32/api/winhttp/nf-winhttp-winhttpqueryoption) explains, what this means. – IInspectable Mar 06 '21 at 18:24
  • 1
    `L"http://"+*sw` doesn't do what you think it does. Use `(L"http://"+stemp).c_str()` instead. Also, `std::wstring(proxy.begin(), proxy.end())` is not the correct way to convert a `std::string` to a `std::wstring`. If you have 8bit ANSI input strings, you should use ANSI API functions (ie `WinHttpOpenA`) instead of Unicode functions (ie `WinHttpOpenW`). – Remy Lebeau Mar 06 '21 at 19:46
  • `WinHttpQueryOption`: Setting `lpBuffer` to NULL causes this function to return FALSE. Calling `GetLastError` then returns `ERROR_INSUFFICIENT_BUFFER` and `lpdwBufferLength` contains the number of bytes required to hold the requested information. But what I’m more curious about is that I tested and found that `L"http://"+*sw` does not return the correct string. You can use `std::wstring proxy_new = (L"http://" + stemp).c_str(); instead. As Remy said that. – Strive Sun Mar 08 '21 at 02:40
  • @IInspectable could you please kindly help here: https://stackoverflow.com/questions/66563076/winhttpsendrequest-is-failing-with-error-127 – aromahola Mar 10 '21 at 13:14

0 Answers0