【问题标题】:Why HttpClient uses an incorrect requestUri in post request?为什么 HttpClient 在 post 请求中使用了不正确的 requestUri?
【发布时间】: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


【解决方案1】:

你的 Uri 必须以这样的斜线结尾:

  var url = new Uri("https://sandbox-api.alopeyk.com/");

这是对 HttpClient 的一个相当愚蠢的限制。

【讨论】:

    【解决方案2】:

    试试这个代码:

     HttpClient client = new HttpClient();
       client.BaseAddress = new Uri("https://sandbox-api.alopeyk.com");
       HttpResponseMessage response = client.PostAsync("api/v2/orders", new StringContent(jsonString, Encoding.UTF8, "text/json")).Result;  
                    if (response.IsSuccessStatusCode)
                    {
                        // Parse the response body. Blocking!
                       var  responseData = response.Content.ReadAsStringAsync().Result;                    
    
                    }
    

    【讨论】:

      【解决方案3】:

      你可以试试这段代码

      HttpResponseMessage response = null;
                  using (var client = new HttpClient())
                  {
                      using (var request = new HttpRequestMessage(HttpMethod.Post,"https://sandbox-api.alopeyk.com/api/v2/orders"))
                      {
                          request.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
                          request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", /*token herer*/);
                          var data = new StringContent(JsonConvert.SerializeObject(request, Encoding.UTF8, "application/json"));
      request.Content = data;
                          response = await client.SendAsync(request);
                      }
                  }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2017-06-05
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-10-18
        • 2011-03-30
        相关资源
        最近更新 更多