5

I have an application coded in VB.net that has this method of accessing Webservice, i have this error and after searching fixes i still have no luck.

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

 'ServicePointManager.Expect100Continue = False 'ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls Dim request As HttpWebRequest = DirectCast(WebRequest.Create(url), HttpWebRequest) Dim ErrMsg As String = String.Empty Try Dim response As WebResponse = request.GetResponse() Using responseStream As Stream = response.GetResponseStream() Dim reader As New StreamReader(responseStream, Encoding.UTF8) Return reader.ReadToEnd() End Using Catch ex As WebException ErrMsg = ex.Message.ToString MsgBox(ErrMsg) Dim errorResponse As WebResponse = ex.Response Using responseStream As Stream = errorResponse.GetResponseStream() Dim reader As New StreamReader(responseStream, Encoding.GetEncoding("utf-8")) ' log errorText Dim errorText As [String] = reader.ReadToEnd() End Using Throw End Try 

This is my code and I'm using VS2015 and Windows 10.

All help are really appreciated. TIA

4
  • Maybe the webservice requires TLS 1.1/1.2? Commented Oct 19, 2017 at 8:24
  • Yes Mat i think the problem here circles in this code: ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls Commented Oct 19, 2017 at 9:10
  • Thanks Mat, i tried that solution and yes it works, I created an new project targeting the 4.5 framework, the challenge now is that my Project is targeting 4.0 framework and a bunch of errors occurs when I change it to 4.5. Commented Oct 19, 2017 at 9:18
  • stackoverflow.com/questions/72205024/… Commented May 20, 2022 at 6:29

4 Answers 4

14

Obviously the URL you're calling requires TLS 1.1 or TLS 1.2.

You can enable TLS 1.1 or TLS 1.2 by setting the security-protocol with ServicePointManager.SecurityProtocol:

ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12 

.NET 4.0 supports up to TLS 1.0 while .NET 4.5 or higher supports up to TLS 1.2
For reference:

Sign up to request clarification or add additional context in comments.

2 Comments

For a public portal, I had to check if stored urls still work with System.Net.WebRequest. Thereby I have noted, that calls with http crash, when the site was updated to https in the meantime (whereby a browser handles this automatically). With your answer it now works.
Thanks it worked. I had to change Target Framework in project properties(Right click on Project on solution explorer->properties->Application) to 4.6.1 to use this
2

I'm going to add something here in case it helps others. I have to support some legacy code that is written in VB.Net and targets .NET Framework 4.6.1. The same piece of code runs in both an .exe, and in IIS as a web page.

In the website, this call:

Dim dataStream As Stream = request.GetRequestStream() 

would throw this exception

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

but it worked fine in the .exe.

Forcing the TLS protocol fixed it in the website.

ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12 

Of course if the host ever deems TLS 1.2 unworthy I'll have to fix/recompile/redeploy, but it solved the error for now.

Comments

1

for vb.net 4.0 this worked for me: ServicePointManager.SecurityProtocol = DirectCast(&HC0 Or &H300 Or &HC00, SecurityProtocolType)

2 Comments

This works in legacy vb .net app thats works in pro but fails my local env. Thanks!
I cannot thank you as much as you deserve it! Saved my day using this as a last minute fix for a customer presentation! Thank you so much, first upvote after 2 years 😬
0

I face this problem too. There are "The request was aborted: Could not create SSL/TLS secure channel". Below are my solving lines.

 Private Sub Button4_Click(sender As Object, e As EventArgs) Handles Button4.Click Dim ts1 As Uri ServicePointManager.Expect100Continue = True 'AA ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12 'AB ts1 = New Uri(TextBox10.Text) PictureBox1.Load(ts1.ToString) WebBrowser1.Url = ts1 End sub 

If not place line code "AA" and "AB" it will be trouble every time when click.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.