为什么System.Net.Http HttpClient编码我的请求URL? [英] Why is System.Net.Http HttpClient encoding my request URL?

查看:275
本文介绍了为什么System.Net.Http HttpClient编码我的请求URL?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用System.Net.Http中的HttpClient向Web服务发出请求,如下所示:

I use the HttpClient in System.Net.Http to make requests to a web service as below:

using (var client = new HttpClient())
{
    using (var response = client.GetAsync(url).Result)
    {
        var result = response.Content.ReadAsStringAsync().Result;
    }
}

我有一个沙箱应用程序和一个实时应用程序.沙盒应用程序具有相同的代码(在共享存储库中),可以很好地工作,但是在实时应用程序中调用client.GetAsync(url).Result时,由于某种原因,Fiddler向我显示了所请求的URL已经编码,从而使请求混乱.

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.

请求的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

知道为什么吗?谢谢

N.B.我正在使用.NET Framework 4.5中Nuget的Microsoft.Net.Http库

推荐答案

  1. 请非常具体地回答您的问题:

  1. Please be very specific about your question:

  • 您使用Microsoft.Net.Http版本是什么?
  • 您在.NET版本下编译什么?

原来您是在.NET 4.0下编译的,这是我要说的错误,因为其行为与.NET Fx 4.5 System.Http

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

您可以通过在Uri类中将dontEscape设置为true来解决此问题:

You can fix it by setting dontEscape to true in the Uri class:

 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);

这篇关于为什么System.Net.Http HttpClient编码我的请求URL?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆