编程-菜鸟
2015-03-05 03:11
采纳率: 50%
浏览 4.1k
已采纳

MFC 发送HTTP请求获取Set-Cookie不完全

我想模拟发送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&ltype=1&_=1425523232168

请求头是:

 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
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条,请问大神们这是什么原因啊?求解答,万分感谢!!!(由于是新账号,没悬赏分,非常抱歉)

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

4条回答 默认 最新

  • oyljerry 2015-03-05 15:12
    已采纳

    把你代码发的包也抓包,然后跟l浏览器的对比。看缺少什么数据。

    点赞 打赏 评论
  • devmiao 2015-03-05 03:29

    用fiddler仔细调试下,看看是不是编码的问题

    点赞 打赏 评论
  • 编程-菜鸟 2015-03-05 03:54

    谢谢回答啊,你的意思是说有可能没有用utf-8编码吗?我试了试,多加了条

     pFile->AddRequestHeaders(_T("Content-Type: text/html; Charset=UTF-8"));
    

    结果也一样,是不是我加错地方了?

    点赞 打赏 评论
  • 江湖中已没了哥2 2015-05-24 04:12

    同样出现跟你一样的问题,暂时还没有得到解决 你QQ多少?我的是86612150 一起交流下

    我觉得不是头部的问题,因为我的代码和你差不多,我获取的到cookie也是不完整的,这个好像是个bug,不知道怎么搞

    点赞 打赏 评论

相关推荐 更多相似问题