我正在努力弄清楚这里出了什么问题。我正在发送登录信息,我可以在 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 标头值:
s=541E2101-B768-45C8-B814-34A00525E50F;域=example.com;路径=/; 版本=1