我有一个c#Web api,就像这样:
[HttpGet]
[Route("gatewayinfo/{endpointAddress}")]
public GatewayModel GetGatewayInfo(string endpointAddress)
当我打电话给api时,我需要传递一个网址作为地址(http://example.con)
如何使用HttpClient做到这一点?如果我把url放在参数中它就不会工作:
var client = new HttpClient();
client.SetBearerToken(token);
var result = await client.GetStringAsync(_webApiAddress + parameter);
答案 0 :(得分:3)
您需要对param
using (var client = new HttpClient())
{
client.BaseAddress = new Uri(_webapiAddress);
client.SetBearerToken(token)
var result = await client.GetStringAsync($"gatewayinfo/{HttpUtility.UrlEncode(parameter)}");
}