How to send SMS from VB.Net using HTTP requsts
Download: | send-sms-vb.net-http-sms-example.zip (106 Kb) |
This guide will provide you information on how to send and receive SMS
messages
using HTTP requests from a VB.Net application. The information provided here
can be used to send SMS messages through your own SMS Gateway (e.g.: Ozeki NG
SMS Gateway),
or you can modify this code to connect to any HTTP SMS Service provider over the
Internet.
Introduction
Visual Basic .Net is an excellent programming language for
creating dynamic web pages and standard
applications. It offers great flexibility in all fields. VB.Net developers are
often required to add mobile messaging capability to their applications. Sending
SMS text messages to mobile users, tracking the status of these messages and the
ability to receive SMS text messages can greatly improve the value of a VB.Net
program. If you are faced with such a requirement, the simplest and most
convenient way to add SMS messaging functionality to your system is to use an
HTTP SMS gateway, such as Ozeki NG SMS Gateway and to post SMS messages to
this gateway using HTTP requests (Figure 1).
Figure 1 - How to send SMS from VB.NET using HTTP requests
If you use HTTP in your VB.Net application, you can also
receive SMS messages. You can do this in two ways. You can configure an
HTTP Client user in Ozeki NG SMS Gateway
to forward incoming SMS messages to your VB.Net program. This solution requires
you to be able to process incoming HTTP requests. The second option is to
periodically download incoming messages and message delivery reports from
Ozeki NG SMS Gateway.
Prerequisites
To be able to send SMS messages from your VB.Net application, you need to download
and install Ozeki NG SMS Gateway. After Ozeki NG SMS Gateway has been installed,
you can use Microsoft Visual Studio or any other IDE, that let's you develop
VB.Net code, to develop your solution. You can use the source code on this
webpage to create the functionality. So here is your checklist:
- Download Ozeki NG SMS Gateway
- Microsot Visual
Basic 2008 Express Edition
- send-sms-vb.net-http-sms-example.zip
Setting up the system and writing the code
Ozeki NG SMS Gateway can be setup very easily. The
SMS quick start guide gives
you information on how to install and connect this gateway to the mobile network.
After you have installed Ozeki NG SMS Gateway, you need to create a HTTP Server
user in it. You can do this by clicking on the "Add new user or application..."
link in it's management window. When you create the HTTP Server user, you provide
a username and password. You need to use this username and password in the VB.Net
source code.
Step 1 - Setup the connection parameters in the VB.Net example code
If you open the VB.Net demo project, you will find the following
section:
Dim request As HttpWebRequest Dim response As HttpWebResponse = Nothing Dim url As String Dim username As String = "admin" Dim password As String = "abc123" Dim host As String = "http://127.0.0.1:9501" Dim originator As String = "06201234567" |
In this section, you need to provide the username and
password you have specified, when you have created the HTTP Server user
in Ozeki NG SMS Gateway. If your VB.Net application runs on a different
computer, then the one Ozeki NG SMS Gateway is installed on, you need to
change the host parameter es well. You can also specify a sender ID value
to be used as the originator phone number when you are sending SMS messages.
Step 2 - Compose of the URL
The next section of the VB.Net HTTP SMS example code is
the composition of the HTTP URL, that will be used to post your SMS message
to the SMS Gateway. This URL contains parameters that are specified in the
Ozeki HTTP SMS API specification. Note that
when you compose this URL you have to URL encode the values. This is necessary
to make sure special characters will not break the HTTP specification.
url = host + "/api?action=sendmessage&" _ & "username=" & HttpUtility.UrlEncode(username) _ & "&password=" + HttpUtility.UrlEncode(password) _ & "&recipient=" + HttpUtility.UrlEncode(tbReceiver.Text) _ & "&messagetype=SMS:TEXT" _ & "&messagedata=" + HttpUtility.UrlEncode(tbMessage.Text) _ & "&originator=" + HttpUtility.UrlEncode(originator) _ & "&serviceprovider=" _ & "&responseformat=html" |
Step 3 - Submit the URL
To send your SMS message you need to initiate a HTTP request
using the Webrequest VB.Net method. After the request has been sent, you
can read the response returned by the SMS gateway using the request.GetResponse()
method and you can show the response message in a popup window.
request = DirectCast(WebRequest.Create(url), HttpWebRequest) |
Receiving the answer.
response = DirectCast(request.GetResponse(), HttpWebResponse) |
Displaying the answer in a pop-up window
MessageBox.Show("Response: " & response.StatusDescription) |
As you can see it is very simple to send an SMS message through
an HTTP request. All you have to do is understand the
HTTP SMS API parameters
and you need to send and receive a request using the built in VB.Net methods.
Finally take a look at the full source code we used in this article:
Full source code
Please feel free to use and modify the
source code sample.
Imports System Imports System.IO Imports System.Net Imports System.Text Imports System.Web Public Class fMain Private Sub bSend_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles bSend.Click Dim request As HttpWebRequest Dim response As HttpWebResponse = Nothing Dim url As String Dim username As String Dim password As String Dim host As String Dim originator As String Try host = "http://127.0.0.1:9501" originator = "06201234567" username = "admin" password = "abc123" url = host + "/api?action=sendmessage&" _ & "username=" & HttpUtility.UrlEncode(username) _ & "&password=" + HttpUtility.UrlEncode(password) _ & "&recipient=" + HttpUtility.UrlEncode(tbReceiver.Text) _ & "&messagetype=SMS:TEXT" _ & "&messagedata=" + HttpUtility.UrlEncode(tbMessage.Text) _ & "&originator=" + HttpUtility.UrlEncode(originator) _ & "&serviceprovider=GSMModem1" _ & "&responseformat=html" request = DirectCast(WebRequest.Create(url), HttpWebRequest) response = DirectCast(request.GetResponse(), HttpWebResponse) MessageBox.Show("Response: " & response.StatusDescription) Catch ex As Exception End Try End Sub End Class
-------------------------------------------------------------------------------
Enjoy!
0 Comments:
Post a Comment
Subscribe to Post Comments [Atom]
<< Home