Logging into website via C#

前端 未结 1 1030
梦谈多话
梦谈多话 2021-02-04 17:57

I have tried everything I can find on the web on how to implement this to log into this site. Here is the most recent failure.

 // I have tried with multiple dif         


        
1条回答
  •  迷失自我
    2021-02-04 18:43

    Try this way:

            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
    
        }
    

    Extra Class Used :

    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;
    
        }
    }
    

    Response Received

    
    
    
      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?

    0 讨论(0)
提交回复
热议问题