Samples - C#

The samples below require the .NET Framework 2.0

Web Service

There are two ways we can suggest to connect to the Web Service with C#. 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:


    net.textapp.www.Service mySms = new net.textapp.www.Service();
    
    String 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 using System.Net;):


    IWebProxy proxyObject = new WebProxy("proxy_server", "proxy_port");
    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.cs in the SDK ‘Bin’ folder. You can incorporate this file into your project, and then use the Service object from within your own code:


    Service mySms = new Service();
    
    String 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 using System.Net;):


    IWebProxy proxyObject = new WebProxy("proxy_server", "proxy_port");
    proxyObject.Credentials = new NetworkCredential("username", "password");
    
    mySms.Proxy = proxyObject;
    

HTTPS Service

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

Firstly, you will need a System.Net; and using System.IO; for these to work.


    WebRequest 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=");
    
    WebResponse response = httpRequest.GetResponse();
    StreamReader reader = new StreamReader(response.GetResponseStream());

    String result = reader.ReadToEnd();
    response.Close();
    

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


    IWebProxy proxyObject = new WebProxy("proxy_server", "proxy_port");
    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 HTTPS POST method, your code will look like this:


    String 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=";
                            
    byte[] queryData = Encoding.UTF8.GetBytes(queryString);
    
    WebRequest httpRequest = WebRequest.Create("http://www.textapp.net/webservice/httpservice.aspx");
    httpRequest.Method = "POST";
    httpRequest.ContentType = "application/x-www-form-urlencoded";
    httpRequest.ContentLength = queryData.Length;
    
    Stream requestStream = httpRequest.GetRequestStream();
    requestStream.Write(queryData, 0, queryData.Length);
    
    WebResponse response = httpRequest.GetResponse();
    StreamReader reader = new StreamReader(response.GetResponseStream());
    
    String result = reader.ReadToEnd();
    response.Close();
    

Remember, all parameter values need to be URL encoded no matter which HTTPS method (GET or POST) you choose. This will require a using System.Web; and also adding a reference to System.Web.dll version 2.0:


    HttpContext ctx = new HttpContext(null);
    String body = ctx.Server.UrlEncode("hello world");