0

I am trying to access file using WinHTTP from a web site and it fails for some web sites. I cannot use WinINet as WinINet does not support server implementations and cannot be used from a service.

It fails with error 12029 "The attempt to connect to the server failed", when WinHttpSendRequest() is executed for WEB_URL = "www.plusserver.com"

It works when WEB_URL = "www.google.com"

I can access both the web sites using Chrome web browser, so there is no connectivity issue. Maybe firewall on my end or the server end, am not sure if a firewall is blocking on a specific port.

Sample source code as below -

bool ReadFileFromInternet(LPCWSTR WEB_URL, DWORD dwSize, LPSTR pszOutBuffer)
{
    DWORD dwDownloaded = 0;
    BOOL  bResults = FALSE;
    HINTERNET  hSession = NULL,
        hConnect = NULL,
        hRequest = NULL;

    // Use WinHttpOpen to obtain a session handle.
    hSession = WinHttpOpen(WEB_URL,
        WINHTTP_ACCESS_TYPE_DEFAULT_PROXY,
        WINHTTP_NO_PROXY_NAME,
        WINHTTP_NO_PROXY_BYPASS, 0);

    // Specify an HTTP server.
    if (hSession)
        hConnect = WinHttpConnect(hSession, WEB_URL,
            INTERNET_DEFAULT_HTTPS_PORT, 0);

    int err = GetLastError();

    // Create an HTTP request handle.
    if (hConnect)
        hRequest = 
        WinHttpOpenRequest(hConnect, L"GET", NULL,
            NULL, WINHTTP_NO_REFERER,
            WINHTTP_DEFAULT_ACCEPT_TYPES,
            WINHTTP_FLAG_SECURE);
    err = GetLastError();

    // Send a request.
    if (hRequest)
        bResults = WinHttpSendRequest(hRequest,
            WINHTTP_NO_ADDITIONAL_HEADERS, 0,
            WINHTTP_NO_REQUEST_DATA, 0,
            0, 0);
    err = GetLastError();

    // End the request.
    if (bResults)
    {
        err = GetLastError();
        bResults = WinHttpReceiveResponse(hRequest, NULL);
    }
    err = GetLastError();

    if (bResults)
    {
        if (!WinHttpReadData(hRequest, (LPVOID)pszOutBuffer,
            dwSize, &dwDownloaded))
            printf("Error %u in WinHttpReadData.\n", GetLastError());
    }

    // Report any errors.
    if (!bResults)
        printf("Error %d has occurred.\n", GetLastError());

    // Close any open handles.
    if (hRequest) WinHttpCloseHandle(hRequest);
    if (hConnect) WinHttpCloseHandle(hConnect);
    if (hSession) WinHttpCloseHandle(hSession);

    return bResults;
}

How to troubleshoot this further? Am using Windows-7 , 64-bit

Sanjay Karia
  • 309
  • 1
  • 3
  • 13
  • You can capture and analyze the HTTP traffic with [Wireshark](https://www.wireshark.org/) to see what happens. – rveerd Jan 24 '19 at 16:11
  • 2
    I have a service that uses WinInet as a client to access a webserver (Amazon AWS) and it works fine. – Remy Lebeau Jan 24 '19 at 23:11
  • 1
    Try using [`WinHttpSetStatusCallback()`](https://learn.microsoft.com/en-us/windows/desktop/api/winhttp/nf-winhttp-winhttpsetstatuscallback) to find out exactly what WinHTTP is doing internally and at what stage it is failing at. Your error handling needs a little work, though. – Remy Lebeau Jan 24 '19 at 23:14
  • @RemyLebeau I need to use WinHttp from a Windows service and as per docs it cannot be used. – Sanjay Karia Jan 25 '19 at 04:23
  • @SanjayKaria and as I said, I do use WinInet in a Windows Service and it works fine, despite what the documentation says. – Remy Lebeau Jan 25 '19 at 04:31
  • @rveerd - I tried Wireshark, found TCP errors related to connection being reset, but not sure how to analyze further. I have limited /no knowledge in using Wireshark. – Sanjay Karia Jan 25 '19 at 10:28
  • 1
    @RemyLebeau I tried with WinINet and it worked, will test further within the service, – Sanjay Karia Jan 26 '19 at 04:21
  • @RemyLebeau WinINet is working good inside Windows service, although this is a workaround, it has resolved the issue and I would mark this solution as an answer. – Sanjay Karia Jan 31 '19 at 06:57

0 Answers0