是否可以根据请求将301设置为无限循环 [英] Is it possible to set 301 in an infinite loop based on the request

查看:109
本文介绍了是否可以根据请求将301设置为无限循环的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请认为该语言不可知.我真的很想知道下面发生了什么.该代码在C#中.问题是,我正在尝试获取此URL: 代码中的https://fr.wikipedia.org/wiki/Monast%C3%A8re_d%27Arkadi. 无论我做什么,我都会收到"MaximumAutoRedirects超出"或操作" 超时".

Please consider this language agnostic. I really like to know what happens beneath. The code is in C#. The problem is, I am trying to fetch this URL: https://fr.wikipedia.org/wiki/Monast%C3%A8re_d%27Arkadi from code. Whatever I do, I get either "MaximumAutoRedirects exceeded" or "operation timed out".

使用C#编写示例代码,尽管我在其他语言中看到类似的结果:

Sample code in C#, although I see similar results in other languages:

var url = "https://fr.wikipedia.org/wiki/Monast%C3%A8re_d%27Arkadi";
var request = (HttpWebRequest)WebRequest.Create(url);
try
{
    request.CookieContainer = new CookieContainer();
    request.MaximumAutomaticRedirections = Int32.MaxValue;
    request.AllowAutoRedirect = true;
    request.Method = "GET";
    using (WebResponse response = request.GetResponse())
    using (var stream = response.GetResponseStream())
    {
        var reader = new StreamReader(stream);
        var res = reader.ReadToEnd();
    }
}
catch (Exception ex)
{

}

我的问题:

  1. 为什么会发生这种自动重定向?
  2. 是否可以检测到来自代码/浏览器的获取? (我不知道该怎么问.我的意思是,当从Chrome浏览器访问时,我会得到URL.但是,从代码执行GET时,我会得到无限重定向.因此,这些GET是否有任何不同是我的问题)

P.S:这个问题促使我提出了这个问题. 为什么Wikipedia返回带有特定URL的301响应代码?

P.S: This question prompted me to ask this. Why Wikipedia returns 301 response code with certain URL?

推荐答案

是否可以根据请求将301设置为无限循环?

Is it possible to set 301 in an infinite loop based on the request ?

是的. http服务器可以向您发送无限方案.通常,浏览器会检测到无限重定向循环,并会停止重定向.服务器端的配置问题 可以创建重定向循环. HTTP客户端应该应该能够检测到该错误并在经过一定数量的重定向后停止(在这种情况下request.MaximumAutomaticRedirections = Int32.MaxValue可能太大)

Yes. An http server can send you an inifinite scheme. Usually the browser will detect an infinite redirection loop and will stop redirecting. A configuration problem on the server side can create a redirection loop. An HTTP client should be able to detect that and stop after a certain amount of redirections (and request.MaximumAutomaticRedirections = Int32.MaxValue is maybe too big in that case)

为什么会这样?好吧,错误.您可以在HTTP服务器配置中编写具有无限重定向的规则,该HTTP服务器不是编译器,因此在服务器端将无法检测到.

Why is it happening? Well, mistakes. You can write rules in the HTTP server configuration with infinite redirections, the HTTP server is not a compiler, this will not be detected on the server side.

或者您可能要管理许多不同的服务器,而它们并不能同时获得最后的配置.在Wikipedia的情况下,例如可能有人某人修复了页面的重定向.说你有:

Or maybe you have a lot of different servers to manage and they do not all get the last configuration at the same time. In the wikipedia case it could be for example with someone fixing the redirections of a page. Say you have:

  • A-> B
  • B
  • C-> B

您将其修复为:

  • A
  • B-> A
  • C-> A

如果已注册任何反向代理缓存,则在临时缓存中注册A -> B重定向,或者如果不是所有数据库副本中都立即提供此重定向(因为Wiki在应用程序数据库中存储了很多规则),则. .您可能在客户端同时收到A->BB->A响应.

If any Reverse proxy cache as registered the A -> B redirection in a temporary cache, or if this is not immediatly available in all database replicas (as a wiki is storing a lot of rule in the application database), then... you could have both A->B and B->A responses on the client side.

为什么会发生这种自动重定向?

Why this auto re-direct is happening?

因为你写过:

request.AllowAutoRedirect = true;

请注意,我不是C#专家,但似乎合法.

Note that I'm not a C# expert but it seems legit.

是否可以检测到来自代码/浏览器的获取?

Is it possible to detect a get from code / browser?

不确定您要的是什么.

编辑 好的,所以您的问题是,在制作简单GET 时,如何跟踪浏览器发出的HTTP流量,以及如何从代码中进行跟踪.

EDIT Ok, so your question is how to track the HTTP traffic made by the browser when a simple GET is made, and maybe also how to track it from your code.

在浏览器上,您拥有一些本机工具,例如开发工具中的网络"选项卡,您可以在其中激活preserve log按钮来跟踪重定向,但是对于简单的重定向,我认为您甚至不需要预先登录即可查看302个响应,然后是其他请求.

On the browser you have some native tools like the network tab in the development tools, where you can activate the preserve log button to track redirections, but for simple redirection I think you do not even need to preverve log to see the 302 responses followed by other requests.

如果您想捕获计算机上的所有HTTP流量,则可以始终使用 wireshark ,事实并非如此.难的.或者,您可以通过 Fiddler 之类的代理在应用程序或浏览器中强制执行所有HTTP通信.

If you want to catch all HTTP traffic on your computer you can always use wireshark, which is not so hard. Or you can enforce all your HTTP traffic in your application or browser through a proxy like Fiddler.

这篇关于是否可以根据请求将301设置为无限循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆