在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 Task<ActionResult> Get()
    {
      Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);
      string xmlContent = "<"1.0\" encoding=\"GBK\" standalone=\"yes\"";
      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));
 
    }
  }
}

总结

以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,如果有疑问大家可以留言交流,谢谢大家对的支持。

标签:
.net,core,并发请求,.net,core,发送httpwebrequest,net,core,坑

免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
桃源资源网 Design By www.nqtax.com