开发者

ASP HttpWebRequest and Redirect

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 do开发者_JAVA百科es 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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜