Asp.net core 学习笔记 ( HttpClient )
2018-09-18
core 2.1 之后有新的方案咯
http://www.talkingdotnet.com/3-ways-to-use-httpclientfactory-in-asp-net-core-2-1/
就是解决之前单列等等的问题.
给个例子
定义一个 service
public class ServerTaskHttpClient { public HttpClient Client { get; private set; } public ServerTaskHttpClient( HttpClient httpClient, IConfiguration configuration ) { httpClient.BaseAddress = new Uri(configuration["Origin"] + "/api"); httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue(MediaTypeNames.Application.Json));
// httpClient.DefaultRequestHeaders.Add(HeaderNames.ContentType, MediaTypeNames.Application.Json); //注意, 放这里是错的, content-type 不是这里 set 的,而是通过 content Client = httpClient; } }
startup.cs
加入依赖 providers
services.AddHttpClient<ServerTaskHttpClient>();
在 controller 调用
public class ResponseData { public bool successful { get; set; } } public async Task<IActionResult> Index() { var content = new StringContent(JsonConvert.SerializeObject(new { name = "dada" }), Encoding.UTF8, MediaTypeNames.Application.Json); var result = await ServerTaskHttpClient.Client.PostAsync("/api/debug/clearImage", content); if (result.IsSuccessStatusCode) { var responseData = JsonConvert.DeserializeObject<ResponseData>(await result.Content.ReadAsStringAsync()); } return View(); }
api
[HttpPost("clearImage")] public async Task<IActionResult> ClearImage(Data data) { return Ok(new { successful = true }); }
2017-09-25
core 和 .net 4.5 一样都可以使用 HttpClient
没什么特别的, 只是在学习的时候发现一个之前没留意到的问题.
refer :
https://aspnetmonsters.com/2016/08/2016-08-27-httpclientwrong/
http://byterot.blogspot.my/2016/07/singleton-httpclient-dns.html
http://www.cnblogs.com/dudu/p/csharp-httpclient-attention.html
https://docs.microsoft.com/en-us/aspnet/web-api/overview/advanced/calling-a-web-api-from-a-net-client
http://www.cnblogs.com/Wddpct/p/6229090.html
大致上说的是, HttpClient 不要使用 using, 因为它并不会释放 TCP 连接, 应该在整个应用里把 HttpClient 设置成单列
core 要设置单列对象很容易
public class VisautoHttpClient : HttpClient { public VisautoHttpClient() { BaseAddress = new Uri("http://visauto.stooges.com.my"); Timeout = TimeSpan.FromSeconds(30); DefaultRequestHeaders.Clear(); DefaultRequestHeaders.Add("Accept", "text/html"); DefaultRequestHeaders.Connection.Add("Keep-Alive"); } }
定义好类, 由于每个 TCP 的 baseAddress 不能换的,所以如果你有很多个不同区的请求要发, 那么建议开多几个 HttpClient 实例.
keep-alive 的目的是让服务端知道我们的连接不要马上断开, 这样发多个请求的话可以快一些.
然后在 startup.cs 提供服务就可以了.
public void ConfigureServices(IServiceCollection services) { // services services.AddSingleton<VisautoHttpClient, VisautoHttpClient>(); }
core 会帮我们搞定 singleton 的线程安全等问题. 不需要像从前还要用 lock 什么的那么麻烦了.
而且这里只是定义,一直到控制器注入时才会初始化实例哦.
在控制器注入了就可以发请求咯~
[Area("Web")] public class HttpController : Controller { public HttpController( VisautoHttpClient httpClient ) { this.httpClient = httpClient; } private HttpClient httpClient { get; set; } [Route("http")] public async Task<IActionResult> Index() { try { string x = await httpClient.GetStringAsync(""); } catch (Exception ex) { } return View(); } }
note : 如果服务器 restart, 我们的连接并不需要任何处理, 都是自动的. 放心, dns 的问题我没有遇到, 遇到了才打算呗.