开发者

Escape fullstop before slash in URL string

I've found an oddity I'm trying to get around with C# sanitizing / interfering with a specified URL, and giving a 404 as a result.

It happens where there's a forward slash after a full-stop - C# and Visual Studio are determined to remove the fullstop.

For Visual Studio, this is the Control-click from a string linking to the mal-formed URL

In compiled C# the string is transformed, even before getting to new URI().

The link is:

http://www.mattmulholland.co.nz/Matt_Mulholla开发者_JS百科nd/Matt_Mulholland_-_Official_Website._Boom./rss.xml

(It's the './' at the end that causes the issue)

I've tried escaping both the fullstop + slash in ASCII, and the 'dontEscape' option in the Uri() constructor, but no luck so far...

And other thoughts as to how I can get this string to allow the correct URL?

Thanks.


This is what i use:

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

This solution is found here: HttpWebRequest to URL with dot at the end (.NET 2.0 Framework)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜