Not many .NET examples out there... here it is... have fun...

using System;

using System.Text;

using System.Net;

using System.IO;

using System.Xml;



namespace Tweeter

{

      public class TwitterTools

      {

            #region Members

            #endregion

            public TwitterTools()

            {

                  this.Initialize();

            }

            public TwitterTools(string userName,string password)

            {

                  this.UserName=userName;

                  this.Password=password;



                  this.Initialize();

            }

            private void Initialize()

            {

            }

            public void Dispose()

            {

            }

            #region Properties

            public string UserName=null;

            public string Password=null;

            #endregion

            #region Methods

            public int Update(string message)

            {

                  int retval=0;

                  string code=null;

                  string url="http://twitter.com/account/
rate_limit_status.xml";      //gen.Get("twitterRateService");



                  string result=null;

                  try

                  {

                        result=this.Request(url + "?"

                              ,null

                              ,"GET"

                              );

//                      gen.Test(result);

                  }

                  catch

                  {

                        result=null;

                  }



                  if(result==null)

                        retval=2;

                  else

                  {

                        //parse results

                        try

                        {

                              XmlDocument doc=new XmlDocument();

                              doc.LoadXml(result);



                              XmlNodeList nodes=doc.SelectNodes("/hash/
remaining-hits");

                              int remaining=System.Convert.ToInt32
(nodes[0].InnerText);

                              if(remaining<=0)

                                    retval=2;



                              nodes=null;

                              doc=null;

                        }

                        catch

                        {

                              retval=2;

                        }



                        if(retval!=2)

                        {

                              StringBuilder txt=new StringBuilder();

                              txt.Append("status=");

                              txt.Append(message);

                              code=txt.ToString();



                              try

                              {

                                    string ret=this.Request("http://
twitter.com/statuses/update.xml"//gen.Get("twitterUpdateService")

                                          ,code);



                                    if(ret!=null)

                                          retval=1;

                                    else

                                          retval=0;

                              }

                              catch

                              {

                                    retval=0;

                              }

                        }

                  }



                  return retval;

            }

            private string Request(string url,string code)

            {

                  return this.Request(url,code,"POST");

            }

            private string Request(string url,string code,string
method)

            {

                  byte[] bytes=null;

                  if(code!=null)

                        bytes=System.Text.Encoding.ASCII.GetBytes
(code);



                  string logon=null;

                  if(code==null)

                        logon="Basic " + this.UserName + ":" +
this.Password;

                  else

                        logon=this.UserName + ":" + this.Password;

                  logon=System.Convert.ToBase64String
(System.Text.Encoding.UTF8.GetBytes(logon));



                  //request

                  HttpWebRequest req=(HttpWebRequest)WebRequest.Create
(url);

                  req.Timeout=10000;

                  //req.Timeout=System.Convert.ToInt32(gen.Get
("twitterTimeout"));

                  if(code==null)

                        req.Headers.Add("Authorization",logon);

                  else

                        req.Headers.Add("Authorization","Basic " +
logon);

                  req.ServicePoint.Expect100Continue=false;

                  if(code!=null)

                  {

                        req.ContentType="application/x-www-form-
urlencoded";

                        req.Method=method;

                        req.ContentLength=code==null ? 0 :
bytes.Length;



                        Stream trans=req.GetRequestStream();

                        trans.Write(bytes,0,bytes.Length);

                        trans.Close();

                  }



                  //get response

                  string result=null;

                  try

                  {

                        HttpWebResponse resp=(HttpWebResponse)
req.GetResponse();

                        StreamReader sr=new StreamReader
(resp.GetResponseStream(),Encoding.GetEncoding(1252));



                        //get results

                        result=sr.ReadToEnd();

                        resp.Close();

                  }

                  catch(WebException ex)

                  {

                        HttpWebResponse resp=((HttpWebResponse)
ex.Response);

                        string test=resp.StatusCode.ToString();

//                      gen.Test("! - Twitter query return error code:
" + test);

                        result=null;

                  }

                  catch(Exception ex)

                  {

//                      gen.Test("! - " + ex.Message);

                        result=null;

                  }



                  return result;

            }

            #endregion

      }

}

Reply via email to