views:

1125

answers:

4

I don't want to do anything fancy on Twitter except post to it via my site once a day. I have searched around a bit and there are all sorts of super-complex ways to do every little thing that Twitter does, but there seems to be little documentation on how to do the simplest thing, which is make a post!

Does anyone know how to do this? Or can you at least point me in the right direction? I don't need full wrappers or anything (http://apiwiki.twitter.com/Libraries#C/NET), just one simple function that will post to Twitter.

Thanks!

A: 

There are a couple different ways of doing this, they vary depending on the tools you want to use and have access to. Option 1 will work right out of the box, but the coding can be complicated. Option 3 you will have to download tools for, but once there installed and loaded you should be able to consume the twitter api very quickly.

  1. Use WebRequest.Create to create/send messages to remote endpoints
  2. Use WCF, create a mirror endpoint and access the twitter api using client only endpoint.
  3. Use the WCF REST Starter Kit Preview 2, which has a new class called the HttpClient. I would have to recommend this technique if you can. Here is a great video Consuming a REST Twitter Feed in under 3 minutes.

Here is a sample of using the WCF REST Starter Kit's HttpClient:

    public void CreateFriendship(string friend)
    {
        using (var client = new HttpClient())
        {
            var url = string.Format("http://www.twitter.com/friendships/create/{0}.xml?follow=true", friend);
            client.Post(url)
                .CheckForTwitterError()
                .EnsureStatusIs(HttpStatusCode.OK);
        }
    }

Add a comment if you'd like more info about a particular method.

Update:

For Option #1 see this question: http://stackoverflow.com/questions/547347/remote-http-post-with-csharp

bendewey
yes, i would. i don't care about friends. i want to update my status!
Jason
ugh... it says i need VS2008. i have VS2005...
Jason
See my update. Let me know how that helps you, if not, I can provide some code.
bendewey
no, it doesn't really. i need to know twitter-specific stuff... i don't know anything about REST or webrequests or anything, although i have learned a lot about it today! is there any way i could just get a simple function that posts to twitter? that's it, really: Sub UpdateTwitterStatus(status as string) ... End Sub
Jason
A: 

Its fairly simple; you just need to post an xml file to a web page using webrequest.create. This example is close (assumes you have the xml for the message in another place and just pass it into twitterxml variable as a string. The url might not be the right one; found it on this [page][1] which defines the interface

WebRequest req = null;
   WebResponse rsp = null;
   try
   {
    string twitterXML = "xml as string";
    string uri = "http://twitter.com/statuses/update.format";
    req = WebRequest.Create(uri);
    //req.Proxy = WebProxy.GetDefaultProxy(); // Enable if using proxy
    req.Method = "POST";        // Post method
    req.ContentType = "text/xml";     // content type
    // Wrap the request stream with a text-based writer
    StreamWriter writer = new StreamWriter(req.GetRequestStream());
    // Write the XML text into the stream
    writer.WriteLine(twitterXML);
    writer.Close();
    // Send the data to the webserver
    rsp = req.GetResponse();

   }

[1]: http://apiwiki.twitter.com/Twitter-REST-API-Method%3A-statuses update

u07ch
this helps take me some of the way, but what is the format for your twitterXML variable? i can't just sent them "status update!" as a string... i'm sure they need it in some format
Jason
the link has the twitter xml format
u07ch
+3  A: 

This is the easiest implementation ever. Up and running in under 2 minutes:

http://code.google.com/p/twitterizer/

Jason
+1 Cool Framework.
bendewey
FYI, this uses option 1 from my answer. In future projects I would take a look at WCF if your using .NET v3+
bendewey
NOTE: TWITTERIZER DOES NOT WORK IN MEDIUM TRUST ENVIRONMENTS... CONSIDER YOURSELF WARNED
Jason
ok i think they updated it and now it does work in med trust
Jason
A: 
Sheraz