通过C#登录网站

我已经尝试了我可以在网上找到的关于如何实现这一点来登录这个网站的所有内容。 这是最近的失败。

// I have tried with multiple different URLS this one // and http://www.movable.com/login do not throw errors string url = "http://portal.movable.com/"; string username = ""; string password = ""; string authTok = @"+HOt3NTkkIAHkMSMvzQisEquhun9xvIG1mHzIEh6CAo="; string postData = "utf8=✓" + "&authenticity_token=" + authTok + "&user[login]=" + username + "&user[password]=" + password + "&user[offset]=-5"; var container = new CookieContainer(); var buffer = Encoding.UTF8.GetBytes(postData); var request = (HttpWebRequest)HttpWebRequest.Create(url); request.CookieContainer = container; request.UserAgent = "Mozilla/5.0"; request.Method = "POST"; request.KeepAlive = true; request.AllowAutoRedirect = true; request.CookieContainer = container; request.ContentLength = buffer.Length; request.ContentType = "application/x-www-form-urlencoded; charset=UTF-8"; request.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8"; using (var requestStream = request.GetRequestStream()) requestStream.Write(buffer, 0, buffer.Length); using (var response = request.GetResponse()) { using (var reader = new StreamReader(response.GetResponseStream())) { var result = reader.ReadToEnd(); //this is to read the page source after the request MessageBox.Show(result); } } 

这里还有来自网站的相关数据(我知道令牌在示例中有所不同,我使它们相同但不起作用)

 

Signed out successfully.

login to your account

Forgot password?

试试这种方式:

  var cookieJar = new CookieContainer(); CookieAwareWebClient client = new CookieAwareWebClient(cookieJar); // the website sets some cookie that is needed for login, and as well the 'authenticity_token' is always different string response = client.DownloadString("http://portal.movable.com/signin"); // parse the 'authenticity_token' and cookie is auto handled by the cookieContainer string token = Regex.Match(response, "authenticity_token.+?value=\"(.+?)\"").Groups[1].Value; string postData = string.Format("utf8=%E2%9C%93&authenticity_token={0}&user%5Blogin%5D=USERNAME&user%5Bpassword%5D=PASSWORD&user%5Boffset%5D=5.5&user%5Bremember_me%5D=0&button=", token); //WebClient.UploadValues is equivalent of Http url-encode type post client.Method = "POST"; response = client.UploadString("http://portal.movable.com/signin", postData); //i am getting invalid user/pass, but i am sure it will work fine with normal user/password } 

额外使用的类别:

 public class CookieAwareWebClient : WebClient { public string Method; public CookieContainer CookieContainer { get; set; } public Uri Uri { get; set; } public CookieAwareWebClient() : this(new CookieContainer()) { } public CookieAwareWebClient(CookieContainer cookies) { this.CookieContainer = cookies; } protected override WebRequest GetWebRequest(Uri address) { WebRequest request = base.GetWebRequest(address); if (request is HttpWebRequest) { (request as HttpWebRequest).CookieContainer = this.CookieContainer; (request as HttpWebRequest).ServicePoint.Expect100Continue = false; (request as HttpWebRequest).UserAgent = "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:18.0) Gecko/20100101 Firefox/18.0"; (request as HttpWebRequest).Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8"; (request as HttpWebRequest).Headers.Add(HttpRequestHeader.AcceptLanguage, "en-US,en;q=0.5"); (request as HttpWebRequest).Referer = "http://portal.movable.com/signin"; (request as HttpWebRequest).KeepAlive = true; (request as HttpWebRequest).AutomaticDecompression = DecompressionMethods.Deflate | DecompressionMethods.GZip; if (Method == "POST") { (request as HttpWebRequest).ContentType = "application/x-www-form-urlencoded"; } } HttpWebRequest httpRequest = (HttpWebRequest)request; httpRequest.AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate; return httpRequest; } protected override WebResponse GetWebResponse(WebRequest request) { WebResponse response = base.GetWebResponse(request); String setCookieHeader = response.Headers[HttpResponseHeader.SetCookie]; if (setCookieHeader != null) { //do something if needed to parse out the cookie. try { if (setCookieHeader != null) { Cookie cookie = new Cookie(); //create cookie this.CookieContainer.Add(cookie); } } catch (Exception) { } } return response; } } 

收到回复

    MOVband Portal         

Welcome

Movbanddevice

Just got your MOVband? We'll have you moving in no time with our quick product registration and setup. Join >

Invalid email or password.

login to your account

Forgot password?