【问题标题】:HttpWebRequest to URL with dot at the endHttpWebRequest 到最后带有点的 URL
【发布时间】:2010-10-25 19:22:01
【问题描述】:

当我使用 WebRequest.Create("http://abc/test.") 执行 GET 时,我得到 404,因为根据提琴手的说法,.NET 将尾随点剥离,并且 Web 服务器需要该点。我该如何防止或解决它。感谢任何解决方法!

【问题讨论】:

标签: .net httpwebrequest webrequest


【解决方案1】:

这是一个在 Microsoft 论坛上多次出现的已知问题。

Uri 类错误地认为所有 URI 都像 Windows 磁盘文件一样,其中尾随点(无文件扩展名)不相关。

http://social.msdn.microsoft.com/Forums/en-US/netfxbcl/thread/5206beca-071f-485d-a2bd-657d635239c9/

【讨论】:

    【解决方案2】:

    官方错误报告中解决方法选项卡中的解决方法:

    https://connect.microsoft.com/VisualStudio/feedback/details/386695/system-uri-incorrectly-strips-trailing-dots?wa=wsignin1.0#tabs

    .. 似乎是有效的。基本上,在使用 System.Uri 之前运行此代码以重置 .NET 中的静态标志:

    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);
            }
        }
    }
    

    演示:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Reflection;
    
    namespace ConsoleApplication2
    {
        class Program
        {
            static void Main(string[] args)
            {
                var surl = "http://x/y./z";
    
                var url = new Uri(surl);
                Console.WriteLine("Broken: " + url.ToString());
    
                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);
                        }
                    }
                }
    
                url = new Uri(surl);
                Console.WriteLine("Fixed: " + url.ToString());
    
                Console.WriteLine("Press ENTER to exit ...");
                Console.ReadLine();
            }
        }
    }
    

    【讨论】:

    • 非常感谢! ;-)
    • 对我不起作用。我正在制作一个 Windows 窗体应用程序。
    【解决方案3】:

    你把点变成字符串到十六进制

     string.format("{0:x2}",yoururl);

    我认为它对你有用,因为我在 twitter API Oauth 格式中使用了它

    【讨论】:

      【解决方案4】:

      将其中的一些重写为不需要您添加任何命名空间的函数

          private Uri MyUri(string url)
          {
              Uri uri = new Uri(url);
              System.Reflection.MethodInfo getSyntax = typeof(UriParser).GetMethod("GetSyntax", System.Reflection.BindingFlags.Static | System.Reflection.BindingFlags.NonPublic);
              System.Reflection.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);
                      }
                  }
              }
              uri = new Uri(url);
              return uri;
          }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2019-01-17
        • 2011-08-27
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-11-05
        相关资源
        最近更新 更多