1

I am working on a web scraping program and I am getting the following error on sites that have Tls 1.0 and 1.1 disabled, and Tls 1.2 and 1.3 enabled:

'The request was aborted: Could not create SSL/TLS secure channel'

I should also note that I am using .Net version 4.8

This is the site that I am using to check: https://www.cdn77.com/tls-test

I've read nearly a dozen posts on the topic and none of the solutions have worked for me.

This is the code. It works on most sites:

        Try
            ServicePointManager.Expect100Continue = True
            ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12 Or 
                                                    SecurityProtocolType.Tls13

            Dim address = New Uri(url)

            Using client = New WebClient()
                Dim stream = client.OpenRead(address) ' This is where exception is caught
                Using sr = New StreamReader(stream)
                    Dim page = sr.ReadToEnd()
                    Return page
                End Using
            End Using

        Catch ex As Exception
            Return ex.Message
        End Try
  • Could the problem be a [401 response](https://stackoverflow.com/a/25391935/1115360)? – Andrew Morton Jun 15 '21 at 20:03
  • With this kind of questions, you always need to specify the System version your code has been tested on. Possibly, provide a link to the resource, so it can be tested. -- Remove `SecurityProtocolType.Tls13`, you cannot use it. -- There's a good chance the remote resource needs an User-Agent header set (yes, the exception can be the same if it's missing). Use a not so recent version of FireFox as User-Agent, <= 56 (there's a reason). – Jimi Jun 15 '21 at 22:10

1 Answers1

0

I think you will not find so much sites with only TLS1.3

I use this function for many years:

  Function getFromURL(ByVal url As String) As String
        Dim request As System.Net.WebRequest = System.Net.WebRequest.Create(url)
        'request.Headers.Add("HTTP_USER_AGENT", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 2.0.40607; .NET CLR 1.1.4322; .NET CLR 2.0.50215)")
        System.Net.ServicePointManager.SecurityProtocol = System.Net.SecurityProtocolType.Tls12
        Dim response As System.Net.WebResponse

        response = request.GetResponse()

        Dim responseStream As System.IO.Stream = response.GetResponseStream()

        Dim reader As System.IO.StreamReader = New System.IO.StreamReader(responseStream)

        Return reader.ReadToEnd()

    End Function
Chris Berlin
  • 744
  • 6
  • 15