简体   繁体   English

为什么HttpClient在发布请求中使用了不正确的requestUri?

[英]Why HttpClient uses an incorrect requestUri in post request?

When I use HttpClient class to send a POST request to an API URL, it modifies the URL that I've passed to it. 当我使用HttpClient类将POST请求发送到API URL时,它会修改我传递给它的URL。 For example, when I use the main API URL the RequestUri is incorrect and I receive the not found response. 例如,当我使用主API URL时, RequestUri不正确,并且收到未找到的响应。 This problem happens when I use api word in the URL !! 当我在URL中使用api单词时,会发生此问题!

Concept: 概念:

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

Code: 码:

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);
 }

If you want to see HttpClientFactory it's here . 如果您想查看HttpClientFactory则在这里

The HttpClient have a problem with my main hostname that it's https://sandbox-api.alopeyk.com HttpClient主主机名有一个问题,即https://sandbox-api.alopeyk.com

Your Uri must end with a slash like this: 您的Uri必须以这样的斜杠结尾:

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

That's a rather silly restriction of HttpClient. 这是对HttpClient的相当愚蠢的限制。

Try this code: 试试这个代码:

 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;                    

                }

You can try with this code 您可以尝试使用此代码

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);
                }
            }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 C# NetCore WebAPI 集成测试:HttpClient 使用 HTTPS 进行 GET 请求,但随后仅使用 HTTP 进行 POST 请求,并失败并出现 Forbidden - C# NetCore WebAPI integration testing: HttpClient uses HTTPS for GET request, but then uses only HTTP for the POST request, and fails with Forbidden HttpWebRequest到HttpClient发布请求 - HttpWebRequest to HttpClient post Request 取消 &#39;HttpClient&#39; POST 请求 - Cancel an 'HttpClient' POST request WinRT HttpClient,POST请求 - WinRT HttpClient, POST request 为什么.Net HttpClient发布请求缓慢(当提琴手未运行时)? - Why .Net HttpClient post request is slow (when fiddler is not running)? C# 带有自定义标头的 HttpClient POST 请求发送不正确的 Content-Type header 字段 - C# HttpClient POST Request with Custom Headers sends incorrect Content-Type header field ASP.NET MVC单元测试WEB API Post方法:Request.RequestUri抛出异常 - ASP.NET MVC unit testing WEB API Post method: Request.RequestUri throws an exception 带有Json正文的HttpClient Post请求 - HttpClient Post request with Json body 使用 HttpClient 从请求正文发布 - Post from request body with HttpClient 具有标题和正文的HTTPClient发布请求 - HTTPClient Post Request with Headers and Body
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM