带有BaseAddress的HttpClient
我在使用HttpClient和BaseAddress属性调用webHttpBinding WCF端点时遇到问题。
HttpClient的
我创建了一个HttpClient实例,将BaseAddress属性指定为本地主机端点。
GetAsync Call
然后我调用GetAsync方法传递额外的Uri信息。
HttpResponseMessage response = await client.GetAsync(string.Format("/Layouts/{0}", machineInformation.LocalMachineName()));
服务端点
[OperationContract] [WebGet(UriTemplate = "/Layouts/{machineAssetName}", ResponseFormat = WebMessageFormat.Json)] List GetLayouts(string machineAssetName);
问题
我遇到的问题是,BaseAddress的/AndonService.svc
部分被截断,因此结果调用转到https://localhost:44302/Layouts/1100-00277
而不是https://localhost:44302/AndonService.svc/Layouts/1100-00277
导致404 Not Found。
是否有理由在GetAsync调用中截断BaseAddress? 我该如何解决这个问题?
在BaseAddress
,只包括最后的斜杠: https://localhost:44302/AndonService.svc/
。 如果不这样做,路径的最后部分将被丢弃,因为它不被视为“目录”。
此示例代码说明了差异:
// No final slash var baseUri = new Uri("https://localhost:44302/AndonService.svc"); var uri = new Uri(baseUri, "Layouts/1100-00277"); Console.WriteLine(uri); // Prints "https://localhost:44302/Layouts/1100-00277" // With final slash var baseUri = new Uri("https://localhost:44302/AndonService.svc/"); var uri = new Uri(baseUri, "Layouts/1100-00277"); Console.WriteLine(uri); // Prints "https://localhost:44302/AndonService.svc/Layouts/1100-00277"