C# School Assignment? Project Due Tomorrow? Chat LIVE With A Programming Expert!

 

Code Snippets

  

C# Source Code


Welcome to Dream.In.Code
Become a C# Expert!

Join 300,288 C# Programmers for FREE! Get instant access to thousands of C# experts, tutorials, code snippets, and more! There are 2,074 people online right now. Registration is fast and FREE... Join Now!



Submitted By: gabehabe
Actions:
Rating:
Views: 5,386

Language: C#

Last Modified: January 21, 2009
Instructions: Simply pass the username, password and the "tweet" to the function.

Remember, tweets can be no longer than 140 characters, or they'll get cut off.

You should be using:
using System;
using System.Net;
using System.Web;
using System.IO;

Snippet


  1. /*
  2. * A function to post an update to Twitter programmatically
  3. * Author: Danny Battison
  4. * Contact: gabehabe@hotmail.com
  5. */
  6.  
  7. /// <summary>
  8. /// Post an update to a Twitter acount
  9. /// </summary>
  10. /// <param name="username">The username of the account</param>
  11. /// <param name="password">The password of the account</param>
  12. /// <param name="tweet">The status to post</param>
  13. public static void PostTweet(string username, string password, string tweet)
  14. {
  15.         try {
  16.                 // encode the username/password
  17.                 string user = Convert.ToBase64String(System.Text.Encoding.UTF8.GetBytes(username + ":" + password));
  18.                 // determine what we want to upload as a status
  19.                 byte[] bytes = System.Text.Encoding.ASCII.GetBytes("status=" + tweet);
  20.                 // connect with the update page
  21.                 HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://twitter.com/statuses/update.xml");
  22.                 // set the method to POST
  23.                 request.Method="POST";
  24.                 request.ServicePoint.Expect100Continue = false; // thanks to argodev for this recent change!
  25.                 // set the authorisation levels
  26.                 request.Headers.Add("Authorization", "Basic " + user);
  27.                 request.ContentType="application/x-www-form-urlencoded";
  28.                 // set the length of the content
  29.                 request.ContentLength = bytes.Length;
  30.                
  31.                 // set up the stream
  32.                 Stream reqStream = request.GetRequestStream();
  33.                 // write to the stream
  34.                 reqStream.Write(bytes, 0, bytes.Length);
  35.                 // close the stream
  36.                 reqStream.Close();
  37.         } catch (Exception ex) {/* DO NOTHING */}
  38. }

Copy & Paste


Comments


argodev 2008-12-31 15:37:21

This is great, but you need to add this line due to a recent change in the twitter API... request.ServicePoint.Expect100Continue = false; Otherwise, you will get a continual HTTP 417 Failed Expectation error

gabehabe 2009-01-02 07:42:47

Oooh, thanks~ I noticed it stopped working recently, and I wondered why that was. ^^

ptamzz 2009-06-21 01:17:41

~cool.. m adding this on my site!!~

gabehabe 2009-06-21 01:22:06

What site? If you want to republish code you need the author's permission.

steve_007 2009-10-05 04:18:05

this code results me an exception Unable to connect to the remote server A socket operation encountered a dead network 168.143.162.100:80 Kindly help!!


Add comment


You must be registered and logged on to </dream.in.code> to leave comments.





Live C# Help!

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter Fan Us On Facebook

C# Tutorials

Reference Sheets

C# Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month