by nolovelust
15. March 2010 10:56
Here is another code from my learn by creating approach :P
Code below is beginner friendly and slightly changed version of http://www.dnknormark.net/post/C-TwitPic-API-client.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="twitpic.aspx.cs" Inherits="twitpic" %>
<!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>
<style type="text/css">
.style1
{
width: 100%;
}
.style2
{
width: 110px;
}
</style>
</head>
<body>
<form id="form1" runat="server">
<asp:Label ID="upres" runat="server" Text="" style="text-align: center"></asp:Label>
<table class="style1" runat="server" ID="uptable">
<tr>
<td class="style2">
<asp:Label ID="Label1" runat="server" Text="Twitter username"></asp:Label>
</td>
<td>
<asp:TextBox ID="un" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td class="style2">
<asp:Label ID="Label2" runat="server" Text="Twitter password"></asp:Label>
</td>
<td>
<asp:TextBox ID="pw" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td class="style2">
<asp:Label ID="Label3" runat="server" Text="Twitter message"></asp:Label>
</td>
<td>
<asp:TextBox ID="m" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td class="style2">
<asp:Label ID="Label4" runat="server" Text="Image for TwitPic"></asp:Label>
</td>
<td>
<asp:FileUpload ID="img" runat="server" />
</td>
</tr>
<tr>
<td class="style2">
</td>
<td>
<asp:Button ID="Button1" runat="server" Text="Post" onclick="Button1_Click" />
</td>
</tr>
</table>
</form>
</body>
</html>
and code behind
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Net;
using System.Text;
using System.IO;
public partial class twitpic : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
string ct = img.PostedFile.ContentType.ToString();
string _un = un.Text;
string _pw = pw.Text;
string _m = m.Text;
uptable.Visible = false;
HttpPostedFile myFile = img.PostedFile;
string fileName = myFile.FileName.ToString();
int nFileLen = myFile.ContentLength;
byte[] myData = new byte[nFileLen];
myFile.InputStream.Read(myData, 0, nFileLen);
TwitPic tw = new TwitPic();
upres.Text= tw.UploadPhoto(myData, ct, _m, fileName, _un, _pw).ToString();
}
public class TwitPic
{
private const string TWITPIC_UPLADO_API_URL = "http://twitpic.com/api/upload";
private const string TWITPIC_UPLOAD_AND_POST_API_URL = "http://twitpic.com/api/uploadAndPost";
///
/// Uploads the photo and sends a new Tweet
///
/// The binary image data.
/// The tweet message.
/// The filename.
/// Return true, if the operation was succeded.
public string UploadPhoto(byte[] binaryImageData, string ContentType, string tweetMessage, string filename, string Username, string Password)
{
// Documentation: http://www.twitpic.com/api.do
string boundary = Guid.NewGuid().ToString();
string requestUrl = String.IsNullOrEmpty(tweetMessage) ? TWITPIC_UPLADO_API_URL : TWITPIC_UPLOAD_AND_POST_API_URL;
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(requestUrl);
string encoding = "iso-8859-1";
request.PreAuthenticate = true;
request.AllowWriteStreamBuffering = true;
request.ContentType = string.Format("multipart/form-data; boundary={0}", boundary);
request.Method = "POST";
string header = string.Format("--{0}", boundary);
string footer = string.Format("--{0}--", boundary);
StringBuilder contents = new StringBuilder();
contents.AppendLine(header);
string fileContentType = ContentType;
string fileHeader = String.Format("Content-Disposition: file; name=\"{0}\"; filename=\"{1}\"", "media", filename);
string fileData = Encoding.GetEncoding(encoding).GetString(binaryImageData);
contents.AppendLine(fileHeader);
contents.AppendLine(String.Format("Content-Type: {0}", fileContentType));
contents.AppendLine();
contents.AppendLine(fileData);
contents.AppendLine(header);
contents.AppendLine(String.Format("Content-Disposition: form-data; name=\"{0}\"", "username"));
contents.AppendLine();
contents.AppendLine(Username);
contents.AppendLine(header);
contents.AppendLine(String.Format("Content-Disposition: form-data; name=\"{0}\"", "password"));
contents.AppendLine();
contents.AppendLine(Password);
if (!String.IsNullOrEmpty(tweetMessage))
{
contents.AppendLine(header);
contents.AppendLine(String.Format("Content-Disposition: form-data; name=\"{0}\"", "message"));
contents.AppendLine();
contents.AppendLine(tweetMessage);
}
contents.AppendLine(footer);
byte[] bytes = Encoding.GetEncoding(encoding).GetBytes(contents.ToString());
request.ContentLength = bytes.Length;
using (Stream requestStream = request.GetRequestStream())
{
requestStream.Write(bytes, 0, bytes.Length);
using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
{
using (StreamReader reader = new StreamReader(response.GetResponseStream()))
{
string result = reader.ReadToEnd();
XDocument doc = XDocument.Parse(result);
XElement rsp = doc.Element("rsp");
string status = rsp.Attribute(XName.Get("status")) != null ? rsp.Attribute(XName.Get("status")).Value : rsp.Attribute(XName.Get("stat")).Value;
string mediaurl = rsp.Element("mediaurl").Value;
//return status.ToUpperInvariant().Equals("OK");
return mediaurl;
}
}
}
}
}
}