You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

.NET 6中HttpClient无法复用SSL/TLS1.2会话,每次请求重握手

复用HttpClient仍触发重复SSL/TLS握手的问题

我按照网上通用方案创建了私有静态只读HttpClient并在整个应用中复用,但每次请求仍会触发SSL/TLS1.2握手,导致每个请求响应耗时近6秒。以下是我在.NET 6环境下分块向服务器POST字节数组的代码:

private static readonly HttpClient client = new HttpClient(new HttpClientHandler
{
    Credentials = new NetworkCredential("admin", "private"),
    ServerCertificateCustomValidationCallback = (message, cert, chain, errors) => 
    true,
    CheckCertificateRevocationList = true,
    PreAuthenticate = true,
    //UseCookies = true,
    //CookieContainer = cookieContainer,

});

private async void button1_Click(object sender, EventArgs e)
{
    //System.Net.ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12| SecurityProtocolType.Tls11 | SecurityProtocolType.Tls| SecurityProtocolType.Tls13;

    try
    {
        string http = "https://";
        string fwupdate = "/fwup_install";
        string fwreset = "/fwup_reset";
        string ip = txt_ip.Text;
        byte[] Data = null;
        byte[] array1 = null;
        string filePath = "D:\\fw\\iol_mix_mp_https.fwu";
        Data = File.ReadAllBytes(TxT_SelectedFwuFile.Text);
        var url = http + ip + fwupdate;   

        HttpContent content = new StringContent("size=" + Data.Length);

        var response = await client.PostAsync(url, content);

        richTextBox1.Text = richTextBox1.Text + ("Step1 : " + response.StatusCode);

        int sendbytes = 0;
        int Var = 1024 * 32;
        int Len = Convert.ToInt32(Data.Length / Var);
        int Counter = 0;

        while (Data.Length > sendbytes)
        {

            //Thread.Sleep(1000);
            if (Counter < Len)
            {
                array1 = new byte[Var];

                for (int i = 0; i < array1.Length; i++)
                {
                    if (sendbytes < Data.Length)
                    {
                        array1[i] = Data[sendbytes];
                        sendbytes++;
                    }
                    else
                    {
                        break;
                    }
                }
                Counter++;
                // textBox1.Text = Counter.ToString();
            }
            else
            {

                array1 = new byte[Data.Length - (Var * Len)];
                //textBox1.Text += "Remaining Array Length = " + array1.Length.ToString();
                for (int i = 0; i < array1.Length; i++)
                {
                    array1[i] = Data[sendbytes];
                    sendbytes++;
                }

            }

            ByteArrayContent Bytcontent = new ByteArrayContent(array1);
            Bytcontent.Headers.ContentType = new MediaTypeHeaderValue("application/octet-stream");

            var response1 = await client.PostAsync(url, Bytcontent);
        
            richTextBox1.Text = richTextBox1.Text + (response1.StatusCode);

        }
    }
    catch (Exception ex)
    {
        richTextBox1.Text = richTextBox1.Text + ex.Message;
        richTextBox1.Text = richTextBox1.Text + ex.InnerException;

    }
}

private void button2_Click(object sender, EventArgs e)
{
    OpenFileDialog ofd = new OpenFileDialog();

    ofd.Filter = "FWU file|*.fwu*";
    ofd.Title = "Load FWU file";
    if (ofd.ShowDialog() == DialogResult.OK)
    {
        TxT_SelectedFwuFile.Text = ofd.FileName;
    }
}

private void Form1_Load(object sender, EventArgs e)
{
    TxT_SelectedFwuFile.ReadOnly = true;
}

内容的提问来源于stack exchange,提问作者Aditya Desai

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.27 11:44:54