本篇文章给大家分享的是有关如何在.net core中使用HttpWebRequest发送并发请求,小编觉得挺实用的,因此分享给大家学习,希望大家阅读完这篇文章后可以有所收获,话不多说,跟着小编一起来看看吧。
创新互联建站为企业级客户提高一站式互联网+设计服务,主要包括网站建设、做网站、成都app软件开发、小程序开发、宣传片制作、LOGO设计等,帮助客户快速提升营销能力和企业形象,创新互联各部门都有经验丰富的经验,可以确保每一个作品的质量和创作周期,同时每年都有很多新员工加入,为我们带来大量新的创意。在framework中,大量并发 HttpWebRequest 需要设置一个较大连接数
ServicePointManager.DefaultConnectionLimit = 200;
但是在.net core中却无效,因为core不使用 ServicePointManager 管理连接数,在core中只有使用HttpClient,HttpCilentFactory来管理连接数,如果在core中使用 ServicePointManager 不但不起作用,并且大量并发使用 HttpWebRequest 会导致 IIS 直接假死,所以在core中,只能使用 HttpClient 和 HttpCilentFactory这一条路可走
在Core中的StartUp注册一个HttpClient的名字
public void ConfigureServices(IServiceCollection services) { services.AddHttpClient("HttpClientFactoryDemo"); }
然后在Controller中创建
using System; using System.Collections.Generic; using System.Net.Http; using System.Net.Http.Headers; using System.Text; using System.Threading.Tasks; using System.Web; using Microsoft.AspNetCore.Mvc; namespace HttpClientFactoryDemo.Controllers { [Route("api/[controller]")] [ApiController] public class ValuesController : ControllerBase { private readonly IHttpClientFactory _httpClientFactory; public ValuesController(IHttpClientFactory httpClientFactory) { _httpClientFactory = httpClientFactory; } public static string UrlEncode(string temp, Encoding encoding) { StringBuilder stringBuilder = new StringBuilder(); for (int i = 0; i < temp.Length; i++) { string t = temp[i].ToString(); string k = HttpUtility.UrlEncode(t, encoding); if (t == k) { stringBuilder.Append(t); } else { stringBuilder.Append(k.ToUpper()); } } return stringBuilder.ToString(); } [HttpGet] public async TaskGet() { Encoding.RegisterProvider(CodePagesEncodingProvider.Instance); string xmlContent = " "; xmlContent = UrlEncode(xmlContent, Encoding.GetEncoding("GBK")); Dictionary 1 08A9999999 0002900F0370588 93b4efa6d0d84808a76355ff0f7a178d G1+TBpyEVwsQjeJ9X7zrObRTFtI/ItuJWEEYl3AT/9XlFd844Jv2Wb/gNVkuEVP890Tf1Ub+EaTe1qByHSu97cpQr6riuDxqw2nnjKZBZsG00C1d8070sZPf4c1hkSUfhlR2nPn+7dvIanLCjRFzTgoTQ/WtcArrL/SJIJeaXYg= ALIPAY 卡盟测试 2018121302054468584629 1 127.0.0.1 20181213020544 288232051781304899 1 nvs = new Dictionary { { "req", xmlContent } }; Encoding encoding = Encoding.GetEncoding("GBK"); StringBuilder buffer = new StringBuilder(); int i = 0; IDictionary sortedParams = new SortedDictionary (nvs); foreach (KeyValuePair kvp in nvs) { buffer.AppendFormat(i > 0 ? "&{0}={1}" : "{0}={1}", kvp.Key, UrlEncode(kvp.Value, Encoding.GetEncoding("GBK"))); i++; } byte[] postBody = encoding.GetBytes(buffer.ToString()); var client = _httpClientFactory.CreateClient("HttpClientFactoryDemo"); var request = new HttpRequestMessage { RequestUri = new Uri("/tupian/20230522/commonQuery"), Method = HttpMethod.Post, Content = new ByteArrayContent(postBody), }; request.Content.Headers.ContentType = new MediaTypeHeaderValue("application/x-www-form-urlencoded"); return Ok(await client.SendAsync(request)); } } }
以上就是如何在.net core中使用HttpWebRequest发送并发请求,小编相信有部分知识点可能是我们日常工作会见到或用到的。希望你能通过这篇文章学到更多知识。更多详情敬请关注创新互联行业资讯频道。