过年问个Idhttp获取cookie问题

Bosman 2011-02-03 12:15:38
c++ builder 2009
测试网址:http://www.a1a999.com
用IE测试,HttpWatch监控,输入帐号并登录,login.php返回302重定向,可以监视到IE获取到其中一个PHPSESSID的cookie.
如下:

00:00:00.000 0.183 497 772 GET 200 text/html http://www.a1a999.com/
00:00:00.223 0.186 610 2438 GET 200 text/html http://www.a1a999.com/home.php
00:00:01.171 0.200 410 2315 GET 200 image/jpeg http://www.a1a999.com/images/button_bg.jpg
00:00:24.200 0.205 766 623 POST 302 Redirect to http://www.a1a999.com/check_rule.php?PHPSESSID=&password_modify_date=2011-01-31%2011:20:52 http://www.a1a999.com/login.php
00:00:24.407 0.383 741 3057 GET 200 text/html; charset=gb2312 http://www.a1a999.com/check_rule.php?PHPSESSID=&password_modify_date=2011-01-31%2011:20:52
00:00:24.842 1.643 508 35969 GET 200 image/jpeg http://www.a1a999.com/images/banner.wg.jpg
00:00:24.845 0.390 512 2317 GET 200 image/jpeg http://www.a1a999.com/images/button_bg_red.jpg
00:00:24.872 0.364 514 2316 GET 200 image/jpeg http://www.a1a999.com/images/button_bg_green.jpg
//////////
在 00:00:00.000这一步,IE首先获取到一个Cookie.
NSC_63.217.93.116 Received ffffffff091a183545525d5f4f58455e445a4a423660 / www.a1a999.com (Session)

然后在 00:00:24.200这一步,即输入帐号密码登录后,login返回302重定向后IE再次获取PHPSESSID这个Cookie
NSC_63.217.93.116 Sent ffffffff091a183545525d5f4f58455e445a4a423660 / www.a1a999.com (Session)
NSC_63.217.93.116 Received ffffffff091a183545525d5f4f58455e445a4a423660 / www.a1a999.com (Session)
PHPSESSID Received 0vib8n0n4vi9i0m6t0201q45e5 / www.a1a999.com (Session)


问题是:
如果用idhttp去登录,却获取不到这个PHPSESSID的cookie.
测试如下:

RequestData->Clear();
try
{
IdHTTP1->Get(WebAddress,RequestData);
}
catch(...)
{
MemoInfo->Lines->Add("连接错误");
return;
}
RequestData->SaveToFile("login0.txt");
MemoInfo->Lines->Add("请求头:"+IdHTTP1->Request->RawHeaders->Text);
MemoInfo->Lines->Add("响应头:"+IdHTTP1->Response->RawHeaders->Text);
MemoInfo->Lines->Add("Location:"+IdHTTP1->Response->Location);
RequestData->Clear();
try
{
IdHTTP1->Get(WebAddress+"/home.php",RequestData);
}
catch(...)
{
MemoInfo->Lines->Add("连接错误");
return;
}
RequestData->SaveToFile("login1.txt");
MemoInfo->Lines->Add("请求头:"+IdHTTP1->Request->RawHeaders->Text);
MemoInfo->Lines->Add("响应头:"+IdHTTP1->Response->RawHeaders->Text);
MemoInfo->Lines->Add("Location:"+IdHTTP1->Response->Location);
char *ss=strstr(RequestData->DataString.c_str(),"login.php");
if (!ss) {
MemoInfo->Lines->Add("连接错误2");
return;
}

PostParam->Clear();
PostParam->Add("login=登入");
PostParam->Add("username="+UserName);
PostParam->Add("password="+Password);
RequestData->Clear();
try
{
IdHTTP1->Post(WebAddress+"/login.php",PostParam,RequestData);
}
catch(...)
{
MemoInfo->Lines->Add("连接错误3");
return;
}
RequestData->SaveToFile("login2.txt");
MemoInfo->Lines->Add("请求头:"+IdHTTP1->Request->RawHeaders->Text);
MemoInfo->Lines->Add("响应头:"+IdHTTP1->Response->RawHeaders->Text);
MemoInfo->Lines->Add("Location:"+IdHTTP1->Response->Location);

监控结果如下:
请求头:Host: www.a1a999.com
Accept: text/html, */*
Accept-Encoding: deflate, gzip, identity
User-Agent: Mozilla/3.0 (compatible; Indy Library)

响应头:Date: Wed, 02 Feb 2011 15:57:03 GMT
Server: Apache
Last-Modified: Fri, 02 Jul 2010 08:50:34 GMT
ETag: "45c904-18d-48a63ae174a80"
Accept-Ranges: bytes
Content-Length: 397
Content-Type: text/html
Set-Cookie: NSC_63.217.93.116=ffffffff091a183545525d5f4f58455e445a4a423660;Version=1;Max-Age=2100;path=/

Location:
请求头:Host: www.a1a999.com
Accept: text/html, */*
Accept-Encoding: deflate, gzip, identity
User-Agent: Mozilla/3.0 (compatible; Indy Library)
Cookie: NSC_63.217.93.116=ffffffff091a183545525d5f4f58455e445a4a423660

响应头:Date: Wed, 02 Feb 2011 15:57:04 GMT
Server: Apache
Content-Length: 2164
Content-Type: text/html
Set-Cookie: NSC_63.217.93.116=ffffffff091a183545525d5f4f58455e445a4a423660;Version=1;Max-Age=2100;path=/

Location:
请求头:Connection: keep-alive
Content-Type: application/x-www-form-urlencoded
Content-Length: 58
Host: www.a1a999.com
Accept: text/html, */*
Accept-Encoding: deflate, gzip, identity
User-Agent: Mozilla/3.0 (compatible; Indy Library)
Cookie: NSC_63.217.93.116=ffffffff091a183545525d5f4f58455e445a4a423660

响应头:Date: Wed, 02 Feb 2011 15:57:07 GMT
Server: Apache
Content-Length: 0
Keep-Alive: timeout=300
Connection: Keep-Alive
Content-Type: text/html
Set-Cookie: NSC_63.217.93.116=ffffffff091a183545525d5f4f58455e445a4a423660;Version=1;Max-Age=2100;path=/

Location:

即idhttp既无重定向响应,也无法获取到PHPSESSID的cookie.idhttp设置已正常,AllowCookie=true,允许重定向.

求解..
...全文
266 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
Bosman 2011-02-04
  • 打赏
  • 举报
回复
老妖你太不厚道啦。

经研究已解决。
只要是服务器端设置了检验请求头的Referrer来源网址问题。。
hemiya 2011-02-03
  • 打赏
  • 举报
回复
楼主很好学
CopyReg 2011-02-03
  • 打赏
  • 举报
回复
要设置 禁止重定向 才能从响应头中得到:Location
Bosman 2011-02-03
  • 打赏
  • 举报
回复
学无止境......
ccrun.com 2011-02-03
  • 打赏
  • 举报
回复
楼主真牛,太敬业了。

552

社区成员

发帖
与我相关
我的任务
社区描述
C++ Builder 茶馆
社区管理员
  • 茶馆
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧