【发布时间】:2018-08-26 10:43:51
【问题描述】:
当我使用HttpClient 类向 API URL 发送 POST 请求时,它会修改我传递给它的 URL。例如,当我使用主 API URL 时,RequestUri 不正确,我收到未找到的响应。当我在 URL 中使用 api 字时会出现此问题!
概念:
The Incorrect, modified URL:
Url: https://sandbox-api.alopeyk.com/api/v2/order
Request Url: https://sandbox-api.alopeyk.com
The Correct, and expected URL (This is the one I specify)
Url: https://google.com/api/v2/order
Request Url: https://google.com/api/v2/order
代码:
public async Task<CreateOrderResponse> CreateOrderAsync(CreateOrderRequest request)
{
var endPoint = EndPointFactory<CreateOrderResponse>.Build(HttpMethod.Post);
var jsonString = JsonConvert.SerializeObject(request);
var url = new Uri("https://sandbox-api.alopeyk.com");
var encodedFrom = new StringContent(jsonString);
var httpClient = endPoint.GetHttpClient(url);
var httpResponse = await httpClient.PostAsync("api/v2/orders", encodedFrom).ConfigureAwait(false);
// when use api it's https://sandbox-api.alopeyk.com it should be https://sandbox-api.alopeyk.com/api/v2/orders
// when use other host name for example it's correct
var requesturl = httpResponse.RequestMessage.RequestUri;
return await httpResponse.Content.ReadAsAsync<CreateOrderResponse>().ConfigureAwait(false);
}
// in the EndPoint class
public HttpClient GetHttpClient(Uri url)
{
return new Http.HttpClientFactory().GetOrCreate(Url, Headers);
}
如果你想看HttpClientFactory,那就是here。
HttpClient 我的主主机名有问题,它是https://sandbox-api.alopeyk.com
【问题讨论】:
-
问题解决了吗?
-
@danvasiloiu 是的。
-
解决方案是?
-
@danvasiloiu API 服务器被重定向到另一个页面:D
标签: c# httprequest httpclient