Send tweet message in Twitter using .NET

This code snippet is used to wrap Twitter API to send message. It’s not a complete Twitter wrapper but it can send a tweet message using Twitter easily.

There are two overloaded constructors where both of them requires user name and password to be passed and proxy server is used as the overloaded parameter.

Create a C# wrapper class

using System;
using System.IO;
using System.Net;
using System.Xml;
using System.Web;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Helpers
{
public class TwitterHelpers
{
private string _twitterJsonUrl = "http://twitter.com/statuses/update.json";

public string TwitterJsonUrl
{
get { return _twitterJsonUrl; }
set { _twitterJsonUrl = value; }
}

private string _twitterUser = string.Empty;

public string TwitterUser
{
get { return _twitterUser; }
set { _twitterUser = value; }
}

private string _twitterPass = string.Empty;

public string TwitterPass
{
get { return _twitterPass; }
set { _twitterPass = value; }
}

private string _proxyServer = string.Empty;

public string ProxyServer
{
get { return _proxyServer; }
set { _proxyServer = value; }
}

public string SendTwitterMessage(string message)
{
try
{
HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(TwitterJsonUrl);
System.Net.ServicePointManager.Expect100Continue = false;

string post = string.Empty;
using (TextWriter writer = new StringWriter())
{
writer.Write("status={0}", HttpUtility.UrlEncode(message.Substring(0,140)));
post = writer.ToString();
}

SetRequestParams(request);

request.Credentials = new NetworkCredential(TwitterUser, TwitterPass);

using (Stream requestStream = request.GetRequestStream())
{
using (StreamWriter writer = new StreamWriter(requestStream))
{
writer.Write(post);
}
}

WebResponse response = request.GetResponse();
string content;

using (Stream responseStream = response.GetResponseStream())
{
using (StreamReader reader = new StreamReader(responseStream))
{
content = reader.ReadToEnd();
}
}

return content;

}
catch (Exception ex)
{
throw ex;
}
}

private void SetRequestParams(HttpWebRequest request)
{
request.Timeout = 500000;
request.Method = "POST";
request.ContentType = "application/x-www-form-urlencoded";
request.UserAgent = "My Twitter Application";

if (!string.IsNullOrEmpty(_proxyServer))
{
request.Proxy = new WebProxy(_proxyServer, false);
}

}

public TwitterHelpers(string userName, string userPassword, string proxyServer)
{
_twitterUser = userName;
_twitterPass = userPassword;
_proxyServer = proxyServer;
}

public TwitterHelpers(string userName, string userPassword)
{
_twitterUser = userName;
_twitterPass = userPassword;
}

}
}

Console application to call the wrapper class to post tweet messages to Twitter:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleAppTest
{
class Program
{
private static TwitterHelpers _twitterHelpers = null;

private static TwitterHelpers TwitterHelpers
{
get
{
if (_twitterHelpers == null)
{
_twitterHelpers = new TwitterHelpers("twitteruser", "twitterpassword");
}

return _twitterHelpers;
}
}

static void Main(string[] args)
{
TwitterHelpers.SendTwitterMessage("Hello World!!");
Console.WriteLine("Press any key to continue...");
Console.ReadKey();
}
}
}


Comments
4 Responses to “Send tweet message in Twitter using .NET”
  1. Canerday says:

    Thank you for the good writeup. It if truth be told was a leisure account it. Look complex to more added agreeable from you! However, how can we be in contact?

  2. Faso says:

    Hi there very cool web site!! Man .. Beautiful .. Amazing .. I’ll bookmark your web site and take the feeds additionally…I’m satisfied to search out so many useful info here within the put up, we need work out more strategies on this regard, thanks for sharing. . . . . .

  3. Curz311 says:

    I’m not that much of a online reader to be honest but your sites really nice, keep it up! I’ll go ahead and bookmark your site to come back later on. Many thanks

  4. Jed Delasbour says:

    Eventually, an outstanding web-site that isn’t in my face seeking to frequently sell me something. Cheers, you need to carry on the good work.

  • My Experience

    My expertise are primarily in C#, ASP.NET, HTML, JavaScript, SQL, SEO, Windows Services, XML and Web services. Areas of technical expertise are in Web application design and architecture and security.
  • Quote of the Day !!

    "Be nice to nerds. Chances are you'll end up working for one." – Bill Gates