我已經嘗試了很多方式以編程方式登錄到https網站,但我遇到了問題。每當我收到錯誤消息,說明我的登錄名和密碼不正確。我相信他們是正確的,因爲我可以使用相同的憑據通過瀏覽器登錄到網站。HttpWebRequest,C#和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認證,所以我不能真正告訴你需要設置哪些字段,因爲我不知道這個網站是什麼。同時請格式化並清理此代碼,這在目前的形式中非常混亂。 –