我在模拟登陆的时候 获取不到cookie

bgaidu 2012-05-14 04:12:17
我想模拟下人人自动登陆 然后再打开新的链接,我使用一下代码 获取cookie 但得到的是"";没有获取到任何的值,很是郁闷 请高手指点。我断点后发现 mycookie得到空值,


Test.HttpHeader header = new Test.HttpHeader();
header.accept = "itext/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8";
header.contentType = "application/x-www-form-urlencoded";
header.method = "POST";
header.userAgent = "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; .NET CLR 2.0.50727; .NET CLR 3.0.04506.648; .NET CLR 3.5.21022)";
header.maxTry = 300;

CookieCollection mycookie = Test.HTMLHelper.GetCookieCollection("http://www.renren.com/PLogin.do",
// "mail=bgaidu%40163.com&password=123456789&origURL=https%3A%2F%2Fsafe.renren.com%2F&domain=renren.com&formName=&method=&isplogin=true&submit=%E7%99%BB%E5%BD%95", header);




然后我又断点 getcookiecollection 方法的代码如下,得到的也为空值。



  


HttpWebRequest request = null;
HttpWebResponse response = null;
try
{
CookieContainer cc = new CookieContainer();
request = (HttpWebRequest)WebRequest.Create(loginUrl);
request.Method = header.method;
request.ContentType = header.contentType;
byte[] postdatabyte = Encoding.UTF8.GetBytes(postdata);
request.ContentLength = postdatabyte.Length;
request.AllowAutoRedirect = false;
request.CookieContainer = cc;
request.KeepAlive = true;

//提交请求
Stream stream;
stream = request.GetRequestStream();
stream.Write(postdatabyte, 0, postdatabyte.Length);
stream.Close();

//接收响应
response = (HttpWebResponse)request.GetResponse();
response.Cookies = request.CookieContainer.GetCookies(request.RequestUri);

CookieCollection cook = response.Cookies;
//Cookie字符串格式
string strcrook = request.CookieContainer.GetCookieHeader(request.RequestUri);

return cook;
}
catch (Exception ex)
{

throw ex;
}



求高手指点!
...全文
419 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
sunmenghongm 2012-08-13
  • 打赏
  • 举报
回复
楼主解决了么。?我也遇到相似的问题?
test2050 2012-05-15
  • 打赏
  • 举报
回复
那到底Plogin.do有没有cookie呢
bgaidu 2012-05-15
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 的回复:]

试了一下 访问PLogin.do没有产生cookie
页面将跳转到SysHome.do 此时将会有cookie
建议使用一些抓包工具协作分析。。。
[/Quote]
我用httpwatch分析的时候 Plogin.do 有cookie生成
qlzf11140820 2012-05-14
  • 打赏
  • 举报
回复
试了一下 访问PLogin.do没有产生cookie
页面将跳转到SysHome.do 此时将会有cookie
建议使用一些抓包工具协作分析。。。
qlzf11140820 2012-05-14
  • 打赏
  • 举报
回复
可能根本没有cookie...

110,560

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

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