C# TwitPic API client

I’ve spent some time lately, playing around with the Twitter API. And along with that belongs the TwitPic’s API. I’m using Twitter a lot, to stay in touch with tech news, other developers and just for fun. But it’s getting more and more used for a lot of different things, and I needed it to integrate with an E-commerce platform I’m developing.

The code for post a picture to TwitPic looks like this:

///

/// URL for the TwitPic API's upload method/// privateconststring TWITPIC_UPLADO_API_URL = "http://twitpic.com/api/upload"; /// /// URL for the TwitPic API's upload and post method/// privateconststring TWITPIC_UPLOAD_AND_POST_API_URL = "http://twitpic.com/api/uploadAndPost"; /// /// Uploads the photo and sends a new Tweet/// /// The binary image data./// The tweet message./// The filename./// Return true, if the operation was succeded.publicbool UploadPhoto(byte[] binaryImageData, string tweetMessage, string filename) { // Documentation: http://www.twitpic.com/api.dostring boundary = Guid.NewGuid().ToString(); string requestUrl = String.IsNullOrEmpty(tweetMessage) ? TWITPIC_UPLADO_API_URL : TWITPIC_UPLOAD_AND_POST_API_URL; HttpWebRequest request = (HttpWebRequest)WebRequest.Create(requestUrl); string encoding = "iso-8859-1"; request.PreAuthenticate = true; request.AllowWriteStreamBuffering = true; request.ContentType = string.Format("multipart/form-data; boundary={0}", boundary); request.Method = "POST"; string header = string.Format("--{0}", boundary); string footer = string.Format("--{0}--", boundary); StringBuilder contents = new StringBuilder(); contents.AppendLine(header); string fileContentType = GetImageContentType(filename); string fileHeader = String.Format("Content-Disposition: file; name="{0}"; filename="{1}"", "media", filename); string fileData = Encoding.GetEncoding(encoding).GetString(binaryImageData); contents.AppendLine(fileHeader); contents.AppendLine(String.Format("Content-Type: {0}", fileContentType)); contents.AppendLine(); contents.AppendLine(fileData); contents.AppendLine(header); contents.AppendLine(String.Format("Content-Disposition: form-data; name="{0}"", "username")); contents.AppendLine(); contents.AppendLine(this.Username); contents.AppendLine(header); contents.AppendLine(String.Format("Content-Disposition: form-data; name="{0}"", "password")); contents.AppendLine(); contents.AppendLine(this.Password.ToInsecureString()); if (!String.IsNullOrEmpty(tweetMessage)) { contents.AppendLine(header); contents.AppendLine(String.Format("Content-Disposition: form-data; name="{0}"", "message")); contents.AppendLine(); contents.AppendLine(tweetMessage); } contents.AppendLine(footer); byte[] bytes = Encoding.GetEncoding(encoding).GetBytes(contents.ToString()); request.ContentLength = bytes.Length; using (Stream requestStream = request.GetRequestStream()) { requestStream.Write(bytes, 0, bytes.Length); using (HttpWebResponse response = (HttpWebResponse)request.GetResponse()) { using (StreamReader reader = new StreamReader(response.GetResponseStream())) { string result = reader.ReadToEnd(); XDocument doc = XDocument.Parse(result); XElement rsp = doc.Element("rsp"); string status = rsp.Attribute(XName.Get("status")) != null ? rsp.Attribute(XName.Get("status")).Value : rsp.Attribute(XName.Get("stat")).Value; return status.ToUpperInvariant().Equals("OK"); } } } }