【发布时间】:2012-01-07 01:19:20
【问题描述】:
我尝试了许多以编程方式登录 https 网站的方法,但我遇到了问题。每次我收到一条错误消息,指出我的登录名和密码不正确。我确信它们是正确的,因为我可以使用相同的凭据通过浏览器登录该站点。
失败代码
HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create("https://www.majesticseo.com/account/login?EmailAddress=myemail&Password=mypass&RememberMe=1");
request.UserAgent = "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:8.0) Gecko/20100101 Firefox/8.0";
request.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,**;q=0.8";
request.UnsafeAuthenticatedConnectionSharing = true;
request.Method = "POST";
request.KeepAlive = true;
request.ContentType = "application/x-www-form-urlencoded";
request.AllowAutoRedirect = true;
request.CookieContainer = container;
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
//String tmp;
foreach(Cookie cookie1 in response.Cookies)
{
container.Add(cookie1);
}
Stream stream = response.GetResponseStream();
string html = new StreamReader(stream).ReadToEnd();
Console.WriteLine("" + html);
【问题讨论】:
-
您在代码中的哪个位置设置凭据?所有注释掉的代码的目的是什么?它与问题有关吗?如果没有,请删除它。
-
很明显你没有做 BASIC auth,所以我不能告诉你需要设置哪些字段,因为我不知道这个网站是什么。另外请格式化和清理这段代码,它目前的形式非常令人困惑。
标签: c# ssl https httpwebrequest