.NET Class for handling Twitter Updates and Rate Checks

0 views
Skip to first unread message

ch...@stuffworldwide.com

unread,
Nov 10, 2009, 5:49:25 PM11/10/09
to Twitter Development Talk
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

}

}

Andrew Badera

unread,
Nov 10, 2009, 5:53:18 PM11/10/09
to twitter-deve...@googlegroups.com
I for one tend to prefer Google Code or Code Plex for posting lengthy
chunks of code intended for resharing ...

Also, LinqToTwitter is a pretty solid reference implementation ...
FWIW. (Not affiliated, just a user.)

∞ Andy Badera
+1 518-641-1280 Google Voice
∞ This email is: [ ] bloggable [x] ask first [ ] private
∞ Google me: http://www.google.com/search?q=andrew%20badera

ch...@stuffworldwide.com

unread,
Nov 12, 2009, 2:03:30 PM11/12/09
to Twitter Development Talk
I sent it to the twitter people to post on their site but they asked
me to post here as well... I was like... okay

On Nov 10, 5:53 pm, Andrew Badera <and...@badera.us> wrote:
> I for one tend to prefer Google Code or Code Plex for posting lengthy
> chunks of code intended for resharing ...
>
> Also, LinqToTwitter is a pretty solid reference implementation ...
> FWIW. (Not affiliated, just a user.)
>
> ∞ Andy Badera
> ∞ +1 518-641-1280 Google Voice
> ∞ This email is: [ ] bloggable [x] ask first [ ] private
> ∞ Google me:http://www.google.com/search?q=andrew%20badera
>
> On Tue, Nov 10, 2009 at 5:49 PM, ch...@stuffworldwide.com
>
>
>
> <ch...@stuffworldwide.com> wrote:
>

> > Not many .NETexamples out there... here it is... have fun...

> > }- Hide quoted text -
>
> - Show quoted text -

ryan alford

unread,
Nov 12, 2009, 2:10:37 PM11/12/09
to twitter-deve...@googlegroups.com
You are suppose to post it on a code repository site (like CodePlex or Google Code), then post a link to it here.  Nobody wants 300 lines of code in their emails.

Ryan

John Meyer

unread,
Nov 12, 2009, 7:29:16 PM11/12/09
to twitter-deve...@googlegroups.com

In as bad as that is, at least it’s not 300 lines of Nigerian officers asking you to move $2 million for them. ;-)

No virus found in this incoming message.
Checked by AVG - www.avg.com
Version: 8.5.425 / Virus Database: 270.14.61/2497 - Release Date: 11/12/09 07:38:00

Andrew Badera

unread,
Nov 12, 2009, 7:31:03 PM11/12/09
to twitter-deve...@googlegroups.com
A modicum of common sense would drive one to post appropriate content
in appropriate places ... and link to it.

∞ Andy Badera
+1 518-641-1280 Google Voice
∞ This email is: [ ] bloggable [x] ask first [ ] private
∞ Google me: http://www.google.com/search?q=andrew%20badera

Reply all
Reply to author
Forward
0 new messages