【发布时间】:2013-02-12 18:05:24
【问题描述】:
我正在努力找出这里出了什么问题。我正在发送登录信息,我可以看到 Header 中的 Set-Cookie 具有正确的值,但是 Cookies 集合没有被填充。
这是 HTTPS,登录自动重定向,但我使用 AllowAutoRedirect=false 禁用它以尝试解决此问题。
在此屏幕截图中,您可以轻松查看调试信息以及应该设置 cookie。我正在将我的 httpWebRequest.Cookie 设置为新的 CookieCollection。
HttpWebRequest httpRequest;
CookieContainer reqCookies = new CookieContainer();
string url = "https://example.com";
string[] email = user.Split('@');
email[0] = System.Web.HttpUtility.UrlEncode(email[0]);
user = email[0] + "@" + email[1];
pass = System.Web.HttpUtility.UrlEncode(pass);
string postData = "email=" + user + "&password=" + pass;
byte[] byteData = Encoding.UTF8.GetBytes(postData);
httpRequest = (HttpWebRequest)WebRequest.Create(url);
httpRequest.Method = "POST";
httpRequest.Referer = url;
httpRequest.CookieContainer = reqCookies;
httpRequest.UserAgent = "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/535.19 (KHTML, like Gecko) Chrome/18.0.1003.1 Safari/535.19";
httpRequest.Accept = "text/html, application/xhtml+xml, */*";
httpRequest.ContentType = "application/x-www-form-urlencoded";
httpRequest.ContentLength = byteData.Length;
using (Stream postStream = httpRequest.GetRequestStream())
{
postStream.Write(byteData, 0, byteData.Length);
postStream.Close();
}
httpRequest.AllowAutoRedirect = false;
HttpWebResponse b = (HttpWebResponse)httpRequest.GetResponse();
尝试使用完全相同的代码连接到http://www.yahoo.com,并将 cookie 放入我的收藏中...啊...
这里是 Set-Cookie Header 值:
s=541E2101-B768-45C8-B814-34A00525E50F;域=example.com;路径=/; 版本=1
【问题讨论】:
-
您确定从服务器设置的域名正确吗?是 domain.com 还是 .domain.com ,它们都是不同的。您可以发布您的 ASP.NET 代码吗?
标签: c# cookies httpwebrequest