我想模擬發送Get請求,獲取服務器傳回的Set-Cookie值,可是獲取到的Set-Cookie不完全。
下面是我用火狐的FireDebug抓到的數據:
URL是:
http://my.37.com/api/login.php?callback=jQuery18301289508974426815_1425523200459&action=login&login_account=dazhewang&password=111111&ajax=0&remember_me=1&save_state=1<ype=1&_=1425523232168
請求頭是:
GET /api/login.php?callback=jQuery18301289508974426815_1425523200459&action=login&login_account=dazhewang&password=111111&ajax=0&remember_me=1&save_state=1<ype=1&_=1425523232168 HTTP/1.1
Host: my.37.com
User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:36.0) Gecko/20100101 Firefox/36.0
Accept: */*
Accept-Language: zh-CN,zh;q=0.8,en-US;q=0.5,en;q=0.3
Accept-Encoding: gzip, deflate
Referer: http://www.37.com/
Connection: keep-alive
響應頭信息是:
HTTP/1.1 200 OK
Server: nginx
Date: Thu, 05 Mar 2015 02:41:55 GMT
Content-Type: text/html; Charset=UTF-8
Transfer-Encoding: chunked
Connection: keep-alive
P3P: CP="CURa ADMa DEVa PSAo PSDo OUR BUS UNI PUR INT DEM STA PRE COM NAV OTC NOI DSP COR"
Set-Cookie: PHPSESSID=16414db50d1ca1961594b4b599f4926a; path=/
rt=1; path=/; domain=37.com
passport_37wan_com=204830022%7Cdazhewang%7C1425523315000%7C2cb762f4dcd29cb0df6b1f982568bb8f; expires=Sat, 04-Apr-2015 02:41:55 GMT; path=/; domain=37.com
ispass_37wan_com=89235a5c%7C1%7C3910fa2c75703ed865fba6c86c2fd4db%7C1; expires=Sat, 04-Apr-2015 02:41:55 GMT; path=/; domain=37.com; httponly
37wan_account=dazhewang; expires=Sun, 02-Mar-2025 02:41:55 GMT; path=/; domain=37.com
37loginrefer=LHd3dy4zNy5jb20sLA%3D%3D%7Cc2470b07de2d81a9e1581defa000a0bb; path=/; domain=37.com
Expires: Thu, 19 Nov 1981 08:52:00 GMT
Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0
Pragma: no-cache
37web: zs_12_33_web
Content-Encoding: gzip
下面是我的代碼:
CInternetSession session;
INTERNET_PORT nPort = 80;
try {
CHttpConnection* pHttpConnect = session.GetHttpConnection(_T("my.37.com"), nPort);
if (pHttpConnect) {
CHttpFile* pFile = pHttpConnect->OpenRequest(CHttpConnection::HTTP_VERB_GET, _T("/api/login.php ?")); //"/api/login.php ?"的問號應該是不要的吧,我嘗試去掉?後,代碼結果不變
if (pFile) {
pFile->AddRequestHeaders(L"GET /api/login.php ? callback = jQuery18301289508974426815_1425523200459&action = login&login_account = dazhewang&password = 111111 & ajax = 0 & remember_me = 1 & save_state = 1 & ltype = 1 & _ = 1425523232168 HTTP / 1.1"); //這裡的參數信息是否是這麼添加的?
pFile->AddRequestHeaders(_T("Accept : */*"));
pFile->AddRequestHeaders(_T("Accept-Language: zh-CN,zh;q=0.8,en-US;q=0.5,en;q=0.3"));
pFile->AddRequestHeaders(_T("Referer: http://www.37.com/"));
pFile->AddRequestHeaders(_T("User-Agent: Mozilla / 5.0 (Windows NT 6.1; WOW64; rv:36.0) Gecko / 20100101 Firefox / 36.0"));
pFile->AddRequestHeaders(_T("Accept-Encoding: gzip, deflate"));
pFile->AddRequestHeaders(_T("Host: my.37.com"));
pFile->AddRequestHeaders(_T("Connection: keep-alive"));
pFile->AddRequestHeaders(_T("Cache - Control: no - cache"));
//TCHAR szRequest
//pFile->SendRequest(NULL, 0, szRequest, MAX_COUNT*sizeof(TCHAR));
pFile->SendRequest(); //這句代碼需要加參數嗎?我這裡沒有要額外添加的信息了
CString strStatus(_T(""));
if (pFile->QueryInfo(HTTP_QUERY_RAW_HEADERS_CRLF, strStatus)) {
AfxMessageBox(strStatus);
}
}
pFile->Close();
delete pFile;
pFile = NULL;
}
return TRUE;
}
catch (CInternetException* e)
{
e->ReportError();
e->Delete();
return FALSE;
}
返回的響應結果如下:
HTTP/1.1 200 OK
Server: nginx
Date: Thu, 05 Mar 2015 02:52:49 GMT
Content-Type: text/html; Charset=UTF-8
Transfer-Encoding: chunked
Connection: keep-alive
P3P: CP="CURa ADMa DEVa PSAo PSDo OUR BUS UNI PUR INT DEM STA PRE COM NAV OTC NOI DSP COR"
Set-Cookie: PHPSESSID=77363a9a9ab0c6b7bf5c96c80d635e2f; path=/
Expires: Thu, 19 Nov 1981 08:52:00 GMT
Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0
Pragma: no-cache
37web: gz_82_133_web
Content-Encoding: gzip
Set-Cookie值只1條,請問大神們這是什麼原因啊?求解答,萬分感謝!!!(由於是新賬號,沒懸賞分,非常抱歉)
把你代碼發的包也抓包,然後跟l浏覽器的對比。看缺少什麼數據。