HttpClient的与BaseAddress [英] HttpClient with BaseAddress

查看:353
本文介绍了HttpClient的与BaseAddress的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个问题,在联络<一个href="http://msdn.microsoft.com/en-us/library/system.servicemodel.webhttpbinding%28v=vs.110%29.aspx">webHttpBinding使用WCF终点<一href="http://msdn.microsoft.com/en-us/library/system.net.http.httpclient%28v=vs.110%29.aspx">HttpClient而<一href="http://msdn.microsoft.com/en-us/library/system.net.http.httpclient.baseaddress%28v=vs.110%29.aspx">BaseAddress属性。

I have a problem calling a webHttpBinding WCF end point using HttpClient and the BaseAddress property.

的HttpClient

我创建了一个<一个href="http://msdn.microsoft.com/en-us/library/system.net.http.httpclient%28v=vs.110%29.aspx">HttpClient例如指定<一href="http://msdn.microsoft.com/en-us/library/system.net.http.httpclient.baseaddress%28v=vs.110%29.aspx">BaseAddress财产作为本地主机终结点。

I created a HttpClient instance specifying the BaseAddress property as a local host endpoint.

GetAsync呼叫

我随后致电 GetAsync 方法传入另外乌里inforamtion。

I then call the GetAsync method passing in the additional Uri inforamtion.

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

服务端点

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

问题

我遇到的问题是,是, /AndonService.svc 的BaseAddress的部分被​​截断,因此所产生的呼叫转到 HTTPS ://本地主机:44302 /设计/ 1100-00277 宁可的https://本地主机:44302 / AndonService.svc /设计/ 1100-00277 造成404未找​​到。

The problem I am having is that the is that /AndonService.svc part of the BaseAddress is being truncated so the resultant call goes to https://localhost:44302/Layouts/1100-00277 rather that https://localhost:44302/AndonService.svc/Layouts/1100-00277 resulting in a 404 Not Found.

有没有对BaseAddress被截断在GetAsync呼叫的原因是什么?我该如何解决这个问题?

Is there a reason the BaseAddress is being truncated in the GetAsync call? How do I get around this?

推荐答案

BaseAddress ,只包括最后的斜线: https://开头本地主机:44302 / AndonService.svc / 。如果不这样做,路径的最后部分被丢弃,因为它不被认为是一个目录。

In the BaseAddress, just include the final slash: https://localhost:44302/AndonService.svc/. If you don't, the final part of the path is discarded, because it's not considered to be a "directory".

此示例code说明了区别:

This sample code illustrates the difference:

// 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"

这篇关于HttpClient的与BaseAddress的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆