DotNet Tutorials

V4 Dot Net Tutorials

Server Intellect Cloud Hosting

 ASP.NET Twitter Posting

In this tutorial we will learn how to post a Tweet on Twitter.com using C# and ASP.NET
Introduction
In this tutorial we will learn how to post a Tweet on Twitter.com using ASP.Net and C#.  The Twitter website was built to make it easy for developers to access certain functions on the website like posting.

Step 1. Setting up the Form
1. Create a new Web Form named "Default.aspx".
2. Place a TextBox on the Form called "TextBox1".
3. Place a Button on the Form named "Button1".
4. These are the only controls that we need as we will be Hard Coding the Login info in the Code Behind

We chose Server Intellect for its cloud servers, for our web hosting. They have managed to handle virtually everything for us, from start to finish and their customer service is stellar.

Step 2. The Tweet Code
1. Open up the Code-Behind for the Form "Default.aspx.cs" by double clicking on the button Control.
2. Copy the code below into your program.
3. Find the Lines that look like this:
string username = "YOUR_USER_NAME";
string password = "YOUR_PASSWORD";
and insert your user name and password inside the quotes.

4. Now build then run the program
5. Insert text into the TextBox and click the button, after a few seconds this should post a Tweet onto your account.

If you're ever in the market for some great Windows web hosting, try Server Intellect. We have been very pleased with their services and most importantly, technical support.


 

protected void Button1_Click(object sender, EventArgs e)
{
string username = "YOUR_USER_NAME";
string password = "YOUR_PASSWORD";
string tweet = TextBox1.Text;
try {
// encode the username/password
string user = 
Convert.ToBase64String(System.Text.Encoding.UTF8.GetBytes(username + ":" + password));
// determine what we want to upload as a status
byte[] bytes = System.Text.Encoding.ASCII.GetBytes("status=" + tweet);
// connect with the update page
HttpWebRequest request = 
(HttpWebRequest)WebRequest.Create("http://twitter.com/statuses/update.xml");
// set the method to POST
request.Method="POST";
request.ServicePoint.Expect100Continue = false;
 // thanks to argodev for this recent change!
// set the authorisation levels
request.Headers.Add("Authorization", "Basic " + user);
request.ContentType="application/x-www-form-urlencoded";
// set the length of the content
request.ContentLength = bytes.Length;		
// set up the stream
Stream reqStream = request.GetRequestStream();
// write to the stream
reqStream.Write(bytes, 0, bytes.Length);
// close the stream
reqStream.Close();
} 
catch (Exception ex) 
{/* DO NOTHING */}
}

	
It should look like this once you are completed.

We stand behind Server Intellect and their whole support team, they offer dedicated servers of course, and they now offer the cloud, which is nice. 

Conclusion
In this tutorial we learned how to Post a Tweet on Twitter.com using C# and ASP.NET.  This can come in handy for making custom Twitter web pages, or Twitter Applications.