最佳答案
考虑下面的代码,其中BaseAddress
定义了部分URI路径。
using (var handler = new HttpClientHandler())
using (var client = new HttpClient(handler))
{
client.BaseAddress = new Uri("http://something.com/api");
var response = await client.GetAsync("/resource/7");
}
我期望它对http://something.com/api/resource/7
执行GET
请求。但事实并非如此。
经过一番搜索,我找到了这个问题和答案:带有BaseAddress的HttpClient。建议将/
放在BaseAddress
的末尾。
using (var handler = new HttpClientHandler())
using (var client = new HttpClient(handler))
{
client.BaseAddress = new Uri("http://something.com/api/");
var response = await client.GetAsync("/resource/7");
}
还是不行。下面是文档:HttpClient。BaseAddress这里发生了什么?