如何使用CookieContainer

时间:2014-09-28 17:20:54

标签: c# cookies httpwebrequest httpwebresponse setcookie

在通过HttpWebRequest发送请求后,没有设置Cookiecontainer。

我使用live httpheader chorme,登录后的结果

Set-Cookie: SHARINGSESSID4S=9jpuh21oi82s10mn6cvomjq190; expires=Mon, 29-Sep-2014 16:09:22 GMT;path=/; domain=.4share.v

然而,我只获得了SHARINGSESSID4S = 9jpuh21oi82s10mn6cvomjq190。 我不能使用cookie来进行子请求。 我的代码:

private void button1_Click(object sender, EventArgs e)
{
 CookieContainer cookie = new CookieContainer();
 Uri site=new Uri("http://4share.vn/index/login");
 var request = (HttpWebRequest)WebRequest.Create(site);
 var postdata = "username=XXXXX&password=XXXXX";
 var data = Encoding.ASCII.GetBytes(postdata);
 request.KeepAlive = true;
 request.Method = "POST";
 request.ContentType = "application/x-www-form-urlencoded";
 request.ContentLength = data.Length;
 request.CookieContainer = cookie;
 var stream = request.GetRequestStream();
 stream.Write(data, 0, data.Length);
 stream.Close();
 MessageBox.Show(cookie.GetCookieHeader(site));
 var response = (HttpWebResponse)request.GetResponse();
 MessageBox.Show(cookie.GetCookieHeader(site));
 Uri site2 = new Uri("http://4share.vn/f/487b7d717a717b71/V-Z%20Naruto.Shippuden.Ultimate.Ninja.Storm.Revolution-CODEX@Jenty.VNZ.iso");
 var request2 = (HttpWebRequest)WebRequest.Create(site2);
 request2.CookieContainer = cookie;
 var response2 = (HttpWebResponse)request2.GetResponse();
 MessageBox.Show(cookie.GetCookieHeader(site2));
 string Text = new StreamReader(response2.GetResponseStream()).ReadToEnd();
 if (Text.IndexOf(" FileDownload:  ") > 0) MessageBox.Show("success");
}

0 个答案:

没有答案