请考虑以下代码,其中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");
}
Run Code Online (Sandbox Code Playgroud)
我希望这可以执行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");
}
Run Code Online (Sandbox Code Playgroud)
它仍然无法正常工作.这是文档:HttpClient.BaseAddress这里发生了什么?
根据这个
https://msdn.microsoft.com/en-us/library/system.net.http.httpclient.baseaddress(v=vs.118).aspx
它包含基地址。嗯,呃……
我不知道微软的文档是否可以理解。基地址到底是什么?
是只有主机名的 URL 吗?
喜欢StackOverflow.com?
是整个网址吗stackoverflow.com/question/ask
是否是包含参数的整个URL,例如stackoverflow.com/hello/world?dfdsdf=34fgdsg
什么是基地址?