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
.NET forums are moving to a new home on Microsoft Q&A, we encourage you to go to Microsoft Q&A for .NET for posting new questions and get involved today.
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
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
.NET forums are moving to a new home on Microsoft Q&A, we encourage you to go to Microsoft Q&A for .NET for posting new questions and get involved today.
Member
6 Points
96 Posts
i cant use HttpClientFactory.Create on asp.net 4.6.1
Jul 20, 2020 04:32 PM|abfrank|LINK
Please can anybody help me on how i can use HttpClient client = HttpClientFactory.Create(customDelegatingHandler) in asp.net 4.61.
Contributor
2710 Points
777 Posts
Re: i cant use HttpClientFactory.Create on asp.net 4.6.1
Jul 21, 2020 05:17 AM|YihuiSun|LINK
Hi abfrank,
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
Member
6 Points
96 Posts
Re: i cant use HttpClientFactory.Create on asp.net 4.6.1
Jul 21, 2020 10:20 AM|abfrank|LINK
All-Star
58184 Points
15655 Posts
Re: i cant use HttpClientFactory.Create on asp.net 4.6.1
Jul 21, 2020 07:42 PM|bruce (sqlwork.com)|LINK
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
Contributor
2710 Points
777 Posts
Re: i cant use HttpClientFactory.Create on asp.net 4.6.1
Jul 22, 2020 07:52 AM|YihuiSun|LINK
Hi abfrank,
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