我在System.Uri类中遇到了意外的行为。当创建了一个System.Uri实例,并且UrlString具有一些模式,如...、...#或.#时,System.Uri将删除所有重复的.字符。
这很奇怪,但是我相信这个行为是基于RFC2396的。
当我尝试从以下网址下载HTML时,问题就开始了:http://www.submarino.com.br/produto/1/23853463/mundo+segundo+steve+jobs,+o:+as+frases+mais+inspiradoras+...
由于网站不能识别“新的.”,所以它重定向到原始的URL。然后抛出一个"System.Net.WebException: Too were redirections the“,该页面永远不会被访问。
我该如何解决这个问题?
发布于 2012-04-29 08:47:00
您可以使用反射来删除该特定属性。在您的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);
}
}
}这已经报告给Connect before了。
https://stackoverflow.com/questions/10368789
复制相似问题