ASP HttpWebRequest and Redirect

10,233

When you have "AllowAutoRedirect" set to true, it means that your HttpWebRequest object will make a 2nd webrequest once it sees a redirect. When you see the "200 OK" from the response object, it is because you are seeing the response for "www.google.com". You can check the Response.ResponseURI to verify this.

You'll need to turn off the "AllowAutoRedirect", then check the response code like Oded said.

Share:
10,233
user252816
Author by

user252816

Updated on June 09, 2022

Comments

  • user252816
    user252816 about 2 years

    OK, I have a client doing a POST to a server with some data. The server receives the post, and answers with a redirect. The problem is that the client does not redirects. Also, I've tried to check the StatusCode of the response the client gets, and it is always the same "OK". Instead of the redirect code. What am I missing?

    In the client side I have something like this:

      StringBuilder sb;
            try
            {
                HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://localhost/serv/Default.aspx");
                request.Method = "POST";                
    
            byte[] data = Encoding.ASCII.GetBytes(GetDATA());
    
            request.ContentType = "text/xml";
            request.ContentLength = data.Length;
            Stream stream = request.GetRequestStream();
            stream.Write(data, 0, data.Length);
    
            request.AllowAutoRedirect = true;
            request.MaximumAutomaticRedirections = 10;
    
            HttpWebResponse response = (HttpWebResponse) request.GetResponse();
                response.Close(); } catch(Exception ex) {}
    

    In the server side I have just this line:

    HttpContext.Current.Response.Redirect("http://www.google.com", true);
    

    In this case, the client receives an answer and does not do nothing.

    Thanks.