我用httpwebrequest 下载 "http://www2.baidu.com/?castk=e0d54av7eb5470266d589"
本来想得到通过HttpWebResponse.Headers.Get("Set-cookie");得到返回的cookie但是奇怪的是,在下载这个页面的时候,会自动下载以下两个url
http://fengchao.baidu.com/indexAction.do?uid=448186
http://fengchao.baidu.com/indexAction.do?uid=448186&userid=448186结果通过HttpWebResponse.Headers.Get("Set-cookie");得到的是
http://fengchao.baidu.com/indexAction.do?uid=448186&userid=448186
这个url返回的cookie,而不是http://www2.baidu.com/?castk=e0d54av7eb5470266d589
这个url返回的cookie;我该怎么解决才能得到http://www2.baidu.com/?castk=e0d54av7eb5470266d589
这个url返回的set-cookie参数各位大虾帮帮忙!!!小弟很急
本来想得到通过HttpWebResponse.Headers.Get("Set-cookie");得到返回的cookie但是奇怪的是,在下载这个页面的时候,会自动下载以下两个url
http://fengchao.baidu.com/indexAction.do?uid=448186
http://fengchao.baidu.com/indexAction.do?uid=448186&userid=448186结果通过HttpWebResponse.Headers.Get("Set-cookie");得到的是
http://fengchao.baidu.com/indexAction.do?uid=448186&userid=448186
这个url返回的cookie,而不是http://www2.baidu.com/?castk=e0d54av7eb5470266d589
这个url返回的cookie;我该怎么解决才能得到http://www2.baidu.com/?castk=e0d54av7eb5470266d589
这个url返回的set-cookie参数各位大虾帮帮忙!!!小弟很急
{
HttpWebRequest myHttpWebRequest = (HttpWebRequest)WebRequest.Create(URL); CookieContainer co = new CookieContainer();
co.SetCookies(new Uri(URL), curcookie);
myHttpWebRequest.CookieContainer = co;
HttpWebResponse myHttpWebResponse = (HttpWebResponse)myHttpWebRequest.GetResponse();
Stream receiveStream = myHttpWebResponse.GetResponseStream();
Encoding encode = System.Text.Encoding.Default;
StreamReader readStream = new StreamReader(receiveStream, encode);
Char[] read = new Char[1024];
int count = readStream.Read( read, 0, 1024 );
StringBuilder sb=new StringBuilder();
while (count > 0)
{
String str = new String(read, 0, count);
sb.Append(str);
count = readStream.Read(read, 0, 1024);
}
//string header=myHttpWebResponse.Headers.ToString();
cookie = myHttpWebResponse.Headers.Get("Set-cookie");
myHttpWebResponse.Close();
readStream.Close();
return sb.ToString(); }
下载 "http://www2.baidu.com/?castk=e0d54av7eb5470266d589" 的时候,url有重定向,那么如何在重定向之前得到“set-cookie”值
下载 "http://www2.baidu.com/?castk=e0d54av7eb5470266d589" 的时候,url有重定向,那么如何在重定向之前得到“set-cookie”值
{
HttpWebResponse res = null;
string strResult = ""; HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);
try
{
req.KeepAlive = true;
req.Accept = "image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/vnd.ms-excel, application/msword, application/x-shockwave-flash, */*";
req.Referer = referer;
req.UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; Maxthon; .NET CLR 2.0.50727)";
//设置cookie
reqcookie.Add(cookie);
req.CookieContainer = reqcookie;
//req.CookieContainer.Add(cookie); if (proxy != "")
{
WebProxy proxys = new WebProxy();
proxys.Address = new Uri("http://" + this.proxy);
req.Proxy = proxys;
}
else
{
req.Proxy = null;
} //byte[] data = encoding.GetBytes(postdata);
if (data.Length > 0)
{
req.Method = "POST";
req.ContentType = "application/x-www-form-urlencoded";
req.ContentLength = data.Length;
using (System.IO.Stream sw = req.GetRequestStream())
{
try
{ sw.Write(data, 0, data.Length);
}
catch
{
strResult = "";
return strResult;
}
finally
{ }
}
}
try
{
HttpWebResponse response = null;
System.IO.StreamReader sr = null; try
{ response = (HttpWebResponse)req.GetResponse();
foreach (Cookie c in response.Cookies)
{
Console.WriteLine("Name: {0}", c.Name);
Console.WriteLine("Value: {0}", c.Value);
Console.WriteLine("Domain: {0}", c.Domain);
Console.WriteLine("Expires: {0}", c.Expires);
}
Console.WriteLine("============================================");
this.cookie.Add(response.Cookies);
sr = new System.IO.StreamReader(response.GetResponseStream(), encoding); strResult = sr.ReadToEnd(); }
catch (WebException webex)
{
if (webex.Status == WebExceptionStatus.KeepAliveFailure)
{
strResult = "";
return strResult;
}
else
{
strResult = "";
return strResult;
}
}
catch (System.Exception ex)
{
strResult = ex.Message;
return strResult;
}
finally
{
if (sr != null) { sr.Close(); }
if (response != null) { response.Close(); }
response = null;
req = null;
}
return strResult;
}
catch
{
return "";
}
}
catch
{
return "";
}
}
模拟发送的包如下
GET /?action=logout&u=http%3A%2F%2Fcas.baidu.com%2F%3Ftpl%3Dwww2 HTTP/1.1Accept: image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/vnd.ms-excel, application/msword, application/x-shockwave-flash, */*User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; Maxthon; .NET CLR 2.0.50727)Host: cas.baidu.com返回的数据包如下
HTTP/1.1 302 FoundDate: Sat, 18 Jul 2009 03:13:09 GMTServer: Apache/2.2.9 (Unix) mod_ssl/2.2.9 OpenSSL/0.9.7a DAV/2 PHP/5.2.4X-Powered-By: PHP/5.2.4Expires: Thu, 19 Nov 1981 08:52:00 GMTCache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0Pragma: no-cacheLocation: http://cas.baidu.com/?tpl=www2Content-Length: 0Connection: closeContent-Type: text/html
请求.AllowAutoRedirect = false;