【问题标题】:HttpClient with BaseAddress带有 BaseAddress 的 HttpClient
【发布时间】:2013-12-16 10:54:23
【问题描述】:

我在使用HttpClientBaseAddress 属性调用webHttpBinding WCF 端点时遇到问题。

HttpClient

我创建了一个HttpClient 实例,将BaseAddress 属性指定为本地主机端点。

GetAsync 调用

然后我调用 GetAsync 方法,传入额外的 Uri 信息。

HttpResponseMessage response = await client.GetAsync(string.Format("/Layouts/{0}", machineInformation.LocalMachineName()));

服务端点

[OperationContract]
[WebGet(UriTemplate = "/Layouts/{machineAssetName}", ResponseFormat = WebMessageFormat.Json)]
List<LayoutsDto> GetLayouts(string machineAssetName);

问题

我遇到的问题是 BaseAddress 的 /AndonService.svc 部分被截断,因此结果调用转到 https://localhost:44302/Layouts/1100-00277 而不是 https://localhost:44302/AndonService.svc/Layouts/1100-00277 导致 404 Not Found。

BaseAddress 在 GetAsync 调用中被截断是否有原因?我该如何解决这个问题?

【问题讨论】:

标签: c# .net wcf dotnet-httpclient webhttpbinding


【解决方案1】:

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"

【讨论】:

猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-06-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-05-07
相关资源
最近更新 更多