[英]How-to workaround differences with Uri and encoded URLs in .Net4.0 vs .Net4.5 using HttpClient
Uri
behaves differently in .Net4.0 vs .Net4.5 Uri
在.Net4.0与.Net4.5中的表现不同
var u = new Uri("http://localhost:5984/mycouchtests_pri/test%2F1");
Console.WriteLine(u.OriginalString);
Console.WriteLine(u.AbsoluteUri);
Outcome NET4.0 结果NET4.0
http://localhost:5984/mycouchtests_pri/test%2F1
http://localhost:5984/mycouchtests_pri/test/1
Outcome NET4.5 结果NET4.5
http://localhost:5984/mycouchtests_pri/test%2F1
http://localhost:5984/mycouchtests_pri/test%2F1
So when using the HttpClient
distributed by Microsoft via NuGet requests like the above fail with .Net4.0, since the HttpRequestMessage
is using the Uri
. 因此,当使用由微软通过NuGet分发的HttpClient
请求时,如上所述使用.Net4.0失败,因为HttpRequestMessage
正在使用Uri
。
Any ideas for a workaround? 任何解决方法的想法?
EDIT There is a NON APPLICABLE workaround by adding configuration for <uri>
in eg App.config
or Machine.config
( http://msdn.microsoft.com/en-us/library/ee656539(v=vs.110).aspx ). 编辑通过在例如App.config
或Machine.config
添加<uri>
配置,有一个非适用的解决方法( http://msdn.microsoft.com/en-us/library/ee656539(v=vs.110).aspx )。
<configuration>
<uri>
<schemeSettings>
<add name="http" genericUriParserOptions="DontUnescapePathDotsAndSlashes"/>
</schemeSettings>
</uri>
</configuration>
But as this is a tools library, that's not really an option. 但由于这是一个工具库,这不是一个真正的选择。 If the HttpClient
for .Net4.0 is supposed to be on par with the one in .Net4.5, they should have the same behavior. 如果.Net4.0的HttpClient
应该与.Net4.5中的HttpClient
相同,它们应该具有相同的行为。
Mike Hadlow wrote a blog post on this a few years back. 几年前,迈克哈德洛写了一篇博客文章 。 Here's the code he came up with to get round this: 这是他想出的代码来解决这个问题:
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});
}
I think it just sets the flag that's available from .config
in code, so while it's hacky, it's not exactly unsupported. 我认为它只是在代码中设置了.config
中可用的标志,所以虽然它很骇人听闻,但它并不完全不受支持。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.