HTTPWebRequest 重定向失败

发布于 2024-11-02 15:30:26 字数 475 浏览 2 评论 0 原文

我的代码尝试导航通过外部进程提供给它的 Url,但最近我发现 HTTPWebRequest 重定向中的一个错误 URL 为 http://ask.bitauto.com/TreeAsk/index.aspx

我发现,当指定了 HTTP-Referer 时,此 url 在浏览器中成功导航,但即使分配了 .Refer,.Net 的 WebRequest 也会失败(不提供任何异常),以防 AutoRedirects Enabled 失败并出现 MaxRedirect 达到异常

根据 MSDN

“如果AllowAutoRedirect属性为true,则当请求重定向到另一个站点时,会自动设置Referer属性。”

这是否意味着同一站点中的重定向会以不同的方式处理。如果有人知道如何解决这个问题,请告诉我。

提前致谢

My Code Tries to navigate Url provided to it via external processes, but recently I found a bug in HTTPWebRequest Redirection
the URL in case was http://ask.bitauto.com/TreeAsk/index.aspx

What I found is this url gets NAvigated in browser successfully when HTTP-Referer is specified but .Net's WebRequest fails (without providing any exception) even when .Refer is assigned, in case of AutoRedirects Enabled is failed with MaxRedirect reached exception

According to MSDN

"If the AllowAutoRedirect property is true, the Referer property is set automatically when the request is redirected to another site."

does this mean redirects with in same site are handled differently. Please let me know if someone has any idea how to solve this.

Thanks in Advance

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

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

发布评论

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

评论(1

岁月无声 2024-11-09 15:30:26

我认为这个问题的根源在于http://ask.bitauto.com/TreeAsk/index。 .aspx.

跟随您的链接(因此有一个 stackoverflow 的引荐来源网址)会得到这个答案:

HTTP/1.1 302 Found
Date: Thu, 21 Apr 2011 07:28:03 GMT
Server: Microsoft-IIS/6.0
X-Powered-By: ASP.NET
X-AspNet-Version: 2.0.50727
Location: 
Cache-Control: private
Content-Type: text/html; charset=utf-8
Content-Length: 117

注意空的 Location 标头 - 来自服务器的回复是重定向到...什么都没有。

编辑:

似乎只有在您没有引荐来源网址或非ask.bitauto.com引荐来源网址时才会显示空重定向页面。将目标 URL 设置为引荐来源网址会产生 200 OK 答案。

编辑 2:

空 Location 标头行为不符合 RFC2616,似乎不同的网络代理在不同的方式。

我认为最可靠的解决方案是指定 HTTPWebRequest。您的请求中的推荐人

I think the root of this problem is within http://ask.bitauto.com/TreeAsk/index.aspx.

Following your link (thus having a referrer to stackoverflow) results in this answer:

HTTP/1.1 302 Found
Date: Thu, 21 Apr 2011 07:28:03 GMT
Server: Microsoft-IIS/6.0
X-Powered-By: ASP.NET
X-AspNet-Version: 2.0.50727
Location: 
Cache-Control: private
Content-Type: text/html; charset=utf-8
Content-Length: 117

Notice the empty Location header - the reply from the server is a redirect to... nothing.

Edit:

It seems like the empty redirect page is only shown if you have no referrer or a non-ask.bitauto.com referrer. Setting the target URL as referrer results in a 200 OK answer.

Edit 2:

The empty Location header behavior isn't according to RFC2616, and it seems like different web agents react to it in different way.

I think the most robust solution for you is to specify HTTPWebRequest.Referrer in your request.

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