Skip to content

Post on twitter/kwippy using .NET

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.

Related posts:

  1. Twitter background generator version 0.1 Working on a very basic twitter background generator, right now...

Related posts brought to you by Yet Another Related Posts Plugin.

{ 4 } Comments

  1. robin | March 4, 2009 at 2:58 am | Permalink

    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 */}
    }

  2. Dipankar Sarkar | March 4, 2009 at 3:04 am | Permalink

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

  3. robin | March 4, 2009 at 3:22 am | Permalink

    @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?

  4. Jeff Tardif | April 22, 2009 at 8:08 pm | Permalink

    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)

Post a Comment

Your email is never published nor shared. Required fields are marked *