As part of my ongoing social media integration research I’ve created my own C# Twitter API in order to understand how the system interacts with external applications. The following is an extract of the fundamental communications hub of the Twitter API.protected virtual string ExecuteString(PostingParameters PostingParameters, bool LoginRequired, Method Method)
{
// Create a new HttpWebRequest object for sending http data to the
// URL set in the class PostingParameters
HttpWebRequest Request =
(HttpWebRequest)HttpWebRequest.Create
(PostingParameters.URL);
// Gets or sets a Boolean value that determines whether
// 100-Continue behaviour is used.
Request.ServicePoint.Expect100Continue = false;
// GetMethodType is a method created for setting the
// Request.Method string according to the Method Enum
// passed through the parameters. These can be Post,
// Get, or Delete within this class
Request.Method = GetMethodType(Method);
// If Twitter requires a login for a particular post, this will
// set the security credentials
if (LoginRequired)
{
if (this.Username != String.Empty && this.Password != String.Empty)
{
Request.Credentials = new NetworkCredential(this.Username, this.Password);
}
else
{
throw new ApplicationException(“Login Credentials Required”);
}
}
PostMessage(ref PostingParameters, ref Request);
return GetResponse(ref Request);
}
private void PostMessage(ref PostingParameters PostingParameters, ref HttpWebRequest Request)
{
byte[] Bytes = null;
if (PostingParameters.Message != null)
{
Request.ContentType = “application/x-www-form-urlencoded”;
// Convert the message to a byte array
// before the message is sent to Twitter
Bytes = System.Text.Encoding.ASCII.GetBytes
(PostingParameters.Message);
// Set the ContentLength of the request to the size of // the byte array
Request.ContentLength = Bytes.Length;
// Create a new Stream. The ‘using’ will destroy
// the object if an exception is thrown.
using (Stream RequestStream = Request.GetRequestStream())
{
RequestStream.Write(Bytes, 0, Bytes.Length);
RequestStream.Close();
}
}
}
private string GetResponse(ref HttpWebRequest Request)
{
string ReturnValue = String.Empty;
// Get the Response from the Request object
WebResponse Response = Request.GetResponse();
// Read the Response into a new Stream. The ‘using’
// will destroy the object if an exception is thrown
using (StreamReader Reader =
new StreamReader(Response.GetResponseStream()))
{
// Stream the response into the ReturnValue string
ReturnValue = Reader.ReadToEnd();
Reader.Close();
}
return ReturnValue;
}
About James McLeod, Managing Director at Narvi Digital Media, Brighton



[...] object and process. If you missed out on the last article it can be found here: HTTP Posting to Twitter Part 1 The code example below overrides the virtual method ExecuteString within [...]
By: HTTP Posting to Twitter Part 2 « James McLeod on October 26, 2009
at 10:08 pm
Interesting code snippets. Would you care for a code review?
If my team wrote code like that, they’d have quite some feedback…
By: Andy Fryer on October 29, 2009
at 9:40 am
Hey Andy,
Thanks for the feedback. It’ll be interesting to hear your views, good or bad
By: followmcleod on October 29, 2009
at 11:17 am
[...] 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 [...]
By: HTTP Posting to Twitter Part 3 « James McLeod on November 25, 2009
at 9:52 pm