<?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>Ian Mercer &#187; URL</title>
	<atom:link href="http://blog.abodit.com/tag/url/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.abodit.com</link>
	<description>Living in the World&#039;s Smartest House</description>
	<lastBuildDate>Sat, 07 Jan 2012 19:50:56 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
		<item>
		<title>Lengthening short Urls in C#</title>
		<link>http://blog.abodit.com/2010/07/lengthening-short-urls-in-csharp/</link>
		<comments>http://blog.abodit.com/2010/07/lengthening-short-urls-in-csharp/#comments</comments>
		<pubDate>Mon, 05 Jul 2010 22:08:00 +0000</pubDate>
		<dc:creator>Ian Mercer</dc:creator>
				<category><![CDATA[.NET]]></category>
		<category><![CDATA[URL]]></category>

		<guid isPermaLink="false">http://blog.abodit.com/?p=977</guid>
		<description><![CDATA[If you deal with Twitter APIs, sooner or later, you&#8217;ll probably want to lengthen those short URLs back to long URLs. There&#8217;s a couple of services you can use to do this but it&#8217;s really quite easy so you might as well do it yourself. The code below follows each redirect until it reaches a <a href="http://blog.abodit.com/2010/07/lengthening-short-urls-in-csharp/" class="more-link">More &#62;</a>]]></description>
			<content:encoded><![CDATA[<p>If you deal with Twitter APIs, sooner or later, you&#8217;ll probably want to lengthen those short URLs back to long URLs.  There&#8217;s a couple of services you can use to do this but it&#8217;s really quite easy so you might as well do it yourself.  The code below follows each redirect until it reaches a real page (or an error), it then returns the last URL it finds.  </p>
<pre class="brush: csharp; title: ; notranslate">
        private string UrlLengthen(string url)
        {
            string newurl = url;

            bool redirecting = true;

            while (redirecting)
            {

                try
                {
                    HttpWebRequest request = (HttpWebRequest)WebRequest.Create(newurl);
                    request.AllowAutoRedirect = false;
                    request.UserAgent = &quot;Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.1.3) Gecko/20090824 Firefox/3.5.3 (.NET CLR 4.0.20506)&quot;;
                    HttpWebResponse response = (HttpWebResponse)request.GetResponse();
                    if ((int)response.StatusCode == 301 || (int)response.StatusCode == 302)
                    {
                        string uriString = response.Headers[&quot;Location&quot;];
                        Log.Debug(&quot;Redirecting &quot; + newurl + &quot; to &quot; + uriString + &quot; because &quot; + response.StatusCode);
                        newurl = uriString;
                        // and keep going
                    }
                    else
                    {
                        Log.Debug(&quot;Not redirecting &quot; + url + &quot; because &quot; + response.StatusCode);
                        redirecting = false;
                    }
                }
                catch (Exception ex)
                {
                    ex.Data.Add(&quot;url&quot;, newurl);
                    Exceptions.ExceptionRecord.ReportCritical(ex);
                    redirecting = false;
                }
            }
            return newurl;
        }
</pre>
]]></content:encoded>
			<wfw:commentRss>http://blog.abodit.com/2010/07/lengthening-short-urls-in-csharp/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Shortened URLs should be treated like a Codec &#8230;</title>
		<link>http://blog.abodit.com/2009/10/shortened-urls-should-be-treated-like-a-codec/</link>
		<comments>http://blog.abodit.com/2009/10/shortened-urls-should-be-treated-like-a-codec/#comments</comments>
		<pubDate>Sun, 11 Oct 2009 05:14:00 +0000</pubDate>
		<dc:creator>Ian Mercer</dc:creator>
				<category><![CDATA[.NET]]></category>
		<category><![CDATA[Commentary]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[URL]]></category>

		<guid isPermaLink="false">http://blog.abodit.com/?p=43</guid>
		<description><![CDATA[Codecs are used to compress data to send over the wire, but when it gets to the other end it is decoded back to its original form for display. Shortened URLs are used to compress long URLs to send over Twitter. So why aren&#8217;t they expanded again on the other side? Why do Twitter clients <a href="http://blog.abodit.com/2009/10/shortened-urls-should-be-treated-like-a-codec/" class="more-link">More &#62;</a>]]></description>
			<content:encoded><![CDATA[<p>Codecs are used to compress data to send over the wire, but when it gets to the other end it is decoded back to its original form for display.</p>
<p>Shortened URLs are used to compress long URLs to send over Twitter.</p>
<p>So why aren&#8217;t they expanded again on the other side?  Why do Twitter clients show short urls instead of expanding them back out to a long URL, or at least a display that tells you what the real web site is.</p>
<p>This has implications for reducing attacks hidden in short URLs and for allowing you to see at a glance that &#8220;Check out http://bit.ly/a567as&#8221; is actually the same site you&#8217;ve already read once today from another Tweet.</p>
<p>So for my own Twitter client I decided to expand all shortened URLs as far as I could go.  Here&#8217;s a code snippet to do that:-</p>
<p><!--<br />
{\rtf1\ansi\ansicpg\lang1024\noproof65001\uc1 \deff0{\fonttbl{\f0\fnil\fcharset0\fprq1 Courier New;}}{\colortbl;??\red0\green0\blue0;\red255\green255\blue255;\red128\green128\blue128;\red0\green128\blue0;\red0\green0\blue255;\red163\green21\blue21;\red43\green145\blue175;}??\fs20         \cf3 ///\cf4  \cf3 &lt;summary&gt;\par ??\cf0         \cf3 ///\cf4  Follow any redirects to get back to the original URL\par ??\cf0         \cf3 ///\cf4  TODO: Move this to Utility\par ??\cf0         \cf3 ///\cf4  \cf3 &lt;/summary&gt;\par ??\cf0         \cf5 private\cf0  \cf5 string\cf0  UrlLengthen(\cf5 string\cf0  url)\par ??        \{\par ??            \cf5 string\cf0  newurl = url;\par ??\par ??            \cf5 bool\cf0  redirecting = \cf5 true\cf0 ;\par ??\par ??            \cf5 while\cf0  (redirecting)\par ??            \{\par ??\par ??                \cf5 try\par ??\cf0                 \{\par ??                    HttpWebRequest request = (HttpWebRequest)WebRequest.Create(newurl);\par ??                    request.AllowAutoRedirect = \cf5 false\cf0 ;\par ??                    request.UserAgent = \cf6 "Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.1.3) Gecko/20090824 Firefox/3.5.3 (.NET CLR 4.0.20506)"\cf0 ;\par ??                    HttpWebResponse response = (HttpWebResponse)request.GetResponse();\par ??                    \cf5 if\cf0  ((\cf5 int\cf0 )response.StatusCode == 301 || (\cf5 int\cf0 )response.StatusCode == 302)\par ??                    \{\par ??                        \cf5 string\cf0  uriString = response.Headers[\cf6 "Location"\cf0 ];\par ??                        Log.Debug(\cf6 "Redirecting "\cf0  + newurl + \cf6 " to "\cf0  + uriString + \cf6 " because "\cf0  + response.StatusCode);\par ??                        newurl = uriString;\par ??                        \cf4 // and keep going\par ??\cf0                     \}\par ??                    \cf5 else\par ??\cf0                     \{\par ??                        Log.Debug(\cf6 "Not redirecting "\cf0  + url + \cf6 " because "\cf0  + response.StatusCode);\par ??                        redirecting = \cf5 false\cf0 ;\par ??                    \}\par ??                \}\par ??                \cf5 catch\cf0  (\cf7 Exception\cf0  ex)\par ??                \{\par ??                    ex.Data.Add(\cf6 "url"\cf0 , newurl);\par ??                    Exceptions.ExceptionRecord.ReportCritical(ex);\par ??                    redirecting = \cf5 false\cf0 ;\par ??                \}\par ??            \}\par ??            \cf5 return\cf0  newurl;\par ??        \}\par ??}<br />
--></p>
<div style="font-family: Courier New; font-size: 10pt; color: black; background: white;">
<p style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: gray;">///</span><span style="color: green;"> </span><span style="color: gray;">&lt;summary&gt;</span></p>
<p style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: gray;">///</span><span style="color: green;"> Follow any redirects to get back to the original URL</span></p>
<p style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: gray;">///</span><span style="color: green;"> </span><span style="color: gray;">&lt;/summary&gt;</span></p>
<p style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">private</span> <span style="color: blue;">string</span> UrlLengthen(<span style="color: blue;">string</span> url)</p>
<p style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; {</p>
<p style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">string</span> newurl = url;</p>
<p style="margin: 0px;">&nbsp;</p>
<p style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">bool</span> redirecting = <span style="color: blue;">true</span>;</p>
<p style="margin: 0px;">&nbsp;</p>
<p style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">while</span> (redirecting)</p>
<p style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; {</p>
<p style="margin: 0px;">&nbsp;</p>
<p style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">try</span></p>
<p style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; {</p>
<p style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; HttpWebRequest request = (HttpWebRequest)WebRequest.Create(newurl);</p>
<p style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; request.AllowAutoRedirect = <span style="color: blue;">false</span>;</p>
<p style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; request.UserAgent = <span style="color: #a31515;">&#8220;Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.1.3) Gecko/20090824 Firefox/3.5.3 (.NET CLR 4.0.20506)&#8221;</span>;</p>
<p style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; HttpWebResponse response = (HttpWebResponse)request.GetResponse();</p>
<p style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">if</span> ((<span style="color: blue;">int</span>)response.StatusCode == 301 || (<span style="color: blue;">int</span>)response.StatusCode == 302)</p>
<p style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; {</p>
<p style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">string</span> uriString = response.Headers[<span style="color: #a31515;">"Location"</span>];</p>
<p style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; Log.Debug(<span style="color: #a31515;">&#8220;Redirecting &#8220;</span> + newurl + <span style="color: #a31515;">&#8221; to &#8220;</span> + uriString + <span style="color: #a31515;">&#8221; because &#8220;</span> + response.StatusCode);</p>
<p style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; newurl = uriString;</p>
<p style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: green;">// and keep going</span></p>
<p style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }</p>
<p style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">else</span></p>
<p style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; {</p>
<p style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; Log.Debug(<span style="color: #a31515;">&#8220;Not redirecting &#8220;</span> + url + <span style="color: #a31515;">&#8221; because &#8220;</span> + response.StatusCode);</p>
<p style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; redirecting = <span style="color: blue;">false</span>;</p>
<p style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }</p>
<p style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }</p>
<p style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">catch</span> (<span style="color: #2b91af;">Exception</span> ex)</p>
<p style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; {</p>
<p style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; ex.Data.Add(<span style="color: #a31515;">&#8220;url&#8221;</span>, newurl);</p>
<p style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; Exceptions.ExceptionRecord.ReportCritical(ex);</p>
<p style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; redirecting = <span style="color: blue;">false</span>;</p>
<p style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }</p>
<p style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }</p>
<p style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">return</span> newurl;</p>
<p style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }</p>
</div>
]]></content:encoded>
			<wfw:commentRss>http://blog.abodit.com/2009/10/shortened-urls-should-be-treated-like-a-codec/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

