Url以.(点)结尾,在使用httpwebrequest读取的时候,微软会有一个bug……

解决方法在此,不重复做赘述,传送门:http://stackoverflow.com/questions/856885/httpwebrequest-to-url-with-dot-at-the-end

解决方法:

        /// <summary>
        /// 传递待处理Url进行处理
        /// </summary>
        /// <param name="oldUrl">待处理url</param>
        /// <returns>处理后的Url</returns>
        public static string UrlFix(string oldUrl) { 
            var url = new Uri(oldUrl);
            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);
                        if ((flagsValue & 0x1000000) != 0)
                            flagsField.SetValue(parser, flagsValue & ~0x1000000);
                    }
                }
            }
            url = new Uri(oldUrl);
            return url.ToString();
        }    

 

另外此在.net4.5中bug已解决!

 

posted @ 2014-12-02 22:58  SpringRen  阅读(266)  评论(0编辑  收藏  举报