gpt4 book ai didi

c# - 在 URL 字符串中的斜杠前转义句号

转载 作者:行者123 更新时间:2023-11-30 18:05:23 25 4
gpt4 key购买 nike

我发现了一个奇怪的地方,我试图绕过 C# 清理/干扰指定的 URL,结果给出 404。

它发生在句号后有正斜杠的地方 - C# 和 Visual Studio 决定删除句号。

对于 Visual Studio,这是从链接到格式错误的 URL 的字符串中按住 Control 单击

编译的 C# 中,字符串被转换,甚至在到达新的 URI() 之前。

链接是:

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

(导致问题的是末尾的'./')

我试过转义 ASCII 中的句号 + 斜杠,以及 Uri() 构造函数中的 'dontEscape' 选项,但到目前为止运气不好......

关于如何让这个字符串允许正确的 URL 的其他想法?

谢谢。

最佳答案

这是我用的:

    // 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)

关于c# - 在 URL 字符串中的斜杠前转义句号,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5634836/

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