Search code examples
vb.netwebclient

VB Webclient "The underlying connection was closed"


When I try to DownloadString from a website I get this error. This error has never come up and has worked all of the time, I have no changed this line of code for at least a month now but for some reason it is creating an error.

Just a bit of information that may be helpful: The variable "Word" is what every the user inputs, I made sure to input a word that exists for example "taberu" (a Japanese word) which then concatenates to "https://jisho.org/search/taberu", this should work because the page exists. (I also checked in the 'autos' window and I am definitely right about the concatenation.

Why is this happening and how do I fix it?

EDIT; In short, this is my code:

Dim Word As String = Console.Readline
Dim WordURL As String = "https://jisho.org/search/" & Word
Dim Client As New WebClient
Client.Encoding = System.Text.Encoding.UTF8

Dim HTML As String
HTML = Client.DownloadString(New Uri(WordURL)) 'this is the line that causes the error

Solution

  • APPARENTLY, adding the following line of code makes it work

    ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12

    Tls11 also works

    I have no idea why but it fixed my issue. Source