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

编程入门 行业动态 更新时间:2024-10-27 09:35:38
本文介绍了为什么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库

推荐答案

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

  • 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(@"google/advert?paginate=1&page=1&language=en&filters[updated_at][ge]=2016-03-21%2012:19:05", dontEscape: true);
  • 更多推荐

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

    本文发布于:2023-11-05 07:19:40,感谢您对本站的认可!
    本文链接:https://www.elefans.com/category/jswz/34/1560238.html
    版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
    本文标签:Net   System   Http   URL   HttpClient

    发布评论

    评论列表 (有 0 条评论)
    草根站长

    >www.elefans.com

    编程频道|电子爱好者 - 技术资讯及电子产品介绍!