| 1 |
using System; |
| 2 |
using System.IO; |
| 3 |
using System.Net; |
| 4 |
|
| 5 |
|
| 6 |
namespace Toast.Twitter |
| 7 |
{ |
| 8 |
public class TweetController |
| 9 |
{ |
| 10 |
private static Uri Url = new Uri("http://twitter.com/statuses/update.xml"); |
| 11 |
|
| 12 |
private string passWord; |
| 13 |
|
| 14 |
public TweetController(string userName, string passWord) |
| 15 |
{ |
| 16 |
if (userName == null) |
| 17 |
{ |
| 18 |
throw new ArgumentNullException("userName"); |
| 19 |
} |
| 20 |
|
| 21 |
if (passWord == null) |
| 22 |
{ |
| 23 |
throw new ArgumentNullException("passWord"); |
| 24 |
} |
| 25 |
|
| 26 |
passWord = passWord; |
| 27 |
UserName = userName; |
| 28 |
} |
| 29 |
|
| 30 |
public ReplyStatus PostMessage(string Message) |
| 31 |
{ |
| 32 |
string Status = null; |
| 33 |
string encMes = System.Web.HttpUtility.UrlEncode(Message); |
| 34 |
|
| 35 |
HttpWebRequest req = (HttpWebRequest)HttpWebRequest.Create(Url); |
| 36 |
req.Method = "POST"; |
| 37 |
req.Credentials = new NetworkCredential(UserName, passWord); |
| 38 |
|
| 39 |
req.ContentType ="application/x-www-form-urlencoded"; |
| 40 |
req.UserAgent = "toast0.1"; |
| 41 |
req.Timeout = 10000; |
| 42 |
|
| 43 |
System.Net.ServicePointManager.Expect100Continue = false; |
| 44 |
|
| 45 |
using (StreamWriter wtr = new StreamWriter(req.GetRequestStream())) |
| 46 |
{ |
| 47 |
wtr.Write("status={0}", encMes); |
| 48 |
wtr.Close(); |
| 49 |
} |
| 50 |
|
| 51 |
|
| 52 |
using (WebResponse response = req.GetResponse()) |
| 53 |
{ |
| 54 |
Status = response.Headers["status"]; |
| 55 |
} |
| 56 |
|
| 57 |
if (Status == null) |
| 58 |
{ |
| 59 |
return ReplyStatus.UnknownReply; |
| 60 |
} |
| 61 |
else if (Status == "200 OK") |
| 62 |
{ |
| 63 |
return ReplyStatus.OK; |
| 64 |
} |
| 65 |
else if (Status == "304 Not Modified") |
| 66 |
{ |
| 67 |
return ReplyStatus.NotModified; |
| 68 |
} |
| 69 |
else if (Status == "400 Bad Request") |
| 70 |
{ |
| 71 |
return ReplyStatus.BadRequest; |
| 72 |
} |
| 73 |
else if (Status == "401 Not Authorized") |
| 74 |
{ |
| 75 |
return ReplyStatus.NotAuthorized; |
| 76 |
} |
| 77 |
else if (Status == "403 Forbidden") |
| 78 |
{ |
| 79 |
return ReplyStatus.Forbidden; |
| 80 |
} |
| 81 |
else if (Status == "404 Not Found") |
| 82 |
{ |
| 83 |
return ReplyStatus.NotFound; |
| 84 |
} |
| 85 |
else if (Status == "500 Internal Server Error") |
| 86 |
{ |
| 87 |
return ReplyStatus.InternalServerError; |
| 88 |
} |
| 89 |
else if (Status == "502 Bad Gateway") |
| 90 |
{ |
| 91 |
return ReplyStatus.BadGateWay; |
| 92 |
} |
| 93 |
else if (Status == "503 Service Unavailable") |
| 94 |
{ |
| 95 |
return ReplyStatus.ServiceUnavailable; |
| 96 |
} |
| 97 |
else |
| 98 |
{ |
| 99 |
return ReplyStatus.UnknownReply; |
| 100 |
} |
| 101 |
} |
| 102 |
|
| 103 |
public string UserName |
| 104 |
{ |
| 105 |
get; |
| 106 |
private set; |
| 107 |
} |
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
} |
| 113 |
} |