简体   繁体   中英

using wininet in C++

I Have a server that if I sent it "https://MyDomain/Dir1/Dir2/login.html?u=1234&t=5678" will respond with an xml document. Does anyone know how I can do this in wininet?

Here is what I have done so far:

std::string GetLoginData(void)
{
    HINTERNET hInternet, hConnection, hRequest;
    std::string retVal;

    hInternet = InternetOpen(L"My Application", INTERNET_OPEN_TYPE_DIRECT, NULL, NULL, 0);
    if(NULL != hInternet)
    {
        hConnection = InternetConnect(hInternet, L"MyDomain", INTERNET_DEFAULT_HTTPS_PORT, L"", L"", INTERNET_SERVICE_HTTP, 0, NULL);
        if(NULL != hConnection)
        {
            const wchar_t* lplpszAcceptTypes[] = {L"text/xml", L"application/xml", L"application/xhtml+xml", NULL};

            hRequest = HttpOpenRequest(hConnection, L"GET", L"Dir1/Dir2/Login.html", NULL, NULL, lplpszAcceptTypes,
                INTERNET_FLAG_DONT_CACHE | INTERNET_FLAG_IGNORE_CERT_CN_INVALID | INTERNET_FLAG_IGNORE_CERT_DATE_INVALID |
                INTERNET_FLAG_IGNORE_REDIRECT_TO_HTTP | INTERNET_FLAG_IGNORE_REDIRECT_TO_HTTPS | INTERNET_FLAG_KEEP_CONNECTION |
                INTERNET_FLAG_NO_AUTH | INTERNET_FLAG_NO_UI | INTERNET_FLAG_PRAGMA_NOCACHE, 0);
            if(NULL != hRequest)
            {
                const char Params[] = {"u=1234&t=5678"};

                BOOL rc = HttpSendRequest(hRequest, NULL, 0, (void*)Params, strlen(Params));

                if(rc) // here rc = 0 and GetLastError() returns 12152
                {
                    DWORD availDataLen;
                    char Buff[4096];
                    DWORD readCount = ERROR_INTERNET_CONNECTION_RESET;

                    InternetQueryDataAvailable(hInternet, &availDataLen, 0, 0);
                    while(0 < availDataLen)
                    {
                        InternetReadFile(hInternet, Buff, min(sizeof(Buff), availDataLen), &readCount);
                        availDataLen -= readCount;
                        retVal = retVal + Buff;
                    }
                }
                InternetCloseHandle(hRequest);
            }
            InternetCloseHandle(hConnection);
        }
        InternetCloseHandle(hInternet);
    }
    return retVal;
}

but as you can see in the code HttpSendRequest() fails and GetLastError() returns 12152

Thank you in advance Sam

That's ERROR_WINHTTP_INVALID_SERVER_RESPONSE. Best guess you're making an HTTP request on an HTTPS port.

I don't know the API very well but it looks like you need to add INTERNET_FLAG_SECURE to your HttpOpenRequest call, and / or in the flags parameter of your InternetConnect call too:

INTERNET_FLAG_SECURE
Uses secure transaction semantics. This translates to using Secure Sockets Layer/Private Communications Technology (SSL/PCT) and is only meaningful in HTTP requests. This flag is used by HttpOpenRequest and InternetOpenUrl, but this is redundant if https:// appears in the URL.The InternetConnect function uses this flag for HTTP connections; all the request handles created under this connection will inherit this flag.

INTERNET_FLAG_SECURE添加到HttpOpenRequest对我来说很有效。

HINTERNET hHttpRequest = HttpOpenRequest(hHttpSession, lpszVerb, lpszObjectName, 0, 0, 0, INTERNET_FLAG_RELOAD | INTERNET_FLAG_SECURE, 0);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM