Twitter API from C#
October 24th, 2008
I did this code to test the API interaction with twitter via .NET. You can see it in action on the Orion's Beltt twitter page. The game logs the result of every battle on that twitter account.
C#:
-
private const string TwitterJsonUrl = "http://twitter.com/statuses/update.json";
-
private const string TwitterUser = "your_user";
-
private const string TwitterPass = "your_pass";
-
-
private static void SendTwitterMessage( string message )
-
{
-
try {
-
HttpWebRequest request = (HttpWebRequest) HttpWebRequest.Create(TwitterJsonUrl);
-
-
string post = string.Empty;
-
writer.Write("status={0}", HttpUtility.UrlEncode(message));
-
post = writer.ToString();
-
Console.WriteLine("Post: {0}", post);
-
}
-
-
SetRequestParams(request);
-
-
-
using( Stream requestStream = request.GetRequestStream() ) {
-
writer.Write(post);
-
}
-
}
-
-
Console.WriteLine("Length: {0}", request.ContentLength);
-
Console.WriteLine("Address: {0}", request.Address);
-
-
WebResponse response = request.GetResponse();
-
string content;
-
-
using( Stream responseStream = response.GetResponseStream() ) {
-
content = reader.ReadToEnd();
-
}
-
}
-
-
Console.WriteLine(content);
-
-
}
-
catch( Exception ex )
-
{
-
Console.WriteLine(ex);
-
}
-
}
-
-
private static void SetRequestParams( HttpWebRequest request )
-
{
-
request.Timeout = 500000;
-
request.Method = "POST";
-
request.ContentType = "application/x-www-form-urlencoded";
-
//request.Referer = "http://www.orionsbelt.eu";
-
request.UserAgent = "Orion's Belt Notifier Bot";
-
#if USE_PROXY
-
#endif
-
}
Related Posts
- CSS Variables Analysis
- You find out about bad usability...
- World of Goo Birthday Results
- The customer's always right!
- LinkedIn Mayhem




October 28th, 2008 at 6:12 pm
obrigado pelo código.
December 26th, 2008 at 2:50 pm
I appreciate your work and this code is very easy to understand but I think it doesn’t work anymore. Since a change in the Twitter API (arround christmas …).
It now returns every time you try to update your status a 417 HTTP error.
December 27th, 2008 at 1:13 pm
I’ve found the solution on this blog: http://blogs.msdn.com/shitals/archive/2008/12/27/9254245.aspx
Just add this line of code above your twitter call so it runs:
System.Net.ServicePointManager.Expect100Continue = false;
April 19th, 2009 at 7:39 pm
Just wanna add something here. Need a fast and clean proxy? Try proxy.my
Unblock friendster, facebook, myspace and even youtube!
April 22nd, 2009 at 11:33 pm
[...] Twittern via C# - "low level" You can use HttpRequest & HttpResponse objects to use twitter: Sample here. [...]
May 11th, 2009 at 5:19 am
This is really going to help me at school.
May 27th, 2009 at 1:39 am
thats great that you are talking about the twitter api,a good example of searching with the twitter api is on twiogle.com because you can search on twitter and google at the same time.
May 28th, 2009 at 3:03 pm
Hi ,
what is the best way to get the following information with Twitter API without getting Login Credentials for twitterUserA:
1)suppose I have a user called twitterUserA
I want to get detailed information for people who are following him and his followers.
2)so i need Followersname, FollowerID, FollowersFollowerCount,FollowersFollowingCount,followersUpdates#,and other info if possible.
what is the fastest and best way to get information for a given twitter account without asking for twitter Log in credentials (username and password)
thanks
Bally
June 25th, 2009 at 7:07 am
Thanks For keeping it on point!
Best Regards
September 22nd, 2009 at 11:44 am
Valew mesmo mano…
Thanks a lot bro…
February 3rd, 2010 at 1:06 pm
Hi thanks for an perceptive post, I actually found your blog by mistake while looking on Goole for something else closely related, in any event before i ramble on too much i would just like to say how much I loved your post, I have bookmarked your site and also taken your RSS feed, Again thank you very much for the post keep up the great work.
April 27th, 2010 at 11:51 am
Interesting, your’s is the second site today that I’ve visited with this opportunity. I’ll keep an eye on it.
May 7th, 2010 at 11:25 pm
Thank you for a nice little read.
May 19th, 2010 at 2:16 pm
Excellent information here. This interesting post made me smile. Maybe if you throw in a couple of pics it will make the whole thing more interesting.
June 12th, 2010 at 3:48 pm
this is nice, but I think you don’t need those timeouts.
June 13th, 2010 at 8:10 pm
not only dat. since Basic Authentication will be banned by Twitter, you have to use oAuth or xAuth….. -caballero