简体   繁体   English

为什么System.Net.Http HttpClient编码我的请求URL?

[英]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库

  1. Please be very specific about your question: 请非常具体地回答您的问题:

    • you use Microsoft.Net.Http version what? 您使用Microsoft.Net.Http版本是什么?
    • you compile under .NET version what? 您在.NET版本下编译什么?
  2. 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.

相关问题 使用System.Net.Http时HttpClient错误 - HttpClient error when using System.Net.Http 为什么我不能在System.Net.Http引用的解决方案中使用System.Net.Http包? - Why I can not use System.Net.Http package in a solution with System.Net.Http reference? System.Net.Http 中的发布请求是否有 HTTP 完成选项? - Is there a HTTP Completion Option for a post request in System.Net.Http? 分发System.Net.Http - Distributing System.Net.Http 尝试使用 System.Net.Http 向 API 发出请求,但出现错误 - Trying to make a request to an API with System.Net.Http but getting errors System.Net.Http 的绑定重定向不起作用 - 为什么? - Binding redirect for System.Net.Http doesn't work - why? 即使安装了.NET v.4.0,HttpClient也会导致找不到“ System.Net.Http”程序集异常 - HttpClient causes 'System.Net.Http' assembly not found exception even though .NET v.4.0 is installed 单元测试在System.Net.Http v4中使用HttpClient的类 - Unit test a class that uses HttpClient in System.Net.Http v4 HttpClient GetAsync 使用 System.Net.Http nuget 包抛出 ArgumentNullException - HttpClient GetAsync throws ArgumentNullException using System.Net.Http nuget package @inject HttpClient 与 @using System.Net.Http 之间的问题; 在查看页面 - Question Between @inject HttpClient vs @using System.Net.Http; on view page
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM