从ASP.NET应用程序向Twitter发布消息

从ASP.NET应用程序向Twitter发布消息,twitter,Twitter,我使用非常简单的代码在twitter上发布消息。代码如下所示: public void UpdateStatus(string username, string password, string tweetMsg) { byte[] bytes = System.Text.Encoding.ASCII.GetBytes("status=" + tweetMsg); try { HttpWebRequest request = (HttpWeb

我使用非常简单的代码在twitter上发布消息。代码如下所示:

public void UpdateStatus(string username, string password, string tweetMsg)
{
     byte[] bytes = System.Text.Encoding.ASCII.GetBytes("status=" + tweetMsg);

      try
      {
         HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://twitter.com/statuses/update.xml");
         request.Credentials = new NetworkCredential(username, password);
         request.Method = "POST";
         request.ContentType = "application/x-www-form-urlencoded";
         request.ServicePoint.Expect100Continue = false;

         string statusUpdate = string.Empty;

         using (StreamWriter sw = new StreamWriter(request.GetRequestStream()))
         {
              statusUpdate = "status=" + System.Web.HttpUtility.UrlEncode(tweetMsg);
              sw.Write(statusUpdate);
         }

         using (HttpWebResponse resp = (HttpWebResponse) request.GetResponse())
         {
             // Handle response here
              using (StreamReader sr = new StreamReader(resp.GetResponseStream()))
              {
                        Console.WriteLine(sr.ReadToEnd());
              }
         }
       }
       catch (WebException ex)
       {
                Console.WriteLine("Web Error: {0}", ex.Message);
                if (ex.Response != null)
                {
                    HttpWebResponse r = (HttpWebResponse)ex.Response; 
                    using (StreamReader esr = new StreamReader(r.GetResponseStream()))
                    {
                        Console.WriteLine("Error: {0}", esr.ReadToEnd());
                    } 
                }
        }
        catch (IOException ex)
        {
                 Console.WriteLine("I/O Error: {0}", ex.Message);
        }
}
我不知道为什么这个代码不起作用。我得到了一个例外 远程服务器返回错误:(401)未经授权。 但是,无论我指的是哪一个网站,我发现人们都可以使用相同的代码发布他们的消息

有人能告诉我这段代码缺少什么吗?我为这个代码引用的站点是

提前谢谢


Hari

Twitter API不再支持用于身份验证的基本身份验证。您需要切换到OAuth,它使用令牌而不是密码