I am developing a .NET Framework 4.0 console application in VB.NET. I’m trying to connect to an API using HttpWebRequest, but I keep encountering the following error:
System.Net.WebException: 'The underlying connection was closed: An unexpected error occurred on a send.'
IOException: Authentication failed because the remote party has closed the transport stream.
Code:
Imports System.Net
Imports System.Net.Http
Imports System.Net.Http.Headers
Module Module1
Sub Main()
' Set the API URL
Dim apiUrl As String = "your_api_url_here"
Console.WriteLine(apiUrl)
Dim request As HttpWebRequest = CType(WebRequest.Create(apiUrl), HttpWebRequest)
Dim response As HttpWebResponse = request.GetResponse()
Console.WriteLine(response.StatusCode)
Console.ReadKey()
End Sub
End Module