<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Smartkey - Java Software Consultancy &#187; REST</title>
	<atom:link href="http://blog.smartkey.co.uk/tag/rest/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.smartkey.co.uk</link>
	<description></description>
	<lastBuildDate>Thu, 02 Sep 2010 15:25:49 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.4</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>How to update your Twitter status using Java</title>
		<link>http://blog.smartkey.co.uk/2009/10/how-to-update-your-twitter-status-using-java/</link>
		<comments>http://blog.smartkey.co.uk/2009/10/how-to-update-your-twitter-status-using-java/#comments</comments>
		<pubDate>Thu, 15 Oct 2009 14:41:53 +0000</pubDate>
		<dc:creator>Steve Neal</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[Java Programming]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[REST]]></category>
		<category><![CDATA[Twitter]]></category>

		<guid isPermaLink="false">http://blog.smartkey.co.uk/?p=133</guid>
		<description><![CDATA[Twitter provide a RESTful API that allows third party applications to access and use their services. In this post I&#8217;ll show you how to submit a status update using the this API from a Java application. To do this we&#8217;ll need to construct an HTTP POST request. In Java it&#8217;s not simple to do this so [...]]]></description>
			<content:encoded><![CDATA[<p>Twitter provide a RESTful API that allows third party applications to access and use their services. In this post I&#8217;ll show you how to submit a status update using the this API from a Java application. To do this we&#8217;ll need to construct an HTTP POST request. In Java it&#8217;s not simple to do this so the code shown below is a little complex. On the plus side though, there&#8217;s not a lot of it.</p>
<h3>The code:</h3>
<pre class="brush: java">
import org.apache.commons.codec.binary.Base64;
import java.io.*;
import java.util.*;
import java.net.*;

//prepare a connection for the twitter API update method
URL url = new URL(&quot;http://twitter.com/statuses/update.xml&quot;);
URLConnection conn = url.openConnection();
conn.setAllowUserInteraction(false);
conn.setDoOutput(true);

//encode users credentials
byte[] credentialsBytes = (username +&quot;:&quot; + password).getBytes();
byte[] encodedBytes = Base64.encodeBase64(credentialsBytes);
String credentialString = new String(encodedBytes);
conn.setRequestProperty(&quot;Authorization&quot;, &quot;Basic &quot; + credentialString);

//set the request to POST and send
conn.setRequestProperty(&quot;Content-Type&quot;, &quot;application/x-www-form-urlencoded&quot;);
DataOutputStream out = new DataOutputStream(conn.getOutputStream());
out.writeBytes(&quot;status=&quot; + URLEncoder.encode(message, &quot;UTF-8&quot;));
out.flush();
out.close();

//get the response
BufferedReader input = new BufferedReader(
        new InputStreamReader(conn.getInputStream()));
StringBuilder builder = new StringBuilder();
String s = null;
while ((s = input.readLine()) != null) {
    builder.append(s);
}
input.close();
String response = builder.toString();
</pre>
<h3>Prepare a request</h3>
<p>First, a URL for Twitter&#8217;s status update method is created (line 7).</p>
<h3>Authenticate the caller</h3>
<p>Most of the methods in the Twitter API require the caller to authenticate. The simplest way to do this is to use HTTP BASIC authentication. To authenticate a user using BASIC authentication the client must construct a string with the users name and password separated by a colon. For example, for a Twitter user with a user name &#8216;fredsmith&#8217; and a password &#8216;fredspassword&#8217;, the string should look like this:</p>
<pre>fredsmith:fredspassword</pre>
<p>the above should then be base 64 encoded (note that this example uses Apache Commons codec for this) before being prepended with the characters &#8220;Basic: &#8220;. Finally, the encoded credentials string must be set in the Authorization header of the request (lines 13-16). When the server receives the request it will extract the users name and password and authenticate them.</p>
<h3>Use HTTP POST to send the status parameter</h3>
<p>To ensure that the request is sent using POST, the Content-Type header is set (line 19).</p>
<p>Then, the status parameter can have its value UTF encoded and set (line 21).</p>
<h3>Read the response message</h3>
<p>Lines 25-33 read the response from Twitter, by default an XML document, that should confirm that the status for the user has been updated.</p>
<h3>Get Twittering</h3>
<p>The above code has been tested and proven to work in a production environment. If you want to use it yourself then please feel free (you can use the [<strong>&lt;&gt;</strong>] tool above the code to copy it). Any comments or suggestions on how it may be simplified or improved are welcomed.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.smartkey.co.uk/2009/10/how-to-update-your-twitter-status-using-java/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>
