Wednesday, June 24, 2009

Twitter asp.net 2.0, C# sample

I created following application to post in Twitter by using twitter apis..in asp.net 2.0, C#
 
 
<%
@ Page Language="C#" AutoEventWireup="true" CodeFile="Default5.aspx.cs" Inherits="Default5" %>

<!

DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<

html xmlns="http://www.w3.org/1999/xhtml" >

<

head runat="server">

<title>Untitled Page</title>

</

head>

<

body>

<form id="form1" runat="server">

<div>

<table style="width: 50%" align="center">

<tr>

<td>

</td>

<td>

</td>

<td>

</td>

</tr>

<tr>

<td align="right">

<asp:Label ID="Label2" runat="server" Text="User Id:"></asp:Label></td>

<td>

<asp:TextBox ID="txtusrid" runat="server" Width="144px"></asp:TextBox>

</td>

<td>

</td>

</tr>

<tr>

<td align="right">

<asp:Label ID="Label3" runat="server" Text="Password:"></asp:Label></td>

<td>

<asp:TextBox ID="txtpass" runat="server" TextMode="Password"></asp:TextBox></td>

<td>

</td>

</tr>

<tr>

<td align="right" style="height: 40px">

<asp:Label ID="Label1" runat="server" Text="Messages:"></asp:Label></td>

<td style="height: 40px">

<asp:TextBox ID="txtmessage" runat="server" TextMode="MultiLine" Width="259px" Height="99px"></asp:TextBox>

</td>

<td style="height: 40px">

</td>

</tr>

<tr>

<td style="height: 21px">

</td>

<td style="height: 21px">

<asp:Button ID="Button1" runat="server" Text="Submit" OnClick="Button1_Click" />

<asp:Label ID="Label4" runat="server" Width="126px"></asp:Label></td>

<td style="height: 21px">

</td>

</tr>

</table>

</div>

</form>

</

body>
</
html>
 
Code behind

using

System;

using

System.Net;

using

System.Web;

using

System.IO;

public

partial class Default5 : System.Web.UI.Page

{

protected void Page_Load(object sender, EventArgs e)

{

}

public void SubmitUserMsg(string username, string passwd, string txttweet)

{

string user = Convert.ToBase64String(System.Text.Encoding.UTF8.GetBytes(username + ":" + passwd));

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

HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://twitter.com/statuses/update.xml");

request.Method =

"POST";

request.ServicePoint.Expect100Continue = false;

request.Headers.Add(

"Authorization", "Basic " + user);

request.ContentType =

"application/x-www-form-urlencoded";

request.ContentLength = bytes.Length;

Stream reqStream = request.GetRequestStream();

reqStream.Write(bytes, 0, bytes.Length);

reqStream.Close();

Label4.Text =

"Message Posted";

}

protected void Button1_Click(object sender, EventArgs e)

{

SubmitUserMsg(txtusrid.Text, txtpass.Text, txtmessage.Text);

}

}

No comments: