.net HttpWebRequest到URL,末尾带有点

.net HttpWebRequest到URL,末尾带有点,.net,httpwebrequest,webrequest,.net,Httpwebrequest,Webrequest,当我对WebRequest.Create(“.”)执行GET时,我得到404,因为根据fiddler的说法,后面的点会被.NET剥离,web服务器需要这个点。我该如何预防或解决这一问题。任何解决方法都值得赞赏 这是一个已知的问题,在Microsoft论坛上出现过几次 Uri类错误地认为所有Uri的行为都类似于Windows磁盘文件,其中尾随点(表示无文件扩展名)不相关 在官方错误报告的“解决方法”选项卡中的解决方法: 。。似乎是正确的。基本上,在使用System.Uri之前,运行以下代码重置.

当我对WebRequest.Create(“.”)执行GET时,我得到404,因为根据fiddler的说法,后面的点会被.NET剥离,web服务器需要这个点。我该如何预防或解决这一问题。任何解决方法都值得赞赏

这是一个已知的问题,在Microsoft论坛上出现过几次

Uri
类错误地认为所有Uri的行为都类似于Windows磁盘文件,其中尾随点(表示无文件扩展名)不相关


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

。。似乎是正确的。基本上,在使用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();
        }
    }
}

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

string.format("{0:x2}",yoururl); 格式(“{0:x2}”,yoururl);
我认为它对u很有用,因为我在TwitterAPI Oauth格式化中使用了它

将其中的一部分重新编写到一个不需要添加任何名称空间的函数中

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

确认此问题已在.NET4.5中修复。非常感谢!;-)没有为我工作。我正在制作一个windows窗体应用程序。