HttpWebRequest к URL с точкой в конце

select type, craft, sum(nvl(regular,0) + nvl(overtime,0)) as total_hours
from hours_t
group by type, craft
order by type, craft
26
задан Chad Birch 13 May 2009 в 09:05
поделиться

2 ответа

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/

0
ответ дан 28 November 2019 в 07:47
поделиться

Временное решение на вкладке временного решения в официальном отчете об ошибке:

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

.. вроде верны. По сути, запустите этот код, чтобы сбросить статический флаг в .NET перед работой с 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);
        }
    }
}

Демонстрация:

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();
        }
    }
}
30
ответ дан 28 November 2019 в 07:47
поделиться
Другие вопросы по тегам:

Похожие вопросы: