WebRequest.Create(" http://abc/test .") で GET を実行すると、フィドラーによると末尾のドットが .NET によって取り除かれ、Web サーバーがドットを必要とするため、404 が返されます。どうすればそれを防ぐか、回避できますか。どんな回避策も大歓迎です!
10309 次
4 に答える
30
公式バグレポートの[回避策]タブの回避策:
..有効なようです。基本的に、System.Uriを操作する前に、このコードを実行して.NETの静的フラグをリセットします。
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();
}
}
}
于 2010-02-17T23:44:47.427 に答える
4
その一部を、名前空間を追加する必要のない関数に書き直しました
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;
}
于 2015-07-22T09:46:44.473 に答える
0
これは、Microsoft フォーラムで何度か取り上げられている既知の問題です。
このUri
クラスは、すべての URI が Windows ディスク ファイルのように機能し、末尾のドット (ファイル拡張子なし) が関係ないと誤って認識しています。
http://social.msdn.microsoft.com/Forums/en-US/netfxbcl/thread/5206beca-071f-485d-a2bd-657d635239c9/
于 2009-10-13T00:20:03.520 に答える
-1
ドットを文字列から16進数に変更します
string.format("{0:x2}",yoururl);
Twitter API Oauth フォーマットで使用したので、役に立つと思います。
于 2012-12-29T10:18:01.347 に答える