Click here to Skip to main content
14,865,287 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,
Am really struggling with Posting Json to a Secure Rest API in a VB.net Compact 3.5 App. Below is the first Piece of code I tried and I get a error that 'Could not establish secure channel for SSL/TLS'

VB
Imports Newtonsoft.Json
Imports System.Net
Imports System.IO
Imports System.Text
Public Class Form1   
   Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        'Create Json of Data for Transmition
        Dim SendData As String
        Dim NetSuiteRequest As New NetSuite.CreateBin.Request
        Dim BinList As New List(Of NetSuite.CreateBin.RequestProperties)
        Dim Bin As New NetSuite.CreateBin.RequestProperties
        Bin.BinNumber = "D20.A01.01.A"
        Bin.RecordType = "create"
        Bin.RecordType = "bin"
        BinList.Add(Bin)
        NetSuiteRequest.request = BinList
        SendData = JsonConvert.SerializeObject(NetSuiteRequest, Formatting.Indented)

        '**********************************************************************************
        Dim request As HttpWebRequest
        Dim response As HttpWebResponse = Nothing
        Dim reader As StreamReader
        Dim address As Uri
        Dim byteData() As Byte
        Dim postStream As Stream = Nothing

        address = New Uri("https://---------------------------Sorry Cant Show URL--------------")

        ' Create the web request  
        request = DirectCast(WebRequest.Create(address), HttpWebRequest)

        ' Set type to POST  
        request.Method = "POST"
        request.ContentType = "application/json; charset=UTF-8"
        request.Accept = "application/json"

        'Add authentication to request
        request.Credentials = New NetworkCredential("UserName", "Password")


        ' Create a byte array of the data we want to send  
        byteData = UTF8Encoding.UTF8.GetBytes(SendData.ToString())

        ' Set the content length in the request headers  
        request.ContentLength = byteData.Length

        ' Write data  
        Try
            postStream = request.GetRequestStream()
            postStream.Write(byteData, 0, byteData.Length)
        Finally
            If Not postStream Is Nothing Then postStream.Close()
        End Try

        Try
            ' Get response  
            response = DirectCast(request.GetResponse(), HttpWebResponse)

            ' Get the response stream into a reader  
            reader = New StreamReader(response.GetResponseStream())

            ' Console application output  
            Console.WriteLine(reader.ReadToEnd())
        Finally
            If Not response Is Nothing Then response.Close()
        End Try


    End Sub
End Class


What I have tried:

I tried using Rebex.net to overcome this issue and this the code that I came up with but it does not work either.

VB
Imports Newtonsoft.Json
Imports System.Net
Imports System.IO
Imports System.Text
Imports Rebex.Net
Public Class Form1

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        'Create Json of Data for Transmition
        Dim SendData As String
        Dim NetSuiteRequest As New NetSuite.CreateBin.Request
        Dim BinList As New List(Of NetSuite.CreateBin.RequestProperties)
        Dim Bin As New NetSuite.CreateBin.RequestProperties
        Bin.BinNumber = "D20.A01.01.A"
        Bin.RecordType = "create"
        Bin.RecordType = "bin"
        BinList.Add(Bin)
        NetSuiteRequest.request = BinList
        SendData = JsonConvert.SerializeObject(NetSuiteRequest, Formatting.Indented)

        '*****************************************************************************************
        Dim creator = New HttpRequestCreator()
        creator.Settings.SslAllowedVersions = TlsVersion.Any

        Dim Address As New Uri("https://---------------------------Sorry Cant Show URL--------------")
        Dim Request As WebRequest = creator.Create(Address)
        Request.Method = "POST"
        Request.ContentType = "application/json; charset=UTF-8"
        Request.Credentials = New NetworkCredential("UserName", "Password")

        '************************************************************************************************************
        Dim byteData() As Byte
        Dim postStream As Stream = Nothing
        Dim Response As WebResponse
        Dim reader As StreamReader
        ' Create a byte array of the data we want to send  
        byteData = UTF8Encoding.UTF8.GetBytes(SendData.ToString())

        ' Set the content length in the request headers  
        Request.ContentLength = byteData.Length

        ' Write data  
        Try
            postStream = Request.GetRequestStream()
            postStream.Write(byteData, 0, byteData.Length)
        Finally
            If Not postStream Is Nothing Then postStream.Close()
        End Try

        Try
            ' Get response  
            Response = DirectCast(Request.GetResponse(), WebResponse)

            ' Get the response stream into a reader  
            reader = New StreamReader(response.GetResponseStream())

            ' Console application output  
            Console.WriteLine(reader.ReadToEnd())
        Finally
            If Not response Is Nothing Then response.Close()
        End Try


    End Sub
End Class



All Help is Very much appreciated.
Cheers,
Joel Steen.
Posted
Updated 12-Feb-21 8:06am
v2
Comments
Kornfeld Eliyahu Peter 20-Jun-17 4:35am
   
The details of the exception?
Have you checked for certificate problems?
Aneets 20-Jun-17 20:50pm
   
Thanks for Your Response Kornfeld Eliyahu Peter,
The Exception to the first piece of code was 'Could not establish Secure Channel for SSL/TLS'
How do I check for Certificate Problems?
Cheers,
Joel Steen.



Before making your request, you may write the following:

System.Net.ServicePointManager.SecurityProtocol = System.Net.SecurityProtocolType.Tls12
   
Comments
Richard Deeming 23-Apr-20 13:38pm
   
The question indicates it was a .NET 3.5 application, which didn't support TLS1.2; the SecurityProtocolType enum didn't have the Tls12 member.

Transport Layer Security (TLS) best practices with the .NET Framework | Microsoft Docs[^]
I am facing the same issue with my legacy vb.net 3.5 solution. Were you ever able to get a workaround for this issue, if so please share.
Thanks,
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900