Reputation: 2108
I want to send a message to a twitter user from my .net application. How can I achieve this? Is there any api or javascript code available for this?. Please anyone help me.
Upvotes: 3
Views: 1146
Reputation:
you can use twitter button plugins for this purpose. Its based on javascript. Please refer this link
Upvotes: 1
Reputation:
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.
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;
}
}
}
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();
}
}
}
Upvotes: 0