locked
i cant use HttpClientFactory.Create on asp.net 4.6.1 RRS feed

  • Question

  • User-1379762086 posted

    Please can anybody help me on how i can use HttpClient client = HttpClientFactory.Create(customDelegatingHandler) in asp.net 4.61.

    Monday, July 20, 2020 4:32 PM

All replies

  • User1686398519 posted

    Hi abfrank,

    • You need to add Microsoft.Extensions.Http and Microsoft.Extensions.DependencyInjection through NuGet.
    • Please refer to the following examples for specific usage.
    using Microsoft.Extensions.DependencyInjection;
            public async Task<string> GetAsync()
            {
    
                var serviceProvider = new ServiceCollection().AddHttpClient().BuildServiceProvider();
                var httpClientFactory = serviceProvider.GetService<IHttpClientFactory>();
                var client = httpClientFactory.CreateClient();
                var response = await client.SendAsync(new HttpRequestMessage(HttpMethod.Get, "https://localhost:44346/api/values/get?id=1"));
                var content = await response.Content.ReadAsStringAsync();
                return content;
            }
            public string Get(int id)
            {
                return "value";
            }

    Here is the result.

    Best Regards,

    YihuiSun

    Tuesday, July 21, 2020 5:17 AM
  • User-1379762086 posted
    Thank you so much @ YihuiSun. Please how do i pass a HTTPClient Custom Handler, am trying to use the code i got from this article https://bitoftech.net/2014/12/15/secure-asp-net-web-api-using-api-key-authentication-hmac-authentication/.
     
    Regards
    Francis
    Tuesday, July 21, 2020 10:20 AM
  • User-474980206 posted

    instead of that old code, I'd use a JWT token as the payload instead. It well understood, and there are lots of libraries to encode and decode. They are just for bearer tokens

    Tuesday, July 21, 2020 7:42 PM
  • User1686398519 posted

    Hi abfrank,

    • According to the link you provided, I tested the code and found that "HttpClientFactory.Create" can be used. You can check whether you have quoted "using System.Net.Http;". The following is an example of my test, please refer to it.
    • You can get the code of CustomDelegatingHandler from the link you gave.
            public async Task<string> GetAsync()
            {
                //string apiBaseAddress = "https://localhost:44346/api/values/get?id=1";
                string apiBaseAddress = "https://localhost:44346/";
                CustomDelegatingHandler customDelegatingHandler = new CustomDelegatingHandler();
                var test = new Test23 { Content= "Test"};
                HttpClient client = HttpClientFactory.Create(customDelegatingHandler);
                HttpResponseMessage response = await client.PostAsJsonAsync(apiBaseAddress + "api/values/Post", test);
                //HttpResponseMessage response = await client.GetAsync(apiBaseAddress);
                string content = "";
                if (response.IsSuccessStatusCode)
                {
                    content = await response.Content.ReadAsStringAsync();
                }
                return content;
            }
            public IHttpActionResult Post(Test23 test)
            {
                return Ok("success");
            }

    Here is the result.

    Best Regards,

    YihuiSun

    Wednesday, July 22, 2020 7:52 AM