28

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!

1

4 Answers 4

31

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();
        }
    }
}
1
  • didn't worked for me. I am making a windows forms application.
    – AZ_
    Mar 27, 2014 at 6:07
4

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

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/

-1

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.