简体   繁体   English

System.Uri问题

[英]Issue with System.Uri

I'm having an unexpected behavior with the System.Uri class. 我在System.Uri类中发生意外行为。 When an instance of System.Uri is created, and the UrlString has some patterns like ... , or ...# , or .# , the System.Uri removes all repeated . 创建System.Uri的实例,并且UrlString具有某些模式,例如......#.#System.Uri删除所有重复的模式. characters. 字符。

This is weird, but I believe this behavior is based on RFC 2396. 这很奇怪,但是我相信这种行为是基于RFC 2396的。

The problem begins when I try to download the HTML from this URL: http://www.submarino.com.br/produto/1/23853463/mundo+segundo+steve+jobs,+o:+as+frases+mais+inspiradoras+ ... 当我尝试从以下URL下载HTML时,问题开始了: http : //www.submarino.com.br/produto/1/23853463/mundo+segundo+steve+jobs,+o :+ as+frases+mais+ inspiradoras + ...

and the System.Uri removes all the repeated . System.Uri删除所有重复的. s. s。 As the web site doesn't recognize the "New URL," it redirects to the rriginal URL. 由于该网站无法识别“新URL”,因此它将重定向到原始URL。 Then a "System.Net.WebException: Too many automatic redirections were attempted" is thrown and the page is never reached. 然后将引发“ System.Net.WebException:尝试进行过多的自动重定向”,并且永远无法访问该页面。

How can I solve this issue? 我该如何解决这个问题?

You can use reflection to remove that particular attribute. 您可以使用反射来删除该特定属性。 Use this before your Uri call: 在您致电Uri之前使用此功能:

MethodInfo getSyntax = typeof(UriParser).GetMethod("GetSyntax", System.Reflection.BindingFlags.Static | System.Reflection.BindingFlags.NonPublic);
FieldInfo flagsField = typeof(UriParser).GetField("m_Flags", System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.NonPublic);
if (getSyntax != null && flagsField != null)
{
    foreach (string scheme in new[] { "http", "https" })
    {
        UriParser parser = (UriParser)getSyntax.Invoke(null, new object[] { scheme });
        if (parser != null)
        {
            int flagsValue = (int)flagsField.GetValue(parser);
            // Clear the CanonicalizeAsFilePath attribute
            if ((flagsValue & 0x1000000) != 0)
                flagsField.SetValue(parser, flagsValue & ~0x1000000);
        }
    }
}

It has been reported to Connect before . 据报道以前Connect的

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM