使用代码在twitter上发送消息

时间:2013-01-29 12:08:57

标签: javascript asp.net twitter

我想从我的.net应用程序向twitter用户发送消息。我怎样才能做到这一点?有没有api或javascript代码可用于此?请有人帮助我。

2 个答案:

答案 0 :(得分:1)

您可以使用twitter按钮插件来实现此目的。它基于javascript。请参阅this链接

答案 1 :(得分:0)

code代码段用于包装Twitter API以发送消息。它不是一个完整的Twitter包装器,但它可以轻松地使用Twitter发送推文消息。 有两个重载的构造函数,其中两个都需要传递用户名和密码,并且代理服务器用作重载参数。

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();
}
}
}