Working with the Clickatell XML API from .NET / C#

Clickatell offer a few different ways to integrate your application with their services. Of all the options available, XML seemed to be the most useful, however it gave me a little bit of trouble when I tried to get it working. Namely, this 'fault response':

<?xml version="1.0" ?>
<clickAPI>
  <fault>XML error: no element found at line 1</fault>
</clickAPI>

I took my input XML and pasted it into the Clickatell test form and it parsed fine. Some searching on the error revealed a lot of other people also had this issue, however most of the search results I found lead to threads where people had either given up, or simply not followed up with a reply posting once they've solved their issue, and so no solutions were forthcoming. Clickatell don't provide what they refer to as 'Developer Support' and basically shoot down anyone asking for code samples. There are a lot of people posting on the forums asking for help, and all of them are being referred to either useless Knowledge base articles, or being told to go and hire a development company.

The Clickatell API guide contains most of the information that you should need in order to get started, however it's missing a few useful pieces of information. What most people seem to really want (and what I wanted myself) was a small piece of working code to use as a reference point. And so now that I've got my problem fixed, here it is (in it's quick and messy but working state) in the hope that it helps any others out there who are having the same issue:

private void btnSendTestMessage_Click(object sender, EventArgs e)
{

    string gatewayUrl = "http://api.clickatell.com/xml/xml";
    HttpWebResponse resp = null;

    try
    {
        HttpWebRequest  request = (HttpWebRequest)WebRequest.Create(gatewayUrl);
        string inputXml = GenerateXml();
        
        // encode post data and set up the request
        // Note: input xml is passed a a parameter named 'data'
        string postData = "data=" + inputXml;
        
        byte[] postDataBytes = ConvertStringToByteArray(postData);
        request.Method = "POST";

        request.ContentType = "application/x-www-form-urlencoded";
        request.ContentLength = postDataBytes.Length;

        Stream requestStream = request.GetRequestStream();
        requestStream.Write(postDataBytes, 0 ,postDataBytes.Length);
        requestStream.Close();

        // get response and write to console
        resp = (HttpWebResponse)request.GetResponse();
        StreamReader responseReader = new
          StreamReader(resp.GetResponseStream(), Encoding.UTF8);

        string theresponse = responseReader.ReadToEnd();

        resp.Close();
    }
    catch(Exception ex)
    {
        // do something tidy with the exception here!
    }
    finally
    {
        if ( resp != null )
        {
            resp.Close();
        }
    }
}


// Simple method to convert a string to a Bytearray
public static byte[] ConvertStringToByteArray(string stringToConvert)
{
    return (new ASCIIEncoding()).GetBytes(stringToConvert);
}

In my case, it was the encoding that was killing my request. The simple HTTP post example that I used as a starting point used UTF8 encoding. Clickatell doesn't like this, so I modified my example to use ASCII encoding and things started working. Also note that there should be no XML declaration, and that it should be sent through inside a paramater named 'data'. This would be information which might be useful in the API manual, but apparently it's more fun to work it out on your own.

Finally, it's not very complex, but here's the code that I used to Generate my test XML. Included really only for completeness: // Generates Clickatell standard XML
public static string GenerateClickATellXml(string number, string fromdisplay, string bodytext)
{
    StringBuilder sb = new StringBuilder();
    StringWriter sw = new StringWriter(sb);
    XmlTextWriter writer = new XmlTextWriter(sw);

    //Write the root element
    writer.WriteStartElement("clickAPI");

    // write the sendMsg element
    writer.WriteStartElement("sendMsg");

    //Write sub-elements
    writer.WriteElementString("api_id", accountApiId);
    writer.WriteElementString("user", accountLogin);
    writer.WriteElementString("password", accountPassword);
    writer.WriteElementString("to", number);
    writer.WriteElementString("text", bodytext);
    writer.WriteElementString("from", fromdisplay);

    // end the sendMsg element
    writer.WriteEndElement();

    // end the root element
    writer.WriteEndElement();

    //Write the XML to file and close the writer
    writer.Close();

    return sb.ToString();
}

Obviously accountApiId, accountLogin, and accountPassword are all variables which are being populated elsewhere. The above example is the simplest to use for testing purposes, as it authenticates and sends a message all in one post, compared to the other methods which require to you authenticate in order to receive a session id which you then use to send subsequent messages.

So there you have it. Incredibly simple when you look back on it.

Some further links:
Link: Clickatell API guides
Link: TraderSystems: PHP XML Clickatell example
Link: Basic C# HTTP POST Client

 Print | Posted on Monday, December 04, 2006 3:56 PM |



Feedback

Gravatar

# re: Working with the Clickatell XML API from .NET / C#

THanks a million. Have mine working but it is not very reliable, and i think the problem is the encoding...

Do you have anything code to analize the returned result for errors ? Such as analising the xml for specific error codes from CLickatell ?

2/22/2008 8:22 PM | Cobus

Gravatar

# re: Working with the Clickatell XML API from .NET / C#

Your clickatell guide link has changed, I found it at http://support.clickatell.com/guides.php

6/28/2008 9:03 PM | Justin

Gravatar

# re: Working with the Clickatell XML API from .NET / C#

Quite inspiring,



Anyway, thanks for the post

8/14/2009 5:27 AM | Web developer

Gravatar

# re: Working with the Clickatell XML API from .NET / C#

Quite inspiring,

Do you have anything code to analize the returned result for errors ? Such as analising the xml for specific error codes from CLickatell ?

Anyway, thanks for the post

8/14/2009 5:44 AM | Web developer


Post Comment

Title  
Name  
Email
Website / Url
 

Your comment

   
Ensure the word in this box says 'orange':
 
Please add 5 and 8 and type the answer here:





Due to excessive comment spam, all comments are now being moderated. If you're a comment spammer then you're wasting your time here. Your comments will not be published - ever.


About me

My name is Ross Hawkins and I'm a developer, consultant, business owner and writer based in Auckland, New Zealand (pictured below!). My current work revolves around ASP.NET, C#, jQuery, Ajax, SQL Server, and a mix of other Microsoft development technologies.

I also have about 15 years of experience with IBM Lotus Notes/Domino and associated technologies. While Notes/Domino is no longer my primary focus I still like to dabble and keep my skills up to date.

I own and run 2 businesses - Hawkins Consulting Services, and Ignition Development.

Bethells Beach, located in sunny West Auckland, New Zealand




Subscribe

Subscribe to this feed


Search




Popular Content

Troubleshooting WebResource.axd

The .NET 2.0 framework changed the way clientside JavaScript is delivered to the browser. Previously, ASP.NET 1.1 used the aspnet_client directory whereas now 2.0 uses WebResource.axd.

Published on October 8, 2006

jQuery Wildcard Selectors - some simple examples

I wrote about jQuery wildcard selector syntax briefly back in 2009, and since then that post has received a lot of views – way more than a post that brief should ever have seen..

Published on October 14, 2011

Microsoft AJAX Extensions: Sys.Debug is null or not an object

One of the breaking changes which was made with the 1.0 release of the Microsoft Ajax Extensions was the renaming of the 'Debug' class to 'Sys.Debug' for reasons of compatiability with other frameworks. Breaking changes like this can often be a source of frustration..

Published on May 22, 2007

Simple ASP.NET Character Counter

A textbox character counter is a pretty simple piece of functionality, and there's a lot of different ways to apply one to your application. The following method is nice and simple, and can be done using only clientside JavaScript if required, or combined with server side code in order to create a more dynamic effect

Published on December 4, 2006

Simple ASP.NET Character Counter - with Master Page Support

A quick update to my previous character counter article adding some changes for those using it with Master Pages.

Published on February 7th, 2009

Adding Tooltips to Gridview Headers

As the title says, this is a very simple but dynamic way of achieving tooltip text on a header column. It's not overly flash, but it's lightweight and quick to implement.

Published on April 15, 2007

SQL Server Web Report Viewer Issues on Windows 2008 Server/IIS7

A fix for another AXD related issue, this time with the SQL Server Web Report Viewer Control which was being served up via IIS7 on a Windows 2008 server.

Published on June 2, 2007
Updated on April 10, 2008





Archives

May, 2012 (1)
April, 2012 (4)
March, 2012 (2)
February, 2012 (4)
January, 2012 (3)
December, 2011 (3)
November, 2011 (8)
October, 2011 (9)
September, 2011 (8)
August, 2011 (5)
July, 2011 (4)
June, 2011 (7)
May, 2011 (5)
April, 2011 (3)
March, 2011 (8)
February, 2011 (4)
January, 2011 (3)
December, 2010 (8)
November, 2010 (5)
October, 2010 (6)
September, 2010 (7)
August, 2010 (11)
July, 2010 (12)
June, 2010 (8)
May, 2010 (8)
April, 2010 (4)
March, 2010 (8)
February, 2010 (6)
January, 2010 (12)
December, 2009 (13)
November, 2009 (11)
October, 2009 (12)
September, 2009 (12)
August, 2009 (2)
July, 2009 (7)
June, 2009 (12)
May, 2009 (9)
April, 2009 (9)
March, 2009 (9)
February, 2009 (8)
January, 2009 (7)
December, 2008 (6)
November, 2008 (7)
October, 2008 (9)
September, 2008 (12)
August, 2008 (9)
July, 2008 (6)
June, 2008 (24)
May, 2008 (13)
April, 2008 (16)
March, 2008 (8)
February, 2008 (10)
January, 2008 (1)
December, 2007 (14)
November, 2007 (11)
October, 2007 (11)
September, 2007 (13)
August, 2007 (11)
July, 2007 (5)
June, 2007 (15)
May, 2007 (11)
April, 2007 (9)
March, 2007 (9)
February, 2007 (10)
January, 2007 (8)
December, 2006 (18)
November, 2006 (11)
October, 2006 (14)
September, 2006 (9)
August, 2006 (10)
July, 2006 (4)
June, 2006 (4)
May, 2006 (6)
April, 2006 (3)
February, 2006 (6)
January, 2006 (10)
September, 2005 (2)
August, 2005 (4)

Post Categories

ASP.NET
AJAX
Amusing
NZ
NZ Trains
Notes/Domino
Visual Studio
Web Development
Miscellaneous
Me
Rugby
C#
SQL