抓取cookie不对,求分析原因

公西雒 2014-03-28 03:02:50
详细情况请见我在PHP版发的帖子http://bbs.csdn.net/topics/390743945
下面简单描述下我的需求:通过客户端登录本地服务器搭建的网站。
但是获取cookie时,获取到的sessionid不对啊,跟浏览器直接登录时的产生的session不一样,那个session是固定的id,而我获取的总是在变。
网上找的代码,不知道问题究竟出在哪?望大神们分析分析!
            HttpHeader header = new HttpHeader();
header.accept = "text/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 8.0; Windows NT 6.1; WOW64; Trident/4.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; InfoPath.3; .NET4.0C; .NET4.0E";
header.maxTry = 200;

CookieCollection mycookie = HTMLHelper.GetCookieCollection("http://localhost/gyjw/index.php/index/login_into",
"admin=admin&password=***", header);

foreach (Cookie cookie in mycookie) //将cookie设置为浏览的cookie
{
InternetSetCookie(

"http://" + cookie.Domain.ToString(),

cookie.Name.ToString(),

cookie.Value.ToString() + ";expires=Sun,22-Feb-2099 00:00:00 GMT");

}
//System.Diagnostics.Process.Start("http://localhost/gyjw/index.php/index/Login.html");
//System.Diagnostics.Process.Start("http://localhost/gyjw/index.php/index/login_into");
//System.Diagnostics.Process.Start("http://localhost/gyjw/index.php");
System.Diagnostics.Process.Start("http://localhost/gyjw/index.php/user/AddUser.html");

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.IO;
using System.Threading;

namespace WpfUI
{
public class HTMLHelper
{
/// <summary>
/// 获取CooKie
/// </summary>
/// <param name="loginUrl"></param>
/// <param name="postdata"></param>
/// <param name="header"></param>
/// <returns></returns>
public static CookieContainer GetCooKie(string loginUrl, string postdata, HttpHeader header)
{
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 cc;
}
catch (Exception ex)
{

throw ex;
}
}

/// <summary>
/// 获取CookieCollection
/// </summary>
/// <param name="loginUrl"></param>
/// <param name="postdata"></param>
/// <param name="header"></param>
/// <returns></returns>
public static CookieCollection GetCookieCollection(string loginUrl, string postdata, HttpHeader header)
{
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);
//byte[] postdatabyte = Encoding.ASCII.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();//这里查看PHPSESSID就不一样了。
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;
}
}

/// <summary>
/// 获取html
/// </summary>
/// <param name="getUrl"></param>
/// <param name="cookieContainer"></param>
/// <param name="header"></param>
/// <returns></returns>
public static string GetHtml(string getUrl, CookieContainer cookieContainer,HttpHeader header)
{
Thread.Sleep(1000);
HttpWebRequest httpWebRequest = null;
HttpWebResponse httpWebResponse = null;
try
{
httpWebRequest = (HttpWebRequest)HttpWebRequest.Create(getUrl);
httpWebRequest.CookieContainer = cookieContainer;
httpWebRequest.ContentType = header.contentType;
httpWebRequest.ServicePoint.ConnectionLimit = header.maxTry;
httpWebRequest.Referer = getUrl;
httpWebRequest.Accept = header.accept;
httpWebRequest.UserAgent = header.userAgent;
httpWebRequest.Method = "GET";
httpWebResponse = (HttpWebResponse)httpWebRequest.GetResponse();
Stream responseStream = httpWebResponse.GetResponseStream();
StreamReader streamReader = new StreamReader(responseStream, Encoding.UTF8);
string html = streamReader.ReadToEnd();
streamReader.Close();
responseStream.Close();
httpWebRequest.Abort();
httpWebResponse.Close();
return html;
}
catch (Exception e)
{
if (httpWebRequest != null) httpWebRequest.Abort();
if (httpWebResponse != null) httpWebResponse.Close();
return string.Empty;
}
}
}

public class HttpHeader
{
public string contentType { get; set; }

public string accept { get; set; }

public string userAgent { get; set; }

public string method { get; set; }

public int maxTry { get; set; }
}
}
...全文
303 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
公西雒 2014-03-31
  • 打赏
  • 举报
回复
引用 3 楼 WM_JAWIN 的回复:
httpWebRequest.CookieContainer = cookieContainer; 这东西,每次请求都一是同一个对像才行。另外,这东东,对某些cookie存在bug
我只执行了一次啊,既然有BUG,那我应该用什么才能获取到正确的cookie呢?
WM_JAWIN 2014-03-31
  • 打赏
  • 举报
回复
httpWebRequest.CookieContainer = cookieContainer; 这东西,每次请求都一是同一个对像才行。另外,这东东,对某些cookie存在bug
公西雒 2014-03-31
  • 打赏
  • 举报
回复
求大神解答啊
公西雒 2014-03-31
  • 打赏
  • 举报
回复
别沉啊 问题还没解决啊

110,533

社区成员

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

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

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