我很努力弄清楚這裏有什麼問題。我發送登錄信息,我可以看到標題中的Set-Cookie具有正確的值,但Cookies集合沒有被填充。HttpWebResponse.Cookies儘管設置Cookie標頭(不重定向)
這是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標頭值:
s = 541E2101-B768-45C8-B814-34A00525E50F;域= example.com;路徑= /; 版本= 1
您確定您從服務器設置了正確的域名?它是domain.com還是.domain.com,它們都是不同的。你可以發佈你的ASP.NET你的代碼? – 2013-08-04 12:30:30