title: Post message from ASP.NET app to Twitter
body: <p>I am using very simple code to post message in twitter. The code is as given below:</p>
  
  <pre><code>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);
          }
  }
  </code></pre>
  
  <p>I am not sure why this code is not working. I am getting the exception 
      The remote server returned an error: (401) Unauthorized.
  But, whichever site I am referring, everywhere I find that people are able to post their messages using the same code.</p>
  
  <p>Can anybody please tell me what I am missing in this code? The site that I referred for this code is <a href="http://www.dreamincode.net/forums/topic/120468-twitter-api-posting-status/" rel="nofollow">http://www.dreamincode.net/forums/topic/120468-twitter-api-posting-status/</a></p>
  
  <p>Thanks in advance....</p>
  
  <p>Hari</p>