關於CURL獲取網頁,返回的網頁內容大小一直變化的原因

最近在維護一個檢測網頁篡改檢測的程序代碼,代碼會判斷網頁內容的大小,如果大小發生很大的變化就會報警,程序是通過CURL來獲取網頁大小的,程序運行在windows7下,使用的是VS2008

下面貼的代碼是獲取網頁信息的函數:

int NetHelper::GetWebPageInfo( CString strUrl, bool bIsWap, bool bJustHeader, int& nFileSize, string& strContent )
{
    strContent.clear();
    nFileSize =0;

    init_curl initcurl;
    CURL * curl = initcurl.get_curl();
    CURLcode curlres;

    //libcurl初始化失敗
    if(!curl)
    {
        strContent = "curl初始化失敗!";
        return -1;
    }

    // 在堆棧上分配內存供 W2A等函數使用
    USES_CONVERSION;

    // 設置URL
    curl_easy_setopt(curl, CURLOPT_URL, W2A(strUrl));

    // 如果是ssl網站,不對ssl網站的證書進行驗證
    curl_easy_setopt(curl,CURLOPT_SSL_VERIFYPEER,0);
    curl_easy_setopt(curl,CURLOPT_SSL_VERIFYHOST,0);

    curl_easy_setopt(curl,CURLOPT_COOKIEJAR,"e:\\cookie.txt");
    curl_easy_setopt(curl,CURLOPT_COOKIEFILE,"e:\\cookie.txt");

    // 超時設置
    curl_easy_setopt(curl,CURLOPT_TIMEOUT,10);

    if ( !bJustHeader )
    {
        // 設置讀取HTTP返回內容的回調函數
        curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_callback);
        // 設置回調函數的參數,見write_callback函數的最後一個參數
        curl_easy_setopt(curl, CURLOPT_WRITEDATA, &strContent);
    }//end if  
    else
    {
        // 設置讀取HTTP頭部內容的回調函數
        curl_easy_setopt(curl,CURLOPT_HEADERFUNCTION,header_callback);
        // 設置回調函數的參數,見header_callback函數的最後一個參數, 如果沒有用到,
        curl_easy_setopt(curl, CURLOPT_HEADERDATA, &strContent);
        curl_easy_setopt( curl, CURLOPT_NOBODY, 1 );
    }


    // 自動轉向
    curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1);

    // 設置頭
    curl_slist *chunk = NULL;
    chunk = curl_slist_append( chunk, "Accept: text/html, application/xhtml+xml, */*" ); 
    chunk = curl_slist_append( chunk, "Accept-Language: zh-CN" ); 
    if ( bIsWap )
    {
        chunk = curl_slist_append( chunk, "User-Agent: Mozilla/5.0 (Linux; U; Android 2.3.4; zh-cn; GT-I9100 Build/GINGERBREAD) AppleWebKit/533.1 (KHTML, like Gecko) Version/4.0 Mobile Safari/533.1" ); 
    } //end if
    else
    {
        chunk = curl_slist_append( chunk, "User-Agent: Mozilla/5.0 (Windows NT 6.1) AppleWebKit/535.7 (KHTML, like Gecko)" ); 
    }
    chunk = curl_slist_append( chunk, "Accept-Encoding: gzip, deflate" ); 
    chunk = curl_slist_append( chunk, "Connection: Keep-Alive" );
    curl_easy_setopt( curl, CURLOPT_HTTPHEADER, chunk );

    strContent.clear();
    curlres = curl_easy_perform(curl);

    long nStatus;

    if( curlres == CURLE_OK )
    {
        //服務器返回碼
        curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &nStatus);
        nFileSize = strContent.size();
    }
    else
    {
        nStatus = curlres;
    }

    return nStatus;
}

情況是這樣的,當我調試這個程序,檢測網站,請求網頁的時候,我用httpdebug抓包,獲取的網頁的Body大小是96354,我查看了內容,裏面是完整的網頁,這說明程序設置的url請求是沒有錯誤的,每次都可以返回正確的網頁,狀態碼Status是200.
這裏寫圖片描述

然後我下斷點查看nFileSize(表示網頁內容大小的變量)情況是這樣的:
這裏寫圖片描述
這裏寫圖片描述
這裏寫圖片描述
大小是變化的,只有98666可以看到是完整的網頁內容,其它都是“?”。

最後找出來的原因是:網頁是被壓縮了,把下圖中的語句註釋掉,服務器就不會返回壓縮過的網頁。這樣每次獲取的網頁內容都是正常的了,大小都是98666。
這裏寫圖片描述

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章