Uri
.Net4.0 与 .Net4.5 中的行为不同
var u = new Uri("http://localhost:5984/mycouchtests_pri/test%2F1");
Console.WriteLine(u.OriginalString);
Console.WriteLine(u.AbsoluteUri);
成果NET4.0
http://localhost:5984/mycouchtests_pri/test%2F1
http://localhost:5984/mycouchtests_pri/test/1
结果NET4.5
http://localhost:5984/mycouchtests_pri/test%2F1
http://localhost:5984/mycouchtests_pri/test%2F1
所以当使用HttpClient
由 Microsoft 通过 NuGet 分发 https://www.nuget.org/packages/Microsoft.Net.Http/类似上面的请求在 .Net4.0 中会失败,因为HttpRequestMessage
正在使用Uri
.
有解决方法的想法吗?
EDIT有a 不适用解决方法是添加配置<uri>
例如App.config
or Machine.config
(http://msdn.microsoft.com/en-us/library/ee656539(v=vs.110).aspx http://msdn.microsoft.com/en-us/library/ee656539(v=vs.110).aspx).
<configuration>
<uri>
<schemeSettings>
<add name="http" genericUriParserOptions="DontUnescapePathDotsAndSlashes"/>
</schemeSettings>
</uri>
</configuration>
但由于这是一个工具库,因此这并不是一个真正的选择。如果HttpClient
因为 .Net4.0 应该与 .Net4.5 中的相同,因此它们应该具有相同的行为。
迈克·哈德洛写道关于此的博客文章 http://mikehadlow.blogspot.co.uk/2011/08/how-to-stop-systemuri-un-escaping.html几年前。这是他为解决这个问题而想出的代码:
private void LeaveDotsAndSlashesEscaped()
{
var getSyntaxMethod =
typeof (UriParser).GetMethod("GetSyntax", BindingFlags.Static | BindingFlags.NonPublic);
if (getSyntaxMethod == null)
{
throw new MissingMethodException("UriParser", "GetSyntax");
}
var uriParser = getSyntaxMethod.Invoke(null, new object[] { "http" });
var setUpdatableFlagsMethod =
uriParser.GetType().GetMethod("SetUpdatableFlags", BindingFlags.Instance | BindingFlags.NonPublic);
if (setUpdatableFlagsMethod == null)
{
throw new MissingMethodException("UriParser", "SetUpdatableFlags");
}
setUpdatableFlagsMethod.Invoke(uriParser, new object[] {0});
}
我认为它只是设置了可用的标志.config
在代码中,所以虽然它很hacky,但并不是完全不受支持。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)