考虑以下代码,其中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
。但事实并非如此。
经过一番搜索,我找到了这个问题和答案:具有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这是怎么回事?