none
Curl VB.Net (SMS gateway API) RRS feed

  • Question

  • Dear All,

    Please can anybody help me to Convert this script bellow to VB.Net?

    This script bellow is a Curl to send SMS through SMS Cloud.

    curl -X POST  https://rest.nexmo.com/sms/json \
    -d api_key=629f7e21 \
    -d api_secret=GEAnJJu4EHPG9J5b \
    -d to=611339454565 \
    -d from="NEXMO" \
    -d text="Hello from Nexmo"

    I Try convert like bellow, but Error.

    

    Thank you...



    • Edited by Frangky_Koe Sunday, September 15, 2019 3:15 PM
    Sunday, September 15, 2019 2:16 PM

Answers

  • Hi,

    try the following code,parameters is the string you want to pass in

      Private Function HttpPost(ByVal uri As String, ByVal parameters As String) As String
            Dim webRequest As WebRequest = WebRequest.Create(uri)
            webRequest.ContentType = "application/x-www-form-urlencoded"
            webRequest.Method = "POST"
            Dim bytes As Byte() = Encoding.ASCII.GetBytes(parameters)
            Dim os As Stream = Nothing
    
            Try
                webRequest.ContentLength = bytes.Length
                os = webRequest.GetRequestStream()
                os.Write(bytes, 0, bytes.Length)
            Catch ex As WebException
                MessageBox.Show(ex.Message, "HttpPost: Request error", MessageBoxButtons.OK, MessageBoxIcon.Error)
            Finally
    
                If os IsNot Nothing Then
                    os.Close()
                End If
            End Try
    
            Try
                Dim WebResponse = webRequest.GetResponse()
    
                If WebResponse Is Nothing Then
                    Return Nothing
                End If
    
                Dim sr As StreamReader = New StreamReader(WebResponse.GetResponseStream())
                Return sr.ReadToEnd().Trim()
            Catch ex As WebException
                MessageBox.Show(ex.Message, "HttpPost: Response error", MessageBoxButtons.OK, MessageBoxIcon.Error)
            End Try
    
            Return Nothing
        End Function

    https://docs.microsoft.com/en-us/dotnet/api/system.net.webrequest?redirectedfrom=MSDN&view=netframework-4.8

    Best Regards,

    Alex


    MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by Frangky_Koe Monday, September 16, 2019 4:57 AM
    Monday, September 16, 2019 1:58 AM
    Moderator

All replies

  • Hi,

    try the following code,parameters is the string you want to pass in

      Private Function HttpPost(ByVal uri As String, ByVal parameters As String) As String
            Dim webRequest As WebRequest = WebRequest.Create(uri)
            webRequest.ContentType = "application/x-www-form-urlencoded"
            webRequest.Method = "POST"
            Dim bytes As Byte() = Encoding.ASCII.GetBytes(parameters)
            Dim os As Stream = Nothing
    
            Try
                webRequest.ContentLength = bytes.Length
                os = webRequest.GetRequestStream()
                os.Write(bytes, 0, bytes.Length)
            Catch ex As WebException
                MessageBox.Show(ex.Message, "HttpPost: Request error", MessageBoxButtons.OK, MessageBoxIcon.Error)
            Finally
    
                If os IsNot Nothing Then
                    os.Close()
                End If
            End Try
    
            Try
                Dim WebResponse = webRequest.GetResponse()
    
                If WebResponse Is Nothing Then
                    Return Nothing
                End If
    
                Dim sr As StreamReader = New StreamReader(WebResponse.GetResponseStream())
                Return sr.ReadToEnd().Trim()
            Catch ex As WebException
                MessageBox.Show(ex.Message, "HttpPost: Response error", MessageBoxButtons.OK, MessageBoxIcon.Error)
            End Try
    
            Return Nothing
        End Function

    https://docs.microsoft.com/en-us/dotnet/api/system.net.webrequest?redirectedfrom=MSDN&view=netframework-4.8

    Best Regards,

    Alex


    MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by Frangky_Koe Monday, September 16, 2019 4:57 AM
    Monday, September 16, 2019 1:58 AM
    Moderator
  • Dear Alex,

    Thank you.. your coding above solved my problem...

    Monday, September 16, 2019 4:58 AM
  • Dear Alex,

    Please Help me again, 

    how to convert this script bellow to VB.Net. the curl script bellow used Authorization: Bearer. I am confused about Authorization Bearer.

    curl -X POST https://api.wavecell.com/sms/v1/Montsion_0pPAD_hq/single

    -H "Authorization: Bearer 97GMaU7MxON1H9nZm937ltWfPEYEduIDkHgfh875j"
    -H "Content-Type: application/json"

    -d $'{ "source": "abcde", "destination": "+6512345678", "text": "Hello World!", "encoding": "AUTO" }'

    Tuesday, October 1, 2019 12:11 PM