HttpWebRequest to URL with dot at the end

2020-01-23 17:21发布

when i do a GET with WebRequest.Create("http://abc/test.") i get 404 because according to fiddler the trailing dot gets stripped away by .NET and the web server needs the dot. how can i prevent that or work around it. any workaround is appreciated!

4条回答
你好瞎i
2楼-- · 2020-01-23 17:58

Workaround in workaround tab at the official bug report:

https://connect.microsoft.com/VisualStudio/feedback/details/386695/system-uri-incorrectly-strips-trailing-dots?wa=wsignin1.0#tabs

.. seems to be valid. Basically, run this code to reset a static flag in .NET before working with System.Uri:

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

Demonstrated:

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();
        }
    }
}
查看更多
够拽才男人
3楼-- · 2020-01-23 17:59

Re-wrote some of it to a function that dont require you to add any namespaces

    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;
    }
查看更多
贪生不怕死
4楼-- · 2020-01-23 18:01

This is a known problem that has come up on the Microsoft forums a few times.

The Uri class incorrectly thinks that all URIs act like Windows disk files where a trailing dot (for no file extension) is not relevant.

http://social.msdn.microsoft.com/Forums/en-US/netfxbcl/thread/5206beca-071f-485d-a2bd-657d635239c9/

查看更多
Rolldiameter
5楼-- · 2020-01-23 18:02

you change the dot into String to Hex

  string.format("{0:x2}",yoururl);

i think it's useful for u, because i used it in twitter API Oauth formatting

查看更多
登录 后发表回答