在基本层面上,我应该能够在
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));
}
}
}
输出
原来这件事好像跟它有关系……
在
BaseAddress
的末尾添加斜线即可解决该问题。
但是,对我来说,这似乎是
Uri
中的一个错误。我确信这会让很多人绊倒。
我创建了一个名为 Urls 的库来解决这个问题。网址是不可变的,您不必使用字符串来构造它们。
RestClient.Net 是使用这些 url 的 REST/HTTP 客户端库。您可以在此处阅读有关该问题的更多信息。