如何处理来自 .net 远程处理的 http 302 重定向?

发布于 2024-07-23 02:29:01 字数 802 浏览 4 评论 0原文

我需要能够将 http 302 重定向与使用 .net 远程处理的代码区分开来。

为了连接到适当的网址,我们按顺序尝试它们,直到其中一个有效为止。 在大多数环境中,第一个 url 会正确失败,并出现 System.Net.WebException,状态为 WebExceptionStatus.NameResolutionFailure。 然而,对于使用 OpenDNS 的客户,这会以 HTTP 302 响应的形式返回,并重定向到其指南页面。 因为我使用的是 .net Remoting,所以重定向被忽略,并且我收到的错误是 System.Runtime.Serialization.SerializationException,只能通过其消息“解析完成之前遇到流结束”来区分。

这是 opendns http 响应: HTTP/1.0 302 找到 位置: http://guide.opendns .com/?url=demonstration.totallyinvalidexceptonmynetwork.com%2Fi%2Fdo%2Fnot%2Fexist.html 内容类型:text/html 内容长度:0 连接:关闭 日期:2009 年 5 月 28 日星期四 20:46:26 GMT 服务器:OpenDNS 指南

I need to be able to distinguish http 302 redirects from my code that use .net remoting.

In order to connect to the appropriate url we try them in order until one works. In most environments the first url correctly fails with a System.Net.WebException with a status of
WebExceptionStatus.NameResolutionFailure.
For Customers that use OpenDNS, however this comes back as a HTTP 302 response that redirects to their Guide page. Because I am using .net Remoting the redirect is ignored, and the error I recieve is a System.Runtime.Serialization.SerializationException that is only distinguishable by its message "End of Stream encountered before parsing was completed."

Here's the opendns http response:
HTTP/1.0 302 Found
Location: http://guide.opendns.com/?url=demonstration.totallyinvalidexceptonmynetwork.com%2Fi%2Fdo%2Fnot%2Fexist.html
Content-type: text/html
Content-Length: 0
Connection: close
Date: Thu, 28 May 2009 20:46:26 GMT
Server: OpenDNS Guide

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

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

发布评论

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

评论(1

逆光下的微笑 2024-07-30 02:29:01

我假设要发布请求,您正在使用 HttpWebRequest 对象。 然后您可以在 HttpWebResponse 对象中接收响应,如下所示。
然后您可以检查 response.StatusCode 属性。 如果是302则执行你的逻辑。

HttpWebResponse response = (HttpWebResponse)request.GetResponse();
if (response.StatusCode = HttpStatusCode.Found)
{
   //your code here
}

更新:如果您使用的是 System.Net.Webclient,那么您可以使用它的 ResponseHeaders 属性。 此处介绍了如何使用它。

I assume that to post the request you are using the HttpWebRequest object. Then you can recieve the response in the HttpWebResponse object like bellow.
Then you can check the response.StatusCode property. If it is 302 then perform your logic.

HttpWebResponse response = (HttpWebResponse)request.GetResponse();
if (response.StatusCode = HttpStatusCode.Found)
{
   //your code here
}

Update: If you are using System.Net.Webclient then you can use the you can use its ResponseHeaders property. Here is how you can use it.

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