gpt4 book ai didi

.net - HttpWebRequest 到 URL 末尾带点

转载 作者:行者123 更新时间:2023-12-03 10:26:51 24 4
gpt4 key购买 nike

当我使用 WebRequest.Create("http://abc/test .") 执行 GET 时,我得到 404,因为根据 fiddler 的说法,尾随点被 .NET 剥离,而 Web 服务器需要该点。我怎样才能防止这种情况或解决它。任何解决方法表示赞赏!

最佳答案

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

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

关于.net - HttpWebRequest 到 URL 末尾带点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/856885/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com