天天看點

WinHTTP開發POST表單的問題及解決

轉自:http://blog.csdn.net/blog51/article/details/4275632

利用WinHTTP編寫通路WEB頁面的用戶端,如果需要POST表單,比如輸入登陸的使用者名和密碼等等,以後的頁面通路都和這個登陸的Session有關。IE浏覽器等用戶端利用從伺服器端獲得的Cookie儲存相關資訊實作會話的一緻性,具體到WinHTTP開發,查了一下,網上沒有太多相關資料,下面函數利用Get掩飾向一個網站擷取Cookie的ID号,儲存到CString中,以後的Get等操作,都利用WinHttpAddRequestHeaders将Cookie添加到Header中,然後在進行Request操作即可:

CStringW GetCookie()

{

    CStringW strCookie = L"";

    DWORD dwSize = 0;

    LPVOID lpOutBuffer = NULL;

    BOOL  bResults = FALSE;

    HINTERNET  hSession = NULL, 

        hConnect = NULL,

        hRequest = NULL;

    // Use WinHttpOpen to obtain a session handle.

    hSession = WinHttpOpen( SZ_AGENT,  

        WINHTTP_ACCESS_TYPE_DEFAULT_PROXY,

        WINHTTP_NO_PROXY_NAME, 

        WINHTTP_NO_PROXY_BYPASS, 0 );

    // Specify an HTTP server.

    if( hSession )

        hConnect = WinHttpConnect( hSession, L"wapmail.webdunia.com",

        INTERNET_DEFAULT_HTTP_PORT, 0 );

    // Create an HTTP request handle.

    if( hConnect )

        hRequest = WinHttpOpenRequest( hConnect, L"GET", NULL,

        NULL, WINHTTP_NO_REFERER, 

        WINHTTP_DEFAULT_ACCEPT_TYPES, 

        0 );

    // Send a request.

    if( hRequest )

        bResults = WinHttpSendRequest( hRequest,

        WINHTTP_NO_ADDITIONAL_HEADERS, 0,

        WINHTTP_NO_REQUEST_DATA, 0, 

        0, 0 );

    // End the request.

    if (bResults)

        bResults = WinHttpReceiveResponse( hRequest, NULL);

    // First, use WinHttpQueryHeaders to obtain the size of the buffer.

    if (bResults)

    {

        WinHttpQueryHeaders( hRequest, WINHTTP_QUERY_RAW_HEADERS_CRLF,

            WINHTTP_HEADER_NAME_BY_INDEX, NULL, 

            &dwSize, WINHTTP_NO_HEADER_INDEX);

        // Allocate memory for the buffer.

        if( GetLastError( ) == ERROR_INSUFFICIENT_BUFFER )

        {

            lpOutBuffer = new WCHAR[dwSize/sizeof(WCHAR)];

            // Now, use WinHttpQueryHeaders to retrieve the header.

            bResults = WinHttpQueryHeaders( hRequest, 

                WINHTTP_QUERY_RAW_HEADERS_CRLF,

                WINHTTP_HEADER_NAME_BY_INDEX, 

                lpOutBuffer, &dwSize, 

                WINHTTP_NO_HEADER_INDEX);

        }

    }

    // Print the header contents.

    char * tmpch = (char *)malloc(sizeof(lpOutBuffer));

    if (bResults)

        sprintf(tmpch,"%S",lpOutBuffer);

    strCookie = tmpch;

    int set_cookie = strCookie.Find(L"Cookie",0);

    int semicolon = strCookie.Find (L";",set_cookie);

    if(set_cookie != -1 && semicolon != -1)

        strCookie = strCookie.Mid (set_cookie,semicolon - set_cookie) + L"/r/n";

    // Free the allocated memory.

    delete [] lpOutBuffer;

    // 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 strCookie;

}