
For the past few weeks I’ve been exploring the Twitter API and have been setting down some examples on how to communicate with the Twitter system using C# and the .NET Framework.
This post ties my previous examples together by posting to the Twitter API update method which is a member of the status class. A full description of the Twitter API can be found here.
If you haven’t seen my previous posts please feel free to read them here: HTTP Posting to Twitter Part 1 and HTTP Posting to Twitter Part 2. I’m also more than happy to accept comments on all my articles.
Below is a segment of code that I have taken from the class Status which contains all of the methods that are contained within Twitter’s status class. Within this example I have focused on the update method.
As you can see the Status class directly inherits from a class that I have named Twitter. The Twitter class contains the overridden ExecuteString method demonstrated in HTTP Posting to Twitter Part 2.
public class Status : Twitter
{..
public string update(string status)
{
return ExecuteString(new PostingParameters("statuses/update.xml",
String.Format("status={0}&source={1}",
HttpUtility.UrlEncode(status),
HttpUtility.UrlEncode(Config.Source))), true,
Method.Post);
}
..}
The first parameter accepted by the ExecuteString method is a class named PostingParameters. This class contains the properties URL and Message. These two properties store all of the information that is posted to Twitter.
The URL property is the relative URL of the method call on the Twitter website. The Message property contains the GET or POST variables the API call requires. Please note that the source variable is no longer accepted by the Twitter update method in favour of OAuth security validation.
The last two parameters that are required by ExecuteString are LoginRequired and an enum named Method. As you can see by the method call the user needs to be logged into the system in order to post a Twitter update. The method also needs to be posted to Twitter, so the HTTP request type Method.Post is set. All of these parameters are passed to the HttpRequest class in HTTP Posting to Twitter Part 1 which adds all of the correct credentials to the HTTP call.
In response Twitter returns an XML string that contains a number of parameters. These are documented in the online Twitter API documents. What I do with the returned XML will be documented in a future post to this blog.
Please feel free to leave a comment or get in contact. I am more than happy to receive your feedback.
About James McLeod, Managing Director at Narvi Digital Media, Brighton



[...] If you want to read related articles they can be found here: HTTP Posting to Twitter Part 1 and HTTP Posting to Twitter Part 3 The code example below overrides the virtual method ExecuteString within [...]
By: HTTP Posting to Twitter Part 2 « James McLeod on November 4, 2009
at 10:37 pm
[...] The second and third articles can be found here: HTTP Posting to Twitter Part 2 and HTTP Posting to Twitter Part 3 By using the HttpWebRequest class I have created a number of methods that manage http posts and [...]
By: HTTP Posting to Twitter Part 1 « James McLeod on November 4, 2009
at 10:44 pm