Desinerd : Dipankar's Blog

Post on twitter/kwippy using .NET

Published: in Technical, , , , , , , by .

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.

Comments

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

  • 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.