-2

I am searching for a long time on net. But no use. Please help or try to give some ideas how to achieve this.

Thanks in advance.

i Have the following code:

void QtGuiApplication::getBtnClick() {
    DWORD dwSize = 0;
    DWORD dwDownloaded = 0;
    LPSTR pszOutBuffer;
    BOOL  bResults = FALSE;
    HINTERNET  hSession = NULL,
        hConnect = NULL,
        hRequest = NULL;

    // Use WinHttpOpen to obtain a session handle.
    hSession = WinHttpOpen(L"WinHTTP Example/1.0",
        WINHTTP_ACCESS_TYPE_DEFAULT_PROXY,
        WINHTTP_NO_PROXY_NAME,
        WINHTTP_NO_PROXY_BYPASS, 0);

    // Specify an HTTP server.
    if (hSession) {

        hConnect = WinHttpConnect(hSession, L"127.0.0.1",
            INTERNET_DEFAULT_HTTP_PORT, 0);
    }
    else {
        qDebug("Error has WinHttpOpen");
        QString errorStr = QString::number(GetLastError());
        qDebug(qPrintable(errorStr));
    }



    // Create an HTTP request handle.
    if (hConnect) {
        hRequest = WinHttpOpenRequest(hConnect, L"GET", NULL,
            NULL, WINHTTP_NO_REFERER,
            WINHTTP_DEFAULT_ACCEPT_TYPES,
            WINHTTP_FLAG_SECURE);
    }
    else {
        qDebug("Error has WinHttpConnect");
        QString errorStr = QString::number(GetLastError());
        qDebug(qPrintable(errorStr));
    }


    // Send a request.
    if (hRequest) {
        bResults = WinHttpSendRequest(hRequest,
            WINHTTP_NO_ADDITIONAL_HEADERS,
            0, WINHTTP_NO_REQUEST_DATA, 0,
            0, 0);
    }
    else {
        qDebug("Error has WinHttpOpenRequest");
        QString errorStr = QString::number(GetLastError());
        qDebug(qPrintable(errorStr));
    }


    // End the request.
    if (bResults) {
        bResults = WinHttpReceiveResponse(hRequest, NULL);
    }
    else {
        qDebug("Error has WinHttpSendRequest");
        QString errorStr = QString::number(GetLastError());
        qDebug(qPrintable(errorStr));
    }


    // Keep checking for data until there is nothing left.
    if (bResults)
        do
        {
            // Check for available data.
            dwSize = 0;
            if (!WinHttpQueryDataAvailable(hRequest, &dwSize))
                printf("Error %u in WinHttpQueryDataAvailable.\n", GetLastError());

            // Allocate space for the buffer.
            pszOutBuffer = new char[dwSize + 1];
            if (!pszOutBuffer)
            {
                printf("Out of memory\n");
                dwSize = 0;
            }
            else
            {
                // Read the Data.
                ZeroMemory(pszOutBuffer, dwSize + 1);

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

                // Free the memory allocated to the buffer.
                delete[] pszOutBuffer;
            }

        } while (dwSize > 0);


        // Report any errors.
        if (!bResults) {
            qDebug("Error has occurred");
            QString str = QString::number(GetLastError());
            qDebug(qPrintable(str));
        }


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

I try to access the local server through winhttp. The browser can normally access 127.0.0.1.

bResults = WinHttpSendRequest(hRequest,
            WINHTTP_NO_ADDITIONAL_HEADERS,
            0, WINHTTP_NO_REQUEST_DATA, 0,
            0, 0);

error var GetLastError returns 6 after call to WinHttpSendRequest

The code of the service running with springboot is as follows

@Controller
public class MyController {
    @ResponseBody
    @RequestMapping("")
    public String home() {
        return "Home";
    }
}

Springboot run information: Tomcat started on port(s): 80 (http) with context path ''

Using winhttp API to access services, Get springboot error information

I've tried a lot of ways, and it's still useless.

For example, modify the springboot port and winhttp API request port to 8089.

kkc
  • 5
  • 2
  • 2
    `ERROR_INVALID_HANDLE 6 (0x6) The handle is invalid` - Straight from [System Error Codes (0-499)](https://learn.microsoft.com/en-us/windows/win32/debug/system-error-codes--0-499-) - Check your return values before you use them. It's **always** a good idea. You can also encapsulate every handle and throw an exception if anything exceptional happens. That's what I prefer. It makes it possible to program with assumptions, but get "saved" if they turn out to be bad assumptions. – Ted Lyngmo Feb 27 '20 at 01:59
  • 1
    You check return values, but even when they indicate failure, you keep going! Looks like your program failed at an earlier step. Fix your error checking code and you'll see where. – David Heffernan Feb 27 '20 at 04:31
  • 2 out of 4 times, you are calling [GetLastError](https://learn.microsoft.com/en-us/windows/win32/api/errhandlingapi/nf-errhandlingapi-getlasterror) at a time, where it returns an indeterminate value. – IInspectable Feb 27 '20 at 09:37

1 Answers1

0

From WinHttpConnect

INTERNET_DEFAULT_HTTP_PORT

Uses the default port for HTTP servers (port 80).

INTERNET_DEFAULT_HTTPS_PORT

Uses the default port for HTTPS servers (port 443). Selecting this port does not automatically establish a secure connection. You must still specify the use of secure transaction semantics by using the WINHTTP_FLAG_SECURE flag with WinHttpOpenRequest.

The :80 part is the TCP port. You can consider these ports as communications endpoints on a particular IP address (in the case of localhost - 127.0.0.1). The IANA is responsible for maintaining the official assignments of standard port numbers for specific services. Port 80 happens to be the standard port for HTTP.

Refer: What's the whole point of “localhost”, hosts and ports at all?

So you need to change INTERNET_DEFAULT_HTTPS_PORT to INTERNET_DEFAULT_HTTP_PORT.

Strive Sun
  • 5,988
  • 1
  • 9
  • 26
  • 1
    What's really important here, far beyond the specific error is to check for errors properly. A good answer should drive that point home. – David Heffernan Feb 27 '20 at 08:17
  • Thank you Strive Sun - MSFT,Your method is effective, but it doesn't solve my problem. I use GetLastError in all winhttp APIs. – kkc Feb 27 '20 at 15:12
  • Still call GetLastError to return 6. I use springboot code: Controller public class MyController { ResponseBody RequestMapping("") public String home() { System.out.println("get Home"); return "Home"; } } Tomcat run message: Tomcat started on port(s): 80 (http) with context path. [I visited the springboot service and found the springboot error](https://stackoverflow.com/questions/49273847/springboot-error-parsing-http-request-header) – kkc Feb 27 '20 at 15:22
  • @黄东东 Which api return 6? For springboot code, it seems be related to JAVA. If it only accesses the localhost, can it succeed? – Strive Sun Feb 28 '20 at 03:08