ASP HttpWebRequest 和重定向

发布于 2024-08-21 01:00:38 字数 1008 浏览 5 评论 0原文

好的,我有一个客户端使用一些数据向服务器发送 POST。服务器接收该帖子,并通过重定向进行答复。问题是客户端没有重定向。另外,我尝试检查客户端收到的响应的 StatusCode,它始终是相同的“OK”。而不是重定向代码。我缺少什么?

在客户端,我有这样的东西:

  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) {}

在服务器端,我只有这一行:

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

在这种情况下,客户端收到答案并且不执行任何操作。

谢谢。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

晚风撩人 2024-08-28 01:00:38

当您将“AllowAutoRedirect”设置为 true 时,这意味着您的 HttpWebRequest 对象在看到重定向后将发出第二个 Web 请求。当您从响应对象中看到“200 OK”时,这是因为您看到的是“www.google.com”的响应。您可以检查 Response.ResponseURI 来验证这一点。

您需要关闭“AllowAutoRedirect”,然后像 Oded 所说的那样检查响应代码。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文