.NET Httpclient 编码 url,但我不希望它

问题描述 投票:0回答:1

我有一个带有管道的网址 |象征。当我从 Httpclient 调用它时,它被编码为 %7C。

我读到 .NET 中的 Uri 上有一个“不要转义”,但现在已被标记为过时。

我能做些什么来做到这一点|请求中未编码?

我正在构建的网址如下:

HttpRequestMessage requestMessage = _httpClient.PrepareRequestMessage($"your/notstandardquerystringparam:term|otherterm", HttpMethod.Get, null, null);
var response = await _httpClient.SendAsync(requestMessage);

当 | 时,我调用的服务失败。符号正在被编码。我需要它作为实际 | 传递给服务符号

c# .net asp.net-core encoding httpclient
1个回答
0
投票

也许这是一篇有趣的文章,可能会有所帮助:使用 C# 进行 URL 编码

就我个人而言,我正在使用像

RestSharp
这样的库来执行此操作。

我不确定该管道需要放在哪里。在 URL 段或查询中?我假设以下示例中的查询参数,因为这是我可以想象您需要管道的唯一地方。

using var client = new RestClient("https://example.com/api");

var request = new RestRequest("your/endpoint");

// the last parameter encode=false will disable encoding
request.AddQueryParameter("queryname", "term|otherterm", false); 

var response = await client.GetAsync(request);

// GET https://example.com/api/your/endpoint?queryname=term|otherterm
© www.soinside.com 2019 - 2024. All rights reserved.