user1824906
user1824906

Reputation: 59

If response redirect = true

I send POST to a url and if the response has redirect with url(which contains jsessionid) it will be true? else false.

How to write it?

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        public bool od_auth(string login, string pass)
        {
            string HTML = PostData("");
            
            return true;
        }

        private void Auth_Click(object sender, EventArgs e)
        {
            string login = textBox1.Text;
            string pass = textBox2.Text;
            bool avt = od_auth(login, pass);
        }
        public static string PostData(string file, string data)
        {
            var cookies = new CookieContainer();
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(file);
            request.Method = "POST";
            request.CookieContainer = cookies;
            request.AllowAutoRedirect = true;
            request.ContentType = "application/x-www-form-urlencoded";
            byte[] EncodedPostParams = Encoding.UTF8.GetBytes(data);
            request.ContentLength = EncodedPostParams.Length;
            request.GetRequestStream().Write(EncodedPostParams,
                                             0,
                                             EncodedPostParams.Length);
            request.GetRequestStream().Close();
            HttpWebResponse response = (HttpWebResponse)request.GetResponse();
            string html = new StreamReader(response.GetResponseStream(),
                                           Encoding.UTF8).ReadToEnd();
            return html;
        }
    }
}

Upvotes: 1

Views: 1111

Answers (1)

jbl
jbl

Reputation: 15413

You should check the response.StatusCode :

http://msdn.microsoft.com/en-us/library/system.web.httpresponse.statuscode.aspx )

and test for a redirect status code (301, 302, 303):

http://msdn.microsoft.com/fr-fr/library/aa383887%28en-us,VS.85%29.aspx

as noted by @hvd, this should not work because of request.AllowAutoRedirect = true;.

You should :

  • set request.AllowAutoRedirect = false;
  • check for the response.StatusCode
  • set your boolean depending if it is a redirect status code
  • build a new request, based on "Location" and retrieve its response

BTW, if you have nothing to add to the second request, maybe you could just compare the response.ResponseUri with file to see if the response might have been redirected

Upvotes: 1

Related Questions