[英]Why is System.Net.Http HttpClient encoding my request URL?
I use the HttpClient in System.Net.Http to make requests to a web service as below: 我使用System.Net.Http中的HttpClient向Web服务发出请求,如下所示:
using (var client = new HttpClient())
{
using (var response = client.GetAsync(url).Result)
{
var result = response.Content.ReadAsStringAsync().Result;
}
}
I have a sandbox application and a live application. 我有一个沙箱应用程序和一个实时应用程序。 The sandbox application has identical code (in a shared repository) which works fine, but when client.GetAsync(url).Result
is called in the live application, for some reason Fiddler shows me that the requested URL has been encoded which messes the request up. 沙盒应用程序具有相同的代码(在共享存储库中),可以很好地工作,但是当在实时应用程序中调用client.GetAsync(url).Result
时,由于某种原因,Fiddler向我显示了所请求的URL已被编码,从而使请求混乱起来
Requested URL is supposed to look like this: 请求的网址应如下所示:
/advert?paginate=1&page=1&language=en&filters[updated_at][ge]=2016-03-21%2012:19:05
But ends up looking like this: 但最终看起来像这样:
/advert?paginate=1&page=1&language=en&filters%5Bupdated_at%5D%5Bge%5D=2016-03-21%2012:19:05
Any idea why? 知道为什么吗? Thanks 谢谢
NB Im using the Microsoft.Net.Http library from Nuget in .NET Framework 4.5 NB Im在.NET Framework 4.5中使用Nuget的Microsoft.Net.Http库
Please be very specific about your question: 请非常具体地回答您的问题:
Turned out that you compile under .NET 4.0 and this is a bug I would say, because the behavior is not identical to the .NET Fx 4.5 System.Http 原来您是在.NET 4.0下编译的,这是我要说的错误,因为其行为与.NET Fx 4.5 System.Http不同
You can fix it by setting dontEscape
to true in the Uri class: 您可以通过在Uri类dontEscape
设置为true来解决此问题:
var url = new Uri(@"http://google.com/advert?paginate=1&page=1&language=en&filters[updated_at][ge]=2016-03-21%2012:19:05", dontEscape: true);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.