Sample - VB.NET

Web Service

There are two ways we can suggest to connect to the Web Service with VB.NET. The first, using Visual Studio is straight forward. Right-click your project and select ‘Add a web reference’. If when right-clicking you don't have an option to "Add Web Reference..." you will need to click "Add Service Reference..." then the "Advanced..." button followed by the "Add Web Reference..." button. Into the URL field enter http://www.textapp.net/webservice/service.asmx?wsdl, and then click the ‘Go’ button. You should see the Service page, if so, click ‘Add Reference’. If you do not see the Service page you should check your Internet Explorer proxy settings or speak to your network administrator.

Assuming you did not change the contents of the ‘Web Reference Name’ field, you would be able to send a message:

Dim mySms = New net.textapp.www.Service Dim result = mySms.sendSMS(false, "myExternalLogin", "myPassword", "clientBillingReference", _ "clientMessageReference", "+447923456789", "+447923456789", _ "hello world", 72, 2, 4, "", "")

You can call any of the other Web Service methods in a similar manner. In most cases, Visual Studio will bring up a list of available methods when you type ‘mySms.’. If you need to specify proxy settings, you can set the properties of the Service object (REQUIRES imports System.Net):

Dim proxyObject = New WebProxy("proxy_server", 8080) proxyObject.Credentials = New NetworkCredential("username", "password") mySms.Proxy = proxyObject

If you cannot use Visual Studio, it is still possible to use the wsdl.exe command line utility to generate the Service object code. The utility is part of the 2.0 .NET framework SDK. You can download this from Microsoft. To create the object code you would navigate to the ‘Bin’ folder of the SDK and execute the following command:

This will produce a file called Service.vb in the SDK ‘Bin’ folder. You can incorporate this file into your project, and then use the Service object from within your own code:

Dim mySms = New Service() Dim result = mySms.sendSMS(false, "myExternalLogin", "myPassword", "clientBillingReference", _ "clientMessageReference", "+447923456789", "+447923456789", _ "hello world", 72, 2, 4, "", "")

If you need to specify proxy details you can do so as required (REQUIRES imports System.Net):

Dim proxyObject = New WebProxy("proxy_server", 8080) proxyObject.Credentials = New NetworkCredential("username", "password") mySms.Proxy = proxyObject

HTTP(S) Service

Connecting to the HTTP(S) Service is very straightforward. You have the option to use HTTP(S) POST and GET methods. You can find the general formula for creating the URIs in the HTTP(S) Service documentation.

Firstly, you will need imports System.Net, imports System.IO and imports System.Text for these to work.

Remember, all parameter values need to be URL encoded no matter which HTTP(S) method (GET or POST) you choose. This will require a imports System.Web and also adding a reference to System.Web.dll version 2.0. If your project is a Web Application then you may perform URL encoding as shown:

Dim ctx As New HttpContext(Nothing) Dim body = ctx.Server.UrlEncode("hello world")

If you are working in a Windows Application or other project you may use the example below:

Dim body As String = System.Web.HttpUtility.UrlEncode("hello world")

Dim httpRequest = WebRequest.Create("http://www.textapp.net/webservice/httpservice.aspx _ ?method=sendsms&returncsvstring=false _ &externallogin=mylogin&password=mypassword _ &clientbillingreference=myclientbillingreference _ &clientmessagereference=myclientmessagereference _ &originator=mynumber&destinations=%2b447912345678 _ &body=hello%20world&validity=72&charactersetid=2 _ &replymethodid=4&replydata=&statusnotificationurl=") Dim response As WebResponse = httpRequest.GetResponse() Dim reader = New StreamReader(response.GetResponseStream()) Dim result = reader.ReadToEnd() response.Close()

If you need to specify proxy details you can do so:

Dim proxyObject = New WebProxy("proxy_server", 8080) proxyObject.Credentials = New NetworkCredential("username", "password") httpRequest.Proxy = proxyObject

You will of course need to specify them before the call to GetResponse().

So, to use the HTTP(S) POST method, your code will look like this:

Dim queryString = "method=sendsms&returncsvstring=false&externallogin=mylogin&password=mypassword _ &clientbillingreference=myclientbillingreference _ &clientmessagereference=myclientmessagereference _ &originator=mynumber&destinations=%2b447912345678 _ &body=hello%20world&validity=72&charactersetid=2 _ &replymethodid=4&replydata=&statusnotificationurl=" Dim queryData = Encoding.UTF8.GetBytes(query_string) Dim httpRequest As WebRequest = WebRequest.Create("http://www.textapp.net/webservice/httpservice.aspx") httpRequest.Method = "POST" httpRequest.ContentType = "application/x-www-form-urlencoded" httpRequest.ContentLength = queryData.Length Dim request_stream = httpRequest.GetRequestStream() requestStream.Write(queryData, 0, queryData.Length) Dim response As WebResponse = httpRequest.GetResponse() Dim reader = New StreamReader(response.GetResponseStream()) Dim result = reader.ReadToEnd() response.Close()

Footer Bottom