激情久久久_欧美视频区_成人av免费_不卡视频一二三区_欧美精品在欧美一区二区少妇_欧美一区二区三区的

服務器之家:專注于服務器技術及軟件下載分享
分類導航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術|正則表達式|C/C++|IOS|C#|Swift|Android|VB|R語言|JavaScript|易語言|vb.net|

服務器之家 - 編程語言 - ASP.NET教程 - .net core并發(fā)請求發(fā)送HttpWebRequest的坑解決

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

2020-06-06 16:00lee576 ASP.NET教程

這篇文章主要給大家介紹了關于.net core并發(fā)請求發(fā)送HttpWebRequest的坑的相關資料,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面來一起看看吧

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

?
1
ServicePointManager.DefaultConnectionLimit = 200;

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

在Core中的StartUp注冊一個HttpClient的名字

?
1
2
3
4
public void ConfigureServices(IServiceCollection services)
{
  services.AddHttpClient("HttpClientFactoryDemo");
}

然后在Controller中創(chuàng)建

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
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));
 
    }
  }
}

總結

以上就是這篇文章的全部內容了,希望本文的內容對大家的學習或者工作具有一定的參考學習價值,如果有疑問大家可以留言交流,謝謝大家對服務器之家的支持。

原文鏈接:https://blog.csdn.net/lee576/article/details/85007730

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 久久精品网站视频 | 777午夜精品视频在线播放 | 国产精品久久久久免费视频 | tube7xxx| 亚洲天堂ww| av免费在线观看国产 | 香蕉久草在线 | 国产精品1区,2区,3区 | 97精品视频在线观看 | 国产成人av免费看 | 国产精品久久久久网站 | 一级黄色免费大片 | 黄色av电影在线 | 韩国一级免费视频 | 91成人午夜性a一级毛片 | 男女视频免费看 | 国产精品久久二区 | av在线电影网址 | 香蕉黄色网 | 中文字幕22页 | 一区在线免费视频 | 黄色av免费网站 | 羞羞视频免费视频欧美 | 看一级毛片 | 欧美一级黄色录像片 | 欧美福利视频一区二区三区 | 狠狠婷婷综合久久久久久妖精 | 免费三级大片 | 国产91亚洲精品一区二区三区 | 亚洲精品tv久久久久久久久久 | 日本人乱人乱亲乱色视频观看 | 法国极品成人h版 | 成人午夜视频免费在线观看 | 姑娘第四集免费看视频 | av电影网站在线观看 | 欧美乱淫| 国产99精品 | 午夜视频免费在线观看 | av在线试看 | 成人毛片在线 | 91福利社在线|