<?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>Dave Amenta .com &#187; URLDecode</title>
	<atom:link href="http://www.daveamenta.com/tag/urldecode/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.daveamenta.com</link>
	<description>(dot (at dave daveamenta) com)</description>
	<lastBuildDate>Tue, 31 Jan 2012 17:22:23 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>C#: URLEncode and URLDecode?</title>
		<link>http://www.daveamenta.com/2008-05/c-urlencode-and-urldecode/</link>
		<comments>http://www.daveamenta.com/2008-05/c-urlencode-and-urldecode/#comments</comments>
		<pubDate>Thu, 08 May 2008 00:33:13 +0000</pubDate>
		<dc:creator>Dave</dc:creator>
				<category><![CDATA[Quick Tips]]></category>
		<category><![CDATA[.NET]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[URLDecode]]></category>
		<category><![CDATA[URLEncode]]></category>

		<guid isPermaLink="false">http://www.daveamenta.com/?p=33</guid>
		<description><![CDATA[Often one needs to make a call to a webserver in .NET, and just as often there is Querystring data that needs to be sent along with this request. Thanksfully .NET has a great WebClient class in System.net.WebClient.  But you can&#8217;t just add the querystring data into your URL like such: Wrong: WebClient.DownloadFile&#40;&#34;http://www.daveamenta.com/file.aspx?mydata=this is a [...]]]></description>
			<content:encoded><![CDATA[<p style="text-align: center;"><a href="http://www.daveamenta.com/wp-content/uploads/2008/05/vs2.png"><img class="size-medium wp-image-34 aligncenter" title="vs2" src="http://www.daveamenta.com/wp-content/uploads/2008/05/vs2.png" alt="" width="108" height="69" /></a></p>
<p style="text-align: left;">Often one needs to make a call to a webserver in .NET, and just as often there is Querystring data that needs to be sent along with this request.  Thanksfully .NET has a great <em>WebClient</em> class in System.net.WebClient.  But you can&#8217;t just add the querystring data into your URL like such:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;">Wrong<span style="color: #008000;">:</span>
WebClient<span style="color: #008000;">.</span><span style="color: #0000FF;">DownloadFile</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;http://www.daveamenta.com/file.aspx?mydata=this is a string that I need to send to the server?&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span></pre></div></div>

<p style="text-align: left;">The data needs to be <em>encoded</em> so that the server will not misinterpret the data.  This is an example of using an encoded string:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;">Right<span style="color: #008000;">:</span>
WebClient<span style="color: #008000;">.</span><span style="color: #0000FF;">DownloadFile</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;http://www.daveamenta.com/file.aspx?mydata=this+is+a+string+that+I+need+to+send+to+the+server%3F&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span></pre></div></div>

<p style="text-align: left;">The second string is encoded properly, this way the webserver will properly parse that last <strong>?</strong>.  This was done using the URLEncode function, .NET has built-in classes for this&#8211;but they are a bit hidden.  They&#8217;re kept in the System.Web namespace.  This means that if you are working with .NET on the desktop side, as opposed to ASP.NET, you&#8217;ll need to <em>Add a Reference</em> to <strong>System.Web.dll</strong>.  Once you do, you&#8217;ll see new classes in System.Web that you can use for many things.</p>
<p style="text-align: left;"><strong>Using URLEncode in C#:</strong></p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;">WebClient<span style="color: #008000;">.</span><span style="color: #0000FF;">DownloadFile</span><span style="color: #008000;">&#40;</span> <span style="color: #666666;">&quot;http://www.daveamenta.com/file.aspx?mydata=&quot;</span> <span style="color: #008000;">+</span>
<span style="color: #000000;">System.<span style="color: #0000FF;">Web</span></span><span style="color: #008000;">.</span><span style="color: #0000FF;">HttpUtility</span><span style="color: #008000;">.</span><span style="color: #0000FF;">UrlEncode</span><span style="color: #008000;">&#40;</span> <span style="color: #666666;">&quot;this is a string that I need to send to the server?&quot;</span> <span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span></pre></div></div>

<p style="text-align: left;">It is unlikely you will have much use for URLDecode in a C# application, but it is applied the same way.</p>
<p style="text-align: left;"><strong>What exactly is URLEncode?</strong></p>
<p style="text-align: left;">URLEncode returns a string in which all non-alphanumeric characters <em>other than &#8211; (hyphen) _ (underscore), and . (period)</em> have been replaced with a percent    (<em>%</em>) sign followed by two hex digits that correspond to the charactor code and spaces encoded    as plus (<em>+</em>) signs. Spaces can alternatively be encoded as %20.  20 is (16*2)+(1*0)=32 in decimal, which is the <a href="http://en.wikipedia.org/wiki/ASCII#ASCII_printable_characters">ASCII code</a> for space.</p>
<p style="text-align: left;">URLDecode will reverse this operation.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.daveamenta.com/2008-05/c-urlencode-and-urldecode/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
	</channel>
</rss>

