Logging into a website using Webrequest

Asked

Viewed 104 times

1

I need to log into a website using the Asp.net web Forms. I am doing so. How could I redirect to the page after logged in?

        protected void Button1_Click(object sender, EventArgs e)
        {
            var loginAddress = "http://sportone.sisguardiao.com.br/";
            var loginData = new NameValueCollection
                    {
                      { "LOGIN", "abfg" },
                      { "SENHA", "123" }
                    };
            Login(loginAddress, loginData);

        }

        public void Login(string loginPageAddress, NameValueCollection loginData)
        {
            CookieContainer container;

            var request = (HttpWebRequest)WebRequest.Create(loginPageAddress);

            request.Method = "POST";
            request.ContentType = "application/x-www-form-urlencoded";
            var buffer = Encoding.ASCII.GetBytes(loginData.ToString());
            request.ContentLength = buffer.Length;
            var requestStream = request.GetRequestStream();
            requestStream.Write(buffer, 0, buffer.Length);
            requestStream.Close();

            container = request.CookieContainer = new CookieContainer();

            var response = request.GetResponse();
            response.Close();

        }
  • Friend, could you give a hand with your great knowledge? I thank you!

1 answer

1

Do Login return bool:

    public bool Login(string loginPageAddress, NameValueCollection loginData)
    {
        try 
        {
            CookieContainer container;

            var request = (HttpWebRequest)WebRequest.Create(loginPageAddress);

            request.Method = "POST";
            request.ContentType = "application/x-www-form-urlencoded";
            var buffer = Encoding.ASCII.GetBytes(loginData.ToString());
            request.ContentLength = buffer.Length;
            var requestStream = request.GetRequestStream();
            requestStream.Write(buffer, 0, buffer.Length);
            requestStream.Close();

            container = request.CookieContainer = new CookieContainer();

            var response = request.GetResponse();
            response.Close();

            return true;
        }
        catch (Exception e)
        {
            return false;
        }
    }

In the event of the button:

    protected void Button1_Click(object sender, EventArgs e)
    {
        var loginAddress = "http://sportone.sisguardiao.com.br/";
        var loginData = new NameValueCollection
                {
                  { "LOGIN", "abfg" },
                  { "SENHA", "123" }
                };

        if (Login(loginAddress, loginData))
            Server.Transfer("Pagina.aspx", true);
    }

Or, if you want to redirect to a fixed address, use instead of Server.Transfer:

Response.Redirect("http://sportone.sisguardiao.com.br");
  • this server.Transfer("Pagina.aspx", true); would be to the page "http://sportone.sisguardiao.com.br/" in this way it would be logged in?

  • The page is yours?

  • Yes, I want to redirect to "http://sportone.sisguardiao.com.br/"; after logging in

  • I edited the answer. See if it fits you.

  • Redirected more was not logged in, should this logged in

  • Then you need to check response. It is in this object that it is set whether the login worked or not.

  • He returned as true

  • here would be the ID of the field and password { "LOGIN", "abfg" }, { "PASSWORD", "123" }

Show 3 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.