带基地址的 HttpClient

编程入门 行业动态 更新时间:2024-10-27 09:33:44
本文介绍了带基地址的 HttpClient的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我在调用 方法传入额外的 Uri 信息.

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

服务端点

[操作契约][WebGet(UriTemplate = "/Layouts/{machineAssetName}", ResponseFormat = WebMessageFormat.Json)]列表GetLayouts(string machineAssetName);

问题

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

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

解决方案

在 BaseAddress 中,只包含最后一个斜杠:localhost:44302/AndonService.svc/.如果不这样做,路径的最后一部分将被丢弃,因为它不被视为目录".

此示例代码说明了不同之处:

//没有最后的斜线var baseUri = new Uri("localhost:44302/AndonService.svc");var uri = new Uri(baseUri, "Layouts/1100-00277");Console.WriteLine(uri);//打印localhost:44302/Layouts/1100-00277"//最后的斜线var baseUri = new Uri("localhost:44302/AndonService.svc/");var uri = new Uri(baseUri, "Layouts/1100-00277");Console.WriteLine(uri);//打印localhost:44302/AndonService.svc/Layouts/1100-00277"

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

HttpClient

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

GetAsync Call

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

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

Service endpoint

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

Problem

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 localhost:44302/Layouts/1100-00277 rather that localhost:44302/AndonService.svc/Layouts/1100-00277 resulting in a 404 Not Found.

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

解决方案

In the BaseAddress, just include the final slash: 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".

This sample code illustrates the difference:

// No final slash var baseUri = new Uri("localhost:44302/AndonService.svc"); var uri = new Uri(baseUri, "Layouts/1100-00277"); Console.WriteLine(uri); // Prints "localhost:44302/Layouts/1100-00277" // With final slash var baseUri = new Uri("localhost:44302/AndonService.svc/"); var uri = new Uri(baseUri, "Layouts/1100-00277"); Console.WriteLine(uri); // Prints "localhost:44302/AndonService.svc/Layouts/1100-00277"

更多推荐

带基地址的 HttpClient

本文发布于:2023-11-07 09:07:02,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1566056.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:基地   HttpClient

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!