简体   繁体   中英

C++ wininet, connect to weblogin, how to set cookies?

Hey all i want to login onto my works webpage with wininet, this is my current code:

int main()
{
    HINTERNET hInet = InternetOpenA("UserAgent/1.0", INTERNET_OPEN_TYPE_PRECONFIG,0, 0, 0 );
    if(!hInet)
    {
        printf("hInet Failed!\n");
        return -1;
    }

    HINTERNET hConnection = InternetConnectA( hInet,"app.tamigo.com",INTERNET_DEFAULT_HTTPS_PORT,"","", INTERNET_SERVICE_HTTP,0,0);
    if (!hConnection)
    {
        InternetCloseHandle(hInet);
        printf("InternetConnectA failed!\n");
        return -1;
    }

    HINTERNET hRequest = HttpOpenRequestA( hConnection, "Accept","text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8",NULL,"https://app.tamigo.com/Home/Pages/Login.aspx", NULL, INTERNET_FLAG_KEEP_CONNECTION, 0 );
    if (!hRequest)
    {
        printf("BuildRequestHeader failed %d!\n",GetLastError());
        InternetCloseHandle(hConnection);
        InternetCloseHandle(hInet);
        return -1;
    }

    HttpSendRequestA(hRequest, NULL, 0, NULL, 0);

    DWORD dwInfoLevel = HTTP_QUERY_RAW_HEADERS_CRLF;
    DWORD dwInfoBufferLength = 10;
    BYTE *pInfoBuffer = (BYTE *)malloc(dwInfoBufferLength+1);
    while (!HttpQueryInfo(hRequest, dwInfoLevel, pInfoBuffer, &dwInfoBufferLength, NULL))
    {
        DWORD dwError = GetLastError();
        if (dwError == ERROR_INSUFFICIENT_BUFFER)
        {
            free(pInfoBuffer);
            pInfoBuffer = (BYTE *)malloc(dwInfoBufferLength+1);
        }
        else
        {
            fprintf(stderr, "HttpQueryInfo failed, error = %d (0x%x)\n",
            GetLastError(), GetLastError());
            break;
        }
    }
    pInfoBuffer[dwInfoBufferLength] = '\0';
    printf("%s", pInfoBuffer);
    free(pInfoBuffer);

    cin.get();
    return 1;
}

if this code is right, i have to login with my username and pass,i got a cookie using "Firefox plugin Tamper Data". How can i set this cookie with wininet?

Thanks alot for reading and for your time

If the cookie already exists from a previous WinInet request, then WinInet will send it automatically. However, if the cookie does not exist in WinInet's cookie cache (if instance, if you got the cookie from another source), then you will have to use HttpAddRequestHeaders() to provide your own Cookie: request header before calling HttpSendRequest() .

Just to add to Remy Lebeau's Answer (since I don't have enough reputation to comment).

HttpAddRequestHeaders will ignore you request to set "Cookie" header, if you do not specify INTERNET_FLAG_NO_COOKIES flag to HttpOpenRequest. Please note that this disabled wininet's auto-cookie management, so you will need to take care of all cookie management.

Here is more information (and another way to do this): https://groups.google.com/forum/#!topic/microsoft.public.inetsdk.programming.wininet/0O1hkyPCF-I

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