0

Below code fails with the generic error from the title on line where creates the response Dim httpResponse As HttpWebResponse = CType(httpWebRequest.GetResponse(), HttpWebResponse)

Code below:

 Dim URL As String = "https://www150.statcan.gc.ca/t1/wds/rest/getSeriesInfoFromVector"
    Dim JsonData As String = "[{""vectorId"":""1038036698""}]"
    Dim OutputFile As String = "C:\Temp\1038036698.json"
    Public Sub Main()
        Dim myRequest As HttpWebRequest = PostJSON(JsonData)
        Dim Response As String = GetResponse(myRequest)
        System.IO.File.WriteAllText(OutputFile, Response)
     End Sub
    Private Function PostJSON(ByVal JsonData As String) As HttpWebRequest
        Dim objhttpWebRequest As HttpWebRequest
        Try
            Dim httpWebRequest As HttpWebRequest = CType(WebRequest.Create(URL), HttpWebRequest)
            httpWebRequest.Method = "POST"
            httpWebRequest.ContentType = "application/json"
            httpWebRequest.MediaType = "application/json"
            httpWebRequest.Accept = "application/json"
            Using streamWriter As StreamWriter = New StreamWriter(httpWebRequest.GetRequestStream(), Encoding.UTF8)
                streamWriter.Write(JsonData)
                streamWriter.Flush()
                streamWriter.Close()
            End Using
            objhttpWebRequest = httpWebRequest
        Catch ex As Exception
            ' Console.WriteLine("Send Request Error[{0}]", ex.Message)
            Return Nothing
        End Try
        Return objhttpWebRequest
    End Function
    Private Function GetResponse(ByVal httpWebRequest As HttpWebRequest) As String
        Dim strResponse As String = "Bad Request:400"
        Try
            ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12
'next line is where it errors out
            Dim httpResponse As HttpWebResponse = CType(httpWebRequest.GetResponse(), HttpWebResponse)
            Dim StreamReader As StreamReader = New StreamReader(httpResponse.GetResponseStream(), True)
            'Dim StreamReader As StreamReader = New StreamReader(DirectCast(httpResponse.GetResponseStream(), String), True)
            Dim result As String = StreamReader.ReadToEnd()
            strResponse = result.ToString()
        Catch ex As Exception
            Console.WriteLine("GetResponse Error[{0}]", ex.Message)
            Return ex.Message
        End Try
        Return strResponse
    End Function
sehor
  • 11
  • 1
  • Does this answer your question? [VB. NET: The request was aborted: Could not create SSL/TLS secure channel](https://stackoverflow.com/questions/46825387/vb-net-the-request-was-aborted-could-not-create-ssl-tls-secure-channel) – Andrew Morton Jun 26 '20 at 08:52
  • Andrew, it does not unfortunately. I already request tls12 in the code as "ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12" – sehor Jun 26 '20 at 16:46
  • I tried your code as-is and ended up with the file in C:\Temp with a content of `[{"status":"SUCCESS","object":{"responseStatusCode":0,"productId":38100238,"coordinate":"1.11.0.0.0.0.0.0.0.0","vectorId":1038036698,"frequencyCode":9,"scalarFactorCode":0,"decimals":2,"terminated":0,"SeriesTitleEn":"Canada;Credit market debt to disposable account","SeriesTitleFr":"Canada;Dette sur le marché du crédit en proportion du revenu disponible","memberUomCode":271}}]`. I'm in the UK. I was going to try using [Fiddler](https://www.telerik.com/fiddler) to see where it went wrong, but it didn't. – Andrew Morton Jun 26 '20 at 17:20
  • This is interesting since I end up with only "The request was aborted: Could not create SSL/TLS secure channel." in the file. – sehor Jun 26 '20 at 20:31
  • Could it be that they are rate-limiting requests/blocking some IP addresses? Does using Fiddler to see what is happening give you any better information? – Andrew Morton Jun 26 '20 at 20:55
  • I just thought: do you have any antivirus software which might be interfering? – Andrew Morton Jun 26 '20 at 22:21

0 Answers0