1
在我的問題Can't login with HttpWebRequests之後,我成功登錄了。我收到頁面,它說「感謝您登錄」,但之後我似乎不是登錄。Cookie似乎並未在HttpWebRequest中使用
對我來說,這看起來像一個餅乾問題。在螢火蟲餅乾似乎是HttpOnly可以這是一個問題?我怎樣才能使HttpWebRequest
使用cookies?
這是我用於登錄的代碼:
string url = "http://www.warriorforum.com/login.php?do=login";
var bytes = Encoding.Default.GetBytes(@"vb_login_username=USERNAME&cookieuser=1&vb_login_password=&s=&securitytoken=guest&do=login&vb_login_md5password=d9350bad28eee253951d7c5211e50179&vb_login_md5password_utf=d9350bad28eee253951d7c5211e50179");
var container = new CookieContainer();
var request = (HttpWebRequest)(WebRequest.Create(url));
request.CookieContainer = container;
request.UserAgent = "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; Trident/4.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; .NET4.0C; .NET4.0E; InfoPath.3)";
request.ContentLength = bytes.Length;
request.Method = "POST";
request.ContentType = "application/x-www-form-urlencoded";
request.KeepAlive = true;
request.AllowAutoRedirect = true;
request.AllowWriteStreamBuffering = true;
request.CookieContainer = container;
using (var requestStream = request.GetRequestStream())
requestStream.Write(bytes, 0, bytes.Length);
var requestResponse = request.GetResponse();
using (var responsStream = requestResponse.GetResponseStream())
if (responsStream != null)
{
using (var responseReader = new StreamReader(responsStream))
{
var responseStreamReader = responseReader.ReadToEnd();
richTextBox1.Text = responseStreamReader; //this is to read the page source after the request
}
}
}
哦,我的上帝啊,非常感謝你,我一直在努力掙扎的一個愚蠢的錯誤 –