Page 1 of 1

VB.NET Code to POST SMS API

Posted: Thu Apr 07, 2011 12:09 pm
by frimbia
A bit spammy to post two new topics together, but as this is a help topic (rather than a request for help) I think I am OK.

Code: Select all

Imports System.IO
Imports System.Net
Imports System.Web

Public Sub ExetelSMS(ByVal strNumber As String, ByVal strbody As String, ByVal strusername As String, _
                        ByVal strpassword As String, ByVal strSender As String)

        Dim myURL As String = Nothing
        Dim rspArray() As String
        Dim Req As HttpWebRequest = Nothing
        Dim PostData() As Byte = System.Text.Encoding.Default.GetBytes(myURL)

        myURL = "https://smsgw.exetel.com.au/sendsms/api_sms.php?username=" & strusername _
            & "&password=" & strpassword & "&mobilenumber=" & strNumber & "&message=" _
            & strbody & "&sender= " & strSender & "&messagetype=Text"

        Try
            Req = CType(WebRequest.Create(myURL), HttpWebRequest)
            Req.Method = "POST"
            Req.ContentType = "application/x-www-form-urlencoded"

            Req.GetRequestStream.Write(PostData, 0, PostData.Length) 'Post the URL
            Req.GetRequestStream.Close()
        Catch ex As Exception
            MessageBox.Show(ex.Message, MBTitle, MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
        End Try

        Try 'To read response from Exetel, and just read the last segment, which is Status (eg OK)
            Dim resp As HttpWebResponse = CType(Req.GetResponse, HttpWebResponse)
            Dim sr As New StreamReader(resp.GetResponseStream)
            Dim ResponseText As String = sr.ReadToEnd

            rspArray = Split(ResponseText, "|")
            MessageBox.Show(Replace(rspArray(UBound(rspArray)), "<br>", ""), MBTitle, MessageBoxButtons.OK, MessageBoxIcon.Exclamation)

            sr.Dispose()
            resp.Close()

        Catch ex As Exception
            MessageBox.Show(ex.Message, MBTitle, MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
        End Try


    End Sub
I think the arguements are obvious. The array is a bit VBA rather than .net, maybe I will fix that up later.