当我使用 WebRequest.Create(" http://abc/test .") 执行 GET 时,我得到 404,因为根据提琴手的说法,.NET 将尾随点剥离,并且 Web 服务器需要该点。我该如何防止或解决它。任何解决方法表示赞赏!
ado
问问题
10309 次
4 回答
30
官方错误报告中解决方法选项卡中的解决方法:
..似乎是有效的。基本上,在使用 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();
}
}
}
于 2010-02-17T23:44:47.427 回答
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;
}
于 2015-07-22T09:46:44.473 回答
0
这是在 Microsoft 论坛上多次出现的已知问题。
该类Uri
错误地认为所有 URI 都像 Windows 磁盘文件一样,其中尾随点(无文件扩展名)不相关。
http://social.msdn.microsoft.com/Forums/en-US/netfxbcl/thread/5206beca-071f-485d-a2bd-657d635239c9/
于 2009-10-13T00:20:03.520 回答
-1
你把点变成字符串到十六进制
string.format("{0:x2}",yoururl);
我认为它对你很有用,因为我在 twitter API Oauth 格式中使用了它
于 2012-12-29T10:18:01.347 回答