4 Replies - 1419 Views - Last Post: 16 August 2009 - 11:26 AM Rate Topic: -----

#1 broli  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 15-August 09

Twitter API Posting Status

Post icon  Posted 15 August 2009 - 10:06 PM

Hi Everyone,

So I been writing my own custom Twitter wrapper for .NET using C# and seem to be having an issue when it comes to posting the status.

Googling around, I found several different codes for the posting method but I cannot get it to work in my application.

Here is what I have for the post status function according to Twitter online API documentation:

public void UpdateStatus(string status)
		{

			byte[] bytes = System.Text.Encoding.ASCII.GetBytes("status=" + status);

			try
			{
				HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://twitter.com/statuses/update.xml");
				request.Credentials = credential;
				request.Method = "POST";
				request.ContentType = "application/x-www-form-urlencoded";
				request.Headers.Add("Authorization", "Basic " + credential.UserName);
				request.ContentLength = bytes.Length;
				request.ServicePoint.Expect100Continue = false;


				Stream requestStream = request.GetRequestStream();
				requestStream.Write(bytes, 0, bytes.Length);
				requestStream.Close();
			}
			catch { }
		}





What am I missing? Thanks.

This post has been edited by broli: 15 August 2009 - 10:44 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Twitter API Posting Status

#2 PsychoCoder  Icon User is offline

  • Google.Sucks.Init(true);
  • member icon

Reputation: 1641
  • View blog
  • Posts: 19,853
  • Joined: 26-July 07

Re: Twitter API Posting Status

Posted 15 August 2009 - 10:29 PM

Are you receiving any errors? Does this code not work that way you intended it? When asking for help there are a couple items that are vital in order for someone to properly help you:
  • Post the code you're having problems with
  • Post the exact error you're receiving, if you are receiving one
  • If no error explain what the code is doing versus what you want it to do
  • Post your question in the body of your post, not the description field

Was This Post Helpful? 0
  • +
  • -

#3 broli  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 15-August 09

Re: Twitter API Posting Status

Posted 15 August 2009 - 10:49 PM

Sorry, edited top post to move question out of code.

I am not receiving errors for the code above. Basically I have this function linked to a button click event where it will take an input text from a text box and post it to your twitter account status.

I been using HttpWebRequest class for most of the methods. I have no issues when it comes to "GET" methods as it will return an XML where I can parse the information for data.

The post seem to be not posting at all. The codes above basically sets all the required information for the post process and encrypts the status message before sending.

If anyone has any experience with the Twitter REST api and know anything about using their status post api, would appreciate some input. Thanks.

This post has been edited by broli: 15 August 2009 - 10:55 PM

Was This Post Helpful? 0
  • +
  • -

#4 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6078
  • View blog
  • Posts: 23,546
  • Joined: 23-August 08

Re: Twitter API Posting Status

Posted 16 August 2009 - 08:56 AM

You shouldn't need to add an Authorization header, as that should be done by setting the credential to a non-null value.

The main problem is you haven't yet made any transaction with the server. You need to call request.GetResponse() to get that going. Here's how I would update your code some (untested, off the top of my head, may need tweaking, caveat your mama, etc., etc.):

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

    try
    {
        HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://twitter.com/statuses/update.xml");
        request.Credentials = credential;
        request.Method = "POST";
        request.ContentType = "application/x-www-form-urlencoded";
        
        using (StreamWriter sw = new StreamWriter(request.GetRequestStream())
        {
               string statusUpdate = "status=" + System.Web.HttpUtility.UrlEncode(status);
               sw.Write(statusUpdate);
        }
        using (HttpWebResponse resp = request.GetResponse())
        {
            // Handle your 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);
    }
}

Was This Post Helpful? 0
  • +
  • -

#5 broli  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 15-August 09

Re: Twitter API Posting Status

Posted 16 August 2009 - 11:26 AM

Awesome...adding the GetResponse fixes the issue without too much alteration to the original code. Thanks alot.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1