3

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

4

2 回答 2

1

为此,您可以使用 twitter 按钮插件。它基于javascript。请参考这个链接

于 2013-04-10T09:14:11.677 回答
0

代码片段用于包装 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();
}
}
}
于 2013-01-29T12:11:46.370 回答