-1
我有一个要求从登录中获取cookie的要求,因为我会将这个cookie应用于同一页面。这里是我的代码我无法从C#HTTPWebRequest获取cookie
string boundary = "41184676334";
string username = "username";
string password = "password";
string login_post_data =
"POSTDATA=-----------------------------" + boundary +
"\nContent-Disposition: form-data; name=\"login\"" +
"\n\n" + username +
"\n-----------------------------" + boundary +
"\nContent-Disposition: form-data; name=\"key\"" +
"\n\n" + password +
"\n-----------------------------" + boundary +
"\nContent-Disposition: form-data; name=\"clcode\"" +
"\n\n" +
"\n-----------------------------" + boundary + "--";
var cookies = new CookieContainer();
HttpWebRequest request = (HttpWebRequest)WebRequest.Create("https://site.com/cgi-bin/login.pl?logout");
request.CookieContainer = cookies;
request.Method = "POST";
request.ContentType = "multipart/form-data; boundary=---------------------------" + boundary;
request.Host = "site.com";
request.UserAgent = "Mozilla/5.0 (Windows NT 5.1; rv:14.0) Gecko/20100101 Firefox/14.0.1";
request.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8";
request.Referer = "https://site.com/cgi-bin/login.pl?logout";
//request.Headers.Add("POSTDATA", post_data);
using (var requestStream = request.GetRequestStream())
{
requestStream.Write(StrToByteArray(login_post_data), 0, StrToByteArray(login_post_data).Length);
}
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
using (Stream stream = response.GetResponseStream())
{
StreamReader reader = new StreamReader(stream);
string temp = reader.ReadToEnd();
richTextBox1.Text = temp;
}
foreach (string c in response.Cookies)
{
listBox1.Items.Add(c);
}
的事情是“https://site.com/cgi-bin/login.pl?logout”是在同一页我登录后,我需要传递Cookie
网址https://site.com/cgi-bin/login.pl?logout是我放置用户名和密码后的登录页面,点击提交按钮时,地址栏上显示的是同一网址,但网页的布局是不同的。我想要做的是获取cookie。显然我的代码没有完成这项工作。 – 2012-08-22 19:53:03
登录页面后面的网页假设在其document.cookie中有一个cookie值,但它没有 – 2012-08-29 19:34:59