国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > .NET > 正文

.net core并發(fā)請求發(fā)送HttpWebRequest的坑解決

2024-07-10 12:48:36
字體:
供稿:網(wǎng)友

在framework中,大量并發(fā) HttpWebRequest 需要設(shè)置一個(gè)最大連接數(shù)

ServicePointManager.DefaultConnectionLimit = 200;

但是在.net core中卻無效,因?yàn)閏ore不使用 ServicePointManager 管理連接數(shù),在core中只有使用HttpClient,HttpCilentFactory來管理連接數(shù),如果在core中使用 ServicePointManager 不但不起作用,并且大量并發(fā)使用 HttpWebRequest 會(huì)導(dǎo)致 IIS 直接假死,所以在core中,只能使用 HttpClient 和 HttpCilentFactory這一條路可走

在Core中的StartUp注冊一個(gè)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 Task<ActionResult> Get()    {      Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);      string xmlContent = "<?xml version=/"1.0/" encoding=/"GBK/" standalone=/"yes/"?><xml><version>1</version><ins_cd>08A9999999</ins_cd><mchnt_cd>0002900F0370588</mchnt_cd><term_id></term_id><random_str>93b4efa6d0d84808a76355ff0f7a178d</random_str><sign>G1+TBpyEVwsQjeJ9X7zrObRTFtI/ItuJWEEYl3AT/9XlFd844Jv2Wb/gNVkuEVP890Tf1Ub+EaTe1qByHSu97cpQr6riuDxqw2nnjKZBZsG00C1d8070sZPf4c1hkSUfhlR2nPn+7dvIanLCjRFzTgoTQ/WtcArrL/SJIJeaXYg=</sign><order_type>ALIPAY</order_type><goods_des>卡盟測試</goods_des><goods_detail></goods_detail><addn_inf></addn_inf><mchnt_order_no>2018121302054468584629</mchnt_order_no><curr_type></curr_type><order_amt>1</order_amt><term_ip>127.0.0.1</term_ip><txn_begin_ts>20181213020544</txn_begin_ts><goods_tag></goods_tag><auth_code>288232051781304899</auth_code><sence>1</sence><reserved_sub_appid></reserved_sub_appid><reserved_limit_pay></reserved_limit_pay></xml>";      xmlContent = UrlEncode(xmlContent, Encoding.GetEncoding("GBK"));       Dictionary<string, string> nvs = new Dictionary<string, string> { { "req", xmlContent } };      Encoding encoding = Encoding.GetEncoding("GBK");      StringBuilder buffer = new StringBuilder();      int i = 0;      IDictionary<string, string> sortedParams = new SortedDictionary<string, string>(nvs);      foreach (KeyValuePair<string, string> 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("https://spay.fuiou.com/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));     }  }}            
發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 三明市| 大连市| 观塘区| 桦甸市| 湖南省| 健康| 弥渡县| 从江县| 通化市| 海城市| 玉屏| 昌江| 车致| 宜丰县| 富民县| 无棣县| 寿光市| 鄂伦春自治旗| 阳春市| 洛宁县| 藁城市| 兴宁市| 张家港市| 板桥市| 峡江县| 民勤县| 鸡西市| 兴宁市| 万源市| 彰化市| 阿拉尔市| 濉溪县| 登封市| 肇庆市| 江川县| 攀枝花市| 屯昌县| 靖安县| 玛曲县| 衡南县| 太仓市|