Post on twitter/kwippy using .NET

March 3rd, 2009 § 4 comments

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.IO;

using System.Net;

using System.Web;

namespace ConsoleApplication1

{

class Program

{

static void Main(string[] args)

{

System.Net.ServicePointManager.Expect100Continue = false;

Uri address = new Uri(http://twitter.com/statuses/update.json);

// Create the web request

HttpWebRequest request = WebRequest.Create(address) as HttpWebRequest;

request.Method = “POST”;

request.ContentType = “application/x-www-form-urlencoded”;

request.Credentials = new NetworkCredential(“username”, “password”);

StringBuilder data = new StringBuilder();

data.Append(“status=from%20.net”);

// Create a byte array of the data we want to send

byte[] byteData = UTF8Encoding.UTF8.GetBytes(data.ToString());

// Set the content length in the request headers

request.ContentLength = byteData.Length;

using (Stream postStream = request.GetRequestStream())

{

postStream.Write(byteData, 0, byteData.Length);

}

using (HttpWebResponse response = request.GetResponse() as HttpWebResponse)

{

// Get the response stream

StreamReader reader = new StreamReader(response.GetResponseStream());

// Console application output

Console.WriteLine(reader.ReadToEnd());

}

}

}

}

Code to post on twitter and kwippy using this simple C# program :) . Njoi.

Tagged , , , , , ,

§ 4 Responses to Post on twitter/kwippy using .NET"

  • robin says:

    Hey man, does this mean the kwippy post url is “http://twitter.com/statuses/update.json“ and works the same as this one?

    i use this to post tweets from my apps:

    public static void PostTweet(string username, string password, string tweet)
    {
    try
    {
    string user = Convert.ToBase64String(System.Text.Encoding.UTF8.GetBytes(username + “:” + password));
    byte[] bytes = System.Text.Encoding.ASCII.GetBytes(“status=” + tweet);
    HttpWebRequest request = (HttpWebRequest)WebRequest.Create(“http://twitter.com/statuses/update.xml”);
    request.ServicePoint.Expect100Continue = false;
    request.Method = “POST”;
    request.Headers.Add(“Authorization”, “Basic ” + user);
    request.ContentType = “application/x-www-form-urlencoded”;
    request.ContentLength = bytes.Length;
    request.UserAgent = “MyApp”;
    Stream reqStream = request.GetRequestStream();
    reqStream.Write(bytes, 0, bytes.Length);
    reqStream.Close();
    }
    catch (Exception ex) {/* DO NOTHING */}
    }

  • Wow :D experience show …. though the solution i posted is much more cleaner wrt the HTTP authentication.

  • robin says:

    @Dipankar Sarkar
    i know, my kode is oldskool :P but ey, if it works, i don’t touch it anymo :P but, if i replace the url, i could post a kwip?

  • Jeff Tardif says:

    Ok works fine, thank you :)

    But now, if I want to check if the user has entered the good login information before posting? I want the user to ‘login” twitter on my website before posting, how can I check if he entered the good password?

    (I want to use Twitter login info as login on my website to check if the user is really the one he told me he is)

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="">