VB.NET HTTP SMS example

See how to add SMS functionality to your VB.NET application. For the solution you will only need an Ozeki 10 and a Microsoft Visual Studio. The communication will work through the HTTP protocol. The source code is provided. You can download and edit it.

How to add SMS functionality to VB.NET (Easy to follow directives)

To add SMS functionality to VB.NET:

  1. Install Microsoft Visual Studio
  2. Download the example project
  3. Launch Ozeki SMS Gateway
  4. Add HTTP Server Connection
  5. Open the VB.NET example project
  6. Rewrite the parameters if needed
  7. Create HTTP request URL
  8. Send the HTTP request to Ozeki

Download: send-sms-vb.net-http-sms-example.zip (106 Kb)

Introduction

VB.NET applications can be flexibly developed for creating dynamic web pages and standard applications. Developers are usually required to add mobile messaging to their applications, so users can send SMS messages to any recipient. Usually the most simplest and convenient way to add a SMS messaging to your system is by using a HTTP SMS gateway, such as Ozeki 10's SMS Gateway application and post messages to this gateway using HTTP requests (Figure 1).

send and receive sms messages with http requests
Figure 1 - Send and receive SMS messages with HTTP requests

Besides sending SMS messages, you can receive them too with VB.NET. You can do this in two ways. The easiest way is to create a HTTP Client User in Ozeki SMS Gateway, which can forward incoming messages to your VB.NET programcodes. This works if you are able to process HTTP requests. The other way is to periodically download incoming messages. You can download text messages with delivery reports from Ozeki SMS Gateway.

Prerequisites

There are only two prerequisites you should download. Ozeki 10's SMS Gateway application and Microsoft Visual Studio for coding VB.NET. Both are the two ends of the HTTP communication. Besides Microsoft Visual Studio you can use any other IDE that let's you develop in VB.NET. If you scroll bellow you can see the VB.NET example codes for the HTTP application. Here you can find the software checklist and the example program:

Set up Ozeki 10 and run the example code

You can easily download and install Ozeki 10. Check the SMS quick start guide to easily connect it to the mobile network. You will also need to create a HTTP Server Connection in Ozeki 10. Click 'Add new user or application...' and look for the HTTP Server Connection in the list. Click the blue 'Install' button next to it (Figure 2).

Figure 2 - Installing a HTTP Server Connection

You will need to provide the username and password. Do not forget these login credentials. You will need to provide them in VB.NET by replacing the example strings in the source code.

Step 1 - Set up the connection parameters in the VB.NET example code

Open the VB.NET example project and rewrite the necessary parameters (Code 1).

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"
Code 1 - HTTP parameters

The 'username' and 'password' strings are the login credentials belonging to the HTTP Server Connection. If Ozeki 10 and your VB.NET application is running on different machines then it is required to rewrite the 'host' parameter to the IP address and port number of the Ozeki 10 machine. The 'originator' is the phone number used as the sender.

Step 2 - Compose URL from parameters

Code 2 composes a HTTP request from the parameters provided in Code 1. This URL will be used to post your SMS message to Ozeki 10's SMS Gateway application. All parameters specified in the Ozeki HTTP SMS API documentation can be contained in the URL. The values must be URL encoded, so special characters can 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"

Code 2 - HTTP request URL created from the parameters

Step 3 - Submit URL to Ozeki 10

Code 3 contains the last three lines of this VB.NET example. It sends the HTTP request and shows the response in a pop-up window. The WebRequest.Create("...") built in VB.NET method sends the URL to Ozeki 10, while GetResponse() method collects the response. It will appear in a pop-up window generated with the MessageBox.Show("...") method.

request = DirectCast(WebRequest.Create(url), HttpWebRequest)
    
response = DirectCast(request.GetResponse(), HttpWebResponse)
    
MessageBox.Show("Response: " & response.StatusDescription)
Code 3 - Send HTTP request and show response in pop-up window

It is advised to walk through the HTTP API actions and the corresponding parameters to control Ozeki 10 through the HTTP API. This way you can create request URLs for your needs.

Full VB.NET example code

The full code you can see below (Code 4) is built up from segments described above (Code 1 - 3). You can freely use and modify the example code as you wish. An exception can be dropped if it is a problem with the HTTP communication.

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
		MessageBox.Show(ex.Message)
		
	End Try
End Sub
End Class
Code 4 - The full example code

Conclusion

This article was written for you to see how to add SMS functionality to your VB.NET application using the Ozeki SMS Gateway. A VB.NET application is really beneficial to use, because it can be flexibly developed for creating dynamic web pages and standard applications equally. If you have followed the tutorial carefully, you have learned how to create an HTTP client user in the Ozeki SMS Gateway and how to use it for message system development in your VB.NET application.

You can find more articles on the Ozeki website to read about interesting topics and impressive solutions. Check out the next guide about how to send SMS from VB.NET using HTTP API.

Download the Ozeki SMS Gateway now, for a better performing SMS system!

More information