2011-04-12 119 views
3

我发现了一个奇怪的现象,我试图解决C#消毒/干扰指定的URL,并给出404结果。在URL字符串中斜线前斜跑全行停留

它发生在完全停止后有正斜杠的地方 - C#和Visual Studio决定删除完整的停止。

对于的Visual Studio,这是链接到恶意形成的URL

编译的C#字符串转化,字符串控制点击,甚至获得新的URI()之前。

的链接是:

http://www.mattmulholland.co.nz/Matt_Mulholland/Matt_Mulholland_-_Official_Website._Boom./rss.xml

(这是 './' 在导致问题结束)

我试图逃避两个句号+斜线在ASCII和Uri()构造函数中的'dontEscape'选项,但目前为止没有运气...

和其他想法如何我可以得到这个str为了允许正确的URL?

谢谢。

+1

什么是你逃跑的样子?当你使用这个URL时会发生什么:'http://www.mattmulholland.co.nz/Matt_Mulholland/Matt_Mulholland_-_ Official_Website._Boom%2E/rss.xml' – 2011-04-12 12:13:36

回答

1

这是我使用:

// call this line only once in application lifetime (when app starts) 
    ApplyFixEndDotUrl(); 

    // -------------------------- 
    Uri testUrl = new Uri("http://www.mattmulholland.co.nz/Matt_Mulholland/Matt_Mulholland_-_Official_Website._Boom./rss.xml"); 
    string strUrl = testUrl.ToString(); 

    // -------------------------- 
    // -> using System.Reflection; 

    public static void ApplyFixEndDotUrl() 
    { 
     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); 

        if ((flagsValue & 0x1000000) != 0) 
         flagsField.SetValue(parser, flagsValue & ~0x1000000); 
       } 
      } 
     } 
    } 

该解决方案在这里找到:HttpWebRequest to URL with dot at the end (.NET 2.0 Framework)