0

I use "ServicePointManager" to fix the problem "The request was aborted : could not create SSL/TLS seccure channel".

try
{
   // ServicePointManager.Expect100Continue = true;
   // ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
   //
   // or this
   //
   // ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3 | SecurityProtocolType.Tls | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls12;
   
   var client = new WebClient();
   var str = client.DownloadString(url);
   textBox1.Text = str;
}catch (Exception ex){
   MessageBox.Show(ex.Message);
}

but my project freeze/hangs. Can someone help me?

Kolimondi
  • 87
  • 1
  • 5

1 Answers1

0

Try using async. This works fine for me.

using System;
using System.Net;
using System.Windows.Forms;

namespace WindowsFormsApp1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            textBox1.Multiline = true;
            ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
        }

        private async void button1_Click(object sender, EventArgs e)
        {
            try
            {
                using (var client = new WebClient())
                {
                    var html = await client.DownloadStringTaskAsync(new Uri("https://www.google.com"));
                    textBox1.Text = html;
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
    }
}
BlueSky
  • 1,449
  • 1
  • 16
  • 22