. net Httpclient编码一个url,但我不希望它



我有一个带有管道|符号的url。当我从Httpclient调用它时,它被编码为%7C。

我读到在。net的Uri上有一个'不转义',但现在已经被标记为过时了。

我能做些什么使这个|不被编码在请求上吗?

我正在构建的url是这样的:

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

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

传递给服务

也许这是一个有趣的帖子,可能会有所帮助:使用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

最新更新