HttpClient 在某些情况下修改 BaseAddress [重复]

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

在基本层面上,我应该能够在

BaseAddress
上设置绝对
HttpClient
,然后使用相对
Uri
发送 Http 请求。期望相对的
Uri
将附加到基地址上以进行 Http 调用。大多数情况下它都会这样做。这个例子效果很好。

编辑:见底部。这在 Uris 的构造方式中似乎是奇怪的行为。

var httpClient = new HttpClient() { BaseAddress = new Uri("http://restcountries.eu/rest/", UriKind.Absolute) };
var requestMessage = new HttpRequestMessage(HttpMethod.Get, new Uri("v2", UriKind.Relative));
var httpResponseMessage = await httpClient.SendAsync(requestMessage);
var json = await httpResponseMessage.Content.ReadAsStringAsync();

Fiddler 网址:

获取http://restcountries.eu/rest/v2HTTP/1.1

我有一个本地 API。当我在本地 API 上运行等效项时,调用会截断部分基址(“Api”)。

var httpClient = new HttpClient() { BaseAddress = new Uri($"http://localhost/JsonPerson/Api", UriKind.Absolute) };
var requestMessage = new HttpRequestMessage(HttpMethod.Get, new Uri("GetApiPerson", UriKind.Relative));
var httpResponseMessage = await httpClient.SendAsync(requestMessage);
var json = await httpResponseMessage.Content.ReadAsStringAsync();

Fiddler 网址:

获取http://localhost/JsonPerson/GetApiPerson HTTP/1.1

为什么要删除“/Api”?

此代码访问本地 API 并且工作正常:

var httpClient = new HttpClient() { BaseAddress = new Uri($"http://localhost/JsonPerson/", UriKind.Absolute) };
var requestMessage = new HttpRequestMessage(HttpMethod.Get, new Uri("Api/GetApiPerson", UriKind.Relative));
var httpResponseMessage = await httpClient.SendAsync(requestMessage);
var json = await httpResponseMessage.Content.ReadAsStringAsync();

Fiddler 网址:

获取http://localhost/JsonPerson/Api/GetApiPerson HTTP/1.1

这是一个奇怪的问题,我不明白为什么会出现这些不一致的情况...对于本地和非本地 uri 是否以不同的方式处理 uri?我在这里缺少什么?

这是.NET Core 3.1

查看这段代码,然后查看输出。这就是

HttpClient
在幕后所做的事情。

using System;
using System.Threading.Tasks;

namespace ConsoleApp1
{
    class Program
    {
        static async Task Main(string[] args)
        {
            var baseAddress = new Uri($"http://restcountries.eu/JsonPerson/Api", UriKind.Absolute);
            var resourceUri = new Uri($"GetApiPerson", UriKind.Relative);
            Console.WriteLine(new Uri(baseAddress, resourceUri));

            baseAddress = new Uri($"http://restcountries.eu/JsonPerson", UriKind.Absolute);
            resourceUri = new Uri($"Api/GetApiPerson", UriKind.Relative);
            Console.WriteLine(new Uri(baseAddress, resourceUri));

            baseAddress = new Uri($"http://restcountries.eu/rest/", UriKind.Absolute);
            resourceUri = new Uri($"v2", UriKind.Relative);
            Console.WriteLine(new Uri(baseAddress, resourceUri));
        }
    }
}

输出

http://restcountries.eu/JsonPerson/GetApiPerson

http://restcountries.eu/Api/GetApiPerson

http://restcountries.eu/rest/v2

原来这件事好像跟它有关系……

https://github.com/microsoft/referencesource/blob/f461f1986ca4027720656a0c77bede9963e20b7e/System/net/System/UriExt.cs#L742

c# .net uri dotnet-httpclient
1个回答
5
投票

BaseAddress
的末尾添加斜线即可解决该问题。

但是,对我来说,这似乎是

Uri
中的一个错误。我确信这会让很多人绊倒。

我创建了一个名为 Urls 的库来解决这个问题。网址是不可变的,您不必使用字符串来构造它们。

RestClient.Net 是使用这些 url 的 REST/HTTP 客户端库。您可以在此处阅读有关该问题的更多信息。

© www.soinside.com 2019 - 2024. All rights reserved.