HttpWebRequest以URL结尾
当我用WebRequest.Create(“ http:// abc / test 。”)做一个GET的时候,我得到了404,因为根据fiddler,尾随的点被.NET剥离,Web服务器需要点。 我怎样才能防止或解决它。 任何解决方法表示赞赏!
官方错误报告的解决方法选项卡中的解决方法:
似乎是有效的。 基本上,在使用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); } } }
certificate:
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(); } } }
重新写一些它不需要你添加任何命名空间的函数
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; }
这是几次在微软论坛上提出的已知问题。
Uri
类错误地认为所有的URI都像Windows磁盘文件一样,后面的点(没有文件扩展名)是不相关的。
http://social.msdn.microsoft.com/Forums/en-US/netfxbcl/thread/5206beca-071f-485d-a2bd-657d635239c9/
您将点更改为string为hex
的String.Format( “{0:X2}”,yoururl);
我认为这对你有用,因为我在Twitter API Oauth格式中使用它