使用Webclient登录时“禁用Cookie”

时间:2011-10-19 10:32:37

标签: c# cookies web

我正在尝试使用以下代码登录网站

CookieContainer cookieContainer = new CookieContainer();
string formUrl = "https://dualis.dhbw.de/scripts/mgrqcgi"; 
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(formUrl);
request.Method = "POST";
request.CookieContainer = cookieContainer;
request.Referer = "https://dualis.dhbw.de";
request.ContentType = "application/x-www-form-urlencoded"; 
using (StreamWriter writer = new StreamWriter(request.GetRequestStream(), Encoding.ASCII)) {
    writer.Write("usrname=" + username + "&pass=" + password);
}
using (HttpWebResponse response = (HttpWebResponse)request.GetResponse()) {
    using (StreamReader reader = new StreamReader(response.GetResponseStream())) {
        String result = reader.ReadToEnd();
    }
}

但网站上写着“你的浏览器cookie被禁用......”

感谢您的帮助。

1 个答案:

答案 0 :(得分:0)

网站在之前的网页上设置Cookie。

获取该页面和Cookie后,登录工作。