考虑以下代码,其中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");
}
我希望它执行一个GET
请求,访问http://something.com/api/resource/7
。但它没有。
搜索一番后,我找到了这个问题和答案:HttpClient with BaseAddress。建议在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 这里发生了什么?