0
我正在使用Visual Studio 2010和.NET Framework 4.0。並非所有的cookie都使用cookiecontainer保存
代碼:
public void LoginTo(string username, string password)
{
HttpWebRequest req = (HttpWebRequest)WebRequest.Create(this.Url + "login.php");
string values =
"username=" + username +
"&password=" + password +
"&redirect=" +
"&autologin=on" +
"&login=Log in";
req.Method = "POST";
req.ContentType = "application/x-www-form-urlencoded";
req.ContentLength = values.Length;
req.KeepAlive = true;
req.UserAgent = "Mozilla/5.0 (Windows NT 5.1; rv:2.0) Gecko/20100101 Firefox/4.0";
CookieContainer a = new CookieContainer();
Uri uri = new Uri(this.Url);
req.CookieContainer = a;
ServicePointManager.Expect100Continue = false; // prevents 417 error
using (StreamWriter writer = new StreamWriter(req.GetRequestStream(), System.Text.Encoding.Default))
{
writer.Write(values);
}
HttpWebResponse response = (HttpWebResponse)req.GetResponse();
StreamReader reader = new StreamReader(response.GetResponseStream());
formResponseWatcher formResponseWatcher = new formResponseWatcher();
formResponseWatcher.SetResponseContent(reader.ReadToEnd());
formResponseWatcher.Show();
foreach (Cookie cookie in response.Cookies)
{
cookie.HttpOnly = true;
Cookie = Cookie + cookie + ";";
}
}
的問題是,的CookieContainer並不包含所有的cookies。在Firefox中有5個cookie,但CookieContainer僅檢索3個。我需要所有的cookies都要被記錄下來。 我認爲像以下域名存在問題:.domain.com。 幫幫我!
Fiddler幫了我很多。它現在有效。謝謝! Cookie沒有問題。還有另一個問題:)。 – carck3r 2011-04-02 09:19:28