方法: public static string GetCookie(string URL, string postData, string cookie, out string newCookie)
{
byte[] byteRequest = Encoding.Default.GetBytes(postData);
long contentLength;
HttpWebRequest httpWebRequest;
HttpWebResponse webResponse;
Stream getStream;
httpWebRequest = (HttpWebRequest)HttpWebRequest.Create(URL);
CookieContainer co = new CookieContainer();
co.SetCookies(new Uri(URL), cookie);
httpWebRequest.CookieContainer = co;
httpWebRequest.ContentType = "application/x-www-form-urlencoded";
httpWebRequest.Referer = "http://test.com";
httpWebRequest.UserAgent =
"Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; Maxthon; .NET CLR 1.1.4322)";
httpWebRequest.Method = "Post";
httpWebRequest.ContentLength = byteRequest.Length;
httpWebRequest.AllowAutoRedirect = false;
Stream stream;
stream = httpWebRequest.GetRequestStream();
stream.Write(byteRequest, 0, byteRequest.Length);
webResponse = (HttpWebResponse)httpWebRequest.GetResponse();
CookieContainer cookies = httpWebRequest.CookieContainer;
newCookie = httpWebRequest.Headers.Get("Set-Cookie");
stream.Close();
return newCookie;
}
//调用
string newCookie="";
newCookie=GetCookie("http://test.com/登录验测.aspx",post提交过来的数据,验证码cookie,输出新的cookie);
取出newCookie怎么为空呢?我同时试了:
httpWebRequest.CookieContainer.GetCookieHeader(new Uri("http://test.com/logindo.aspx")).ToString();
这个能取得到,但是发现和使用的抓包工具http://test.com/logindo.aspx的cookie:Set-Cookie: .ASPXFORMSAUTHXXForTest=7CCB87024AB3A9DA3D90F46FFCDA3F0D4F3BD3657D62997F8AA2141B943D243BE69E54B34E855F2012BDFF8B24F0BCFAA91D3CEDA4593943F6658B09E35D156699E1F24DB045A10DB272C1922EC3019842C2603BF988908F4E36CC9B000D71D8; domain=.test.com; path=/; HttpOnly
Set-Cookie: forever.test.com=UserID=PBjdvkHaNTQ=&Time=zULFFSqYKXe668wECCO0nO5Mvo9jV6qI&Status=KAyeeDyZo6Y=; domain=www.test.com; expires=Tue, 05-Jun-2018 12:32:22 GMT; path=/
httpWebRequest.CookieContainer.GetCookieHeader这方法取到的是:ASP.NET_SessionId=pfxkjfmnnx5aut3wq0af0f45; .ASPXFORMSAUTHXXForTest=C8EAA98C6B26D3442D1E7B2E7FFD1218B9526A69A3075E559A90BEE4927E2BE0A2BF77DD576FD7BBDFA14FEC4864076B3943C7837FE872C31571D908E3A3B1FA2B939226311350A8451205B51414731AF309902868E54B9B9083A961F84C6C5B
帮帮忙呀,高手们
{
byte[] byteRequest = Encoding.Default.GetBytes(postData);
long contentLength;
HttpWebRequest httpWebRequest;
HttpWebResponse webResponse;
Stream getStream;
httpWebRequest = (HttpWebRequest)HttpWebRequest.Create(URL);
CookieContainer co = new CookieContainer();
co.SetCookies(new Uri(URL), cookie);
httpWebRequest.CookieContainer = co;
httpWebRequest.ContentType = "application/x-www-form-urlencoded";
httpWebRequest.Referer = "http://test.com";
httpWebRequest.UserAgent =
"Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; Maxthon; .NET CLR 1.1.4322)";
httpWebRequest.Method = "Post";
httpWebRequest.ContentLength = byteRequest.Length;
httpWebRequest.AllowAutoRedirect = false;
Stream stream;
stream = httpWebRequest.GetRequestStream();
stream.Write(byteRequest, 0, byteRequest.Length);
webResponse = (HttpWebResponse)httpWebRequest.GetResponse();
CookieContainer cookies = httpWebRequest.CookieContainer;
newCookie = httpWebRequest.Headers.Get("Set-Cookie");
stream.Close();
return newCookie;
}
//调用
string newCookie="";
newCookie=GetCookie("http://test.com/登录验测.aspx",post提交过来的数据,验证码cookie,输出新的cookie);
取出newCookie怎么为空呢?我同时试了:
httpWebRequest.CookieContainer.GetCookieHeader(new Uri("http://test.com/logindo.aspx")).ToString();
这个能取得到,但是发现和使用的抓包工具http://test.com/logindo.aspx的cookie:Set-Cookie: .ASPXFORMSAUTHXXForTest=7CCB87024AB3A9DA3D90F46FFCDA3F0D4F3BD3657D62997F8AA2141B943D243BE69E54B34E855F2012BDFF8B24F0BCFAA91D3CEDA4593943F6658B09E35D156699E1F24DB045A10DB272C1922EC3019842C2603BF988908F4E36CC9B000D71D8; domain=.test.com; path=/; HttpOnly
Set-Cookie: forever.test.com=UserID=PBjdvkHaNTQ=&Time=zULFFSqYKXe668wECCO0nO5Mvo9jV6qI&Status=KAyeeDyZo6Y=; domain=www.test.com; expires=Tue, 05-Jun-2018 12:32:22 GMT; path=/
httpWebRequest.CookieContainer.GetCookieHeader这方法取到的是:ASP.NET_SessionId=pfxkjfmnnx5aut3wq0af0f45; .ASPXFORMSAUTHXXForTest=C8EAA98C6B26D3442D1E7B2E7FFD1218B9526A69A3075E559A90BEE4927E2BE0A2BF77DD576FD7BBDFA14FEC4864076B3943C7837FE872C31571D908E3A3B1FA2B939226311350A8451205B51414731AF309902868E54B9B9083A961F84C6C5B
帮帮忙呀,高手们
我去掉就好了。。