本篇文章給大家分享的是有關(guān)如何在.net core中使用HttpWebRequest發(fā)送并發(fā)請求,小編覺得挺實用的,因此分享給大家學(xué)習(xí),希望大家閱讀完這篇文章后可以有所收獲,話不多說,跟著小編一起來看看吧。
創(chuàng)新互聯(lián)建站為企業(yè)級客戶提高一站式互聯(lián)網(wǎng)+設(shè)計服務(wù),主要包括網(wǎng)站建設(shè)、做網(wǎng)站、成都app軟件開發(fā)、小程序開發(fā)、宣傳片制作、LOGO設(shè)計等,幫助客戶快速提升營銷能力和企業(yè)形象,創(chuàng)新互聯(lián)各部門都有經(jīng)驗豐富的經(jīng)驗,可以確保每一個作品的質(zhì)量和創(chuàng)作周期,同時每年都有很多新員工加入,為我們帶來大量新的創(chuàng)意。在framework中,大量并發(fā) HttpWebRequest 需要設(shè)置一個較大連接數(shù)
ServicePointManager.DefaultConnectionLimit = 200;
但是在.net core中卻無效,因為core不使用 ServicePointManager 管理連接數(shù),在core中只有使用HttpClient,HttpCilentFactory來管理連接數(shù),如果在core中使用 ServicePointManager 不但不起作用,并且大量并發(fā)使用 HttpWebRequest 會導(dǎo)致 IIS 直接假死,所以在core中,只能使用 HttpClient 和 HttpCilentFactory這一條路可走
在Core中的StartUp注冊一個HttpClient的名字
public void ConfigureServices(IServiceCollection services) { services.AddHttpClient("HttpClientFactoryDemo"); }
然后在Controller中創(chuàng)建
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發(fā)送并發(fā)請求,小編相信有部分知識點可能是我們?nèi)粘9ぷ鲿姷交蛴玫降摹OM隳芡ㄟ^這篇文章學(xué)到更多知識。更多詳情敬請關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道。