简体   繁体   中英

ASP.NET "The request was aborted: Could not create SSL/TLS secure channel" occurs also with configured servicepointmanager

I'm trying to request this image: https://www.kamerstunt.nl/file/img/web/woning/9577323WenumWieselZwolseweg-142d_6.jpg If the image no longer exists when you read this, it might have been removed, but you'd still be able to view the SSL certificate in question. Using my browser I was able to successfully navigate to the page, request the image and see a valid SSL certificate.

I checked here: The request was aborted: Could not create SSL/TLS secure channel

So I added that solution to my code:

Dim imgRequest As WebRequest = WebRequest.Create("https://www.kamerstunt.nl/file/img/web/woning/9577323WenumWieselZwolseweg-142d_6.jpg")
Dim imgResponse As WebResponse
ServicePointManager.Expect100Continue = True
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls 
'//I also tried: ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls Or SecurityProtocolType.Ssl3
imgResponse = imgRequest.GetResponse()
Dim streamPhoto As Stream = imgResponse.GetResponseStream()

I tried:

ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3 Or SecurityProtocolType.Tls Or SecurityProtocolType.Tls11 Or SecurityProtocolType.Tls12

But then I get errors: 'Tls12' is not a member of 'System.Net.SecurityProtocolType' and 'Tls11' is not a member of 'System.Net.SecurityProtocolType'

I also tried to change the registry to allow windows to not block DHE with 512 bits and added ClientMinKeyBitLength with 0x00000200(512) value under HKEY_LOCAL_MACHINE\\SYSTEM\\CurrentControlSet\\Control\\SecurityProviders\\SCHANNEL\\KeyExchangeAlgorithms\\Diffie-Hellman.

But still it fails...why?

Here is a solution I use that returns a Stream ... You can also modify it so it returns a byte array and then create a new MemoryStream from that byte array. Also instead of a string type to pass you can change it to a url, but that's up to you.

Public Function GetRemoteStream(uRL As String) As MemoryStream
   Dim webClient As New WebClient()
   Dim imageBytes As Byte() = webClient.DownloadData(uRL)
   Dim mem As New MemoryStream(imageBytes)
   Return mem
End Function

Example Usage

 Dim nStream As New MemoryStream
 nStream = GetRemoteStream(yoururlhere)

EDIT PLEASE READ ********************************************

After looking into this and digging a little further, I found a solution. It seem's that the site has dropped support for SSL & Tls11.

I started a new project targeting 4.5 framework . I then used this SecurityProtocolType ...

 SecurityProtocolType.Tls12

Solution

Change the target framework to: 4.5 and use: SecurityProtocolType.Tls12 . Now your protocol should look like this...

 ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12

On another note

I recommend wrapping your Stream so it get's properly disposed of. For example:

Using stream As Stream = imgResponse.GetResponseStream()
            Using ms As New MemoryStream()
                Dim count As Integer = 0
                Do
                    Dim buf As Byte() = New Byte(1023) {}
                    count = stream.Read(buf, 0, 1024)
                    ms.Write(buf, 0, count)
                Loop While stream.CanRead AndAlso count > 0
                'ms is your memory stream... as I take it you want the photo :)
            End Using
        End Using

Here's proof of my output...

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM