<?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; WebClient</title>
	<atom:link href="http://www.daveamenta.com/tag/webclient/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>Search Urban Dictionary with C#</title>
		<link>http://www.daveamenta.com/2010-01/search-urban-dictionary-with-c/</link>
		<comments>http://www.daveamenta.com/2010-01/search-urban-dictionary-with-c/#comments</comments>
		<pubDate>Sat, 23 Jan 2010 00:44:28 +0000</pubDate>
		<dc:creator>Dave</dc:creator>
				<category><![CDATA[Code Snippets]]></category>
		<category><![CDATA[HTML Scraping]]></category>
		<category><![CDATA[RegEx]]></category>
		<category><![CDATA[Urban Dictionary]]></category>
		<category><![CDATA[WebClient]]></category>

		<guid isPermaLink="false">http://www.daveamenta.com/?p=282</guid>
		<description><![CDATA[Here&#8217;s a quick example of how one might scrape the search results from Urban Dictionary.  This shows some nice use of Regular Expressions and WebClient. (View Text) Thankfully Urban Dictionary is a very scrape-friendly site, finding results is as easy as locating two div&#8217;s and extracting the contents. UrbanDictionary.Search(string); will return a list of key-value [...]]]></description>
			<content:encoded><![CDATA[<p>Here&#8217;s a quick example of how one might scrape the search results from <a href="http://www.urbandictionary.com">Urban Dictionary</a>.  This shows some nice use of Regular Expressions and WebClient.</p>
<p><a href="http://www.daveamenta.com/wp-content/uploads/2010/01/code.png"><img class="aligncenter size-full wp-image-289" title="code" src="http://www.daveamenta.com/wp-content/uploads/2010/01/code.png" alt="" width="500" height="350" /></a></p>
<p>(<a href="/ud_code.txt">View Text</a>)</p>
<p>Thankfully Urban Dictionary is a very scrape-friendly site, finding results is as easy as locating two div&#8217;s and extracting the contents.</p>
<p>UrbanDictionary.Search(string); will return a list of key-value pairs which contain the word as the key, and the Urban Dictionary definition as the value.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.daveamenta.com/2010-01/search-urban-dictionary-with-c/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>C#: WebClient Usage</title>
		<link>http://www.daveamenta.com/2008-05/c-webclient-usage/</link>
		<comments>http://www.daveamenta.com/2008-05/c-webclient-usage/#comments</comments>
		<pubDate>Sat, 10 May 2008 02:38:35 +0000</pubDate>
		<dc:creator>Dave</dc:creator>
				<category><![CDATA[Code Snippets]]></category>
		<category><![CDATA[Quick Tips]]></category>
		<category><![CDATA[.NET]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[WebClient]]></category>

		<guid isPermaLink="false">http://www.daveamenta.com/?p=45</guid>
		<description><![CDATA[Microsoft has provided a great utility since .NET 1.0 for doing quick I/O via websites.  The WebClient class provides basic functionality for downloading from and uploading to webservers.  the WebClient example on MSDN leaves a lot to be desired, especially for beginners.  I&#8217;d like to expand on that, as well as provide some example code. [...]]]></description>
			<content:encoded><![CDATA[<p><img src="http://www.daveamenta.com/wp-content/uploads/2008/05/filesave-150x150.png" border="0" alt="" align="right" />Microsoft has provided a great utility since .NET 1.0 for doing quick I/O via websites.  The <strong>WebClient</strong> class provides basic functionality for downloading from and uploading to webservers.  the WebClient example on MSDN leaves a lot to be desired, especially for beginners.  I&#8217;d like to expand on that, as well as provide some example code.</p>
<p><strong>C#: Using WebClient to fetch a page:</strong></p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;">        <span style="color: #008080; font-style: italic;">// create a new instance of WebClient</span>
        WebClient client <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> WebClient<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
        <span style="color: #008080; font-style: italic;">// set the user agent to IE6</span>
        client<span style="color: #008000;">.</span><span style="color: #0000FF;">Headers</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Add</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;user-agent&quot;</span>, <span style="color: #666666;">&quot;Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; .NET CLR 1.0.3705;)&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
        <span style="color: #0600FF; font-weight: bold;">try</span>
        <span style="color: #008000;">&#123;</span>
            <span style="color: #008080; font-style: italic;">// actually execute the GET request</span>
            <span style="color: #6666cc; font-weight: bold;">string</span> ret <span style="color: #008000;">=</span> client<span style="color: #008000;">.</span><span style="color: #0000FF;">DownloadString</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;http://www.google.com/&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
            <span style="color: #008080; font-style: italic;">// ret now contains the contents of the webpage</span>
            Console<span style="color: #008000;">.</span><span style="color: #0000FF;">WriteLine</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;First 256 bytes of response: &quot;</span> <span style="color: #008000;">+</span> ret<span style="color: #008000;">.</span><span style="color: #0000FF;">Substring</span><span style="color: #008000;">&#40;</span><span style="color: #FF0000;">0</span>,<span style="color: #FF0000;">265</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
        <span style="color: #008000;">&#125;</span>
        <span style="color: #0600FF; font-weight: bold;">catch</span> <span style="color: #008000;">&#40;</span>WebException we<span style="color: #008000;">&#41;</span>
        <span style="color: #008000;">&#123;</span>
            <span style="color: #008080; font-style: italic;">// WebException.Status holds useful information</span>
            Console<span style="color: #008000;">.</span><span style="color: #0000FF;">WriteLine</span><span style="color: #008000;">&#40;</span>we<span style="color: #008000;">.</span><span style="color: #0000FF;">Message</span> <span style="color: #008000;">+</span> <span style="color: #666666;">&quot;<span style="color: #008080; font-weight: bold;">\n</span>&quot;</span> <span style="color: #008000;">+</span> we<span style="color: #008000;">.</span><span style="color: #0000FF;">Status</span><span style="color: #008000;">.</span><span style="color: #0000FF;">ToString</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
        <span style="color: #008000;">&#125;</span>
        <span style="color: #0600FF; font-weight: bold;">catch</span> <span style="color: #008000;">&#40;</span>NotSupportedException ne<span style="color: #008000;">&#41;</span>
        <span style="color: #008000;">&#123;</span>
            <span style="color: #008080; font-style: italic;">// other errors</span>
            Console<span style="color: #008000;">.</span><span style="color: #0000FF;">WriteLine</span><span style="color: #008000;">&#40;</span>ne<span style="color: #008000;">.</span><span style="color: #0000FF;">Message</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
        <span style="color: #008000;">&#125;</span></pre></div></div>

<p><em>(This code uses DownloadString, you can also use DownloadData for a more binary-friendly version)</em></p>
<p>This is great for fetching simple pages that have data encoded in the querystring, but there are some problems with the basic <em>DownloadString</em> method of WebClient.  It&#8217;s synchronous, so it will <em>block</em> until the operation completes.  So for slow connections, or large files, this would need to run in another thread.  There is a better way.  But first, here&#8217;s another example of another basic, but important, method, <em>DownloadFile</em>.</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;">        <span style="color: #008080; font-style: italic;">// create a new instance of WebClient</span>
        WebClient client <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> WebClient<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
        <span style="color: #008080; font-style: italic;">// set the user agent to IE6</span>
        client<span style="color: #008000;">.</span><span style="color: #0000FF;">Headers</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Add</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;user-agent&quot;</span>, <span style="color: #666666;">&quot;Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; .NET CLR 1.0.3705;)&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
        <span style="color: #0600FF; font-weight: bold;">try</span>
        <span style="color: #008000;">&#123;</span>
            <span style="color: #008080; font-style: italic;">// actually execute the GET request</span>
            client<span style="color: #008000;">.</span><span style="color: #0000FF;">DownloadFile</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;http://www.google.com/&quot;</span>,<span style="color: #666666;">&quot;google_fetch.txt&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
            <span style="color: #008080; font-style: italic;">// ret now contains the contents of the webpage</span>
            Console<span style="color: #008000;">.</span><span style="color: #0000FF;">WriteLine</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;File Saved.&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
        <span style="color: #008000;">&#125;</span>
        <span style="color: #0600FF; font-weight: bold;">catch</span> <span style="color: #008000;">&#40;</span>WebException we<span style="color: #008000;">&#41;</span>
        <span style="color: #008000;">&#123;</span>
            <span style="color: #008080; font-style: italic;">// WebException.Status holds useful information</span>
            Console<span style="color: #008000;">.</span><span style="color: #0000FF;">WriteLine</span><span style="color: #008000;">&#40;</span>we<span style="color: #008000;">.</span><span style="color: #0000FF;">Message</span> <span style="color: #008000;">+</span> <span style="color: #666666;">&quot;<span style="color: #008080; font-weight: bold;">\n</span>&quot;</span> <span style="color: #008000;">+</span> we<span style="color: #008000;">.</span><span style="color: #0000FF;">Status</span><span style="color: #008000;">.</span><span style="color: #0000FF;">ToString</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
        <span style="color: #008000;">&#125;</span>
        <span style="color: #0600FF; font-weight: bold;">catch</span> <span style="color: #008000;">&#40;</span>NotSupportedException ne<span style="color: #008000;">&#41;</span>
        <span style="color: #008000;">&#123;</span>
            <span style="color: #008080; font-style: italic;">// other errors</span>
            Console<span style="color: #008000;">.</span><span style="color: #0000FF;">WriteLine</span><span style="color: #008000;">&#40;</span>ne<span style="color: #008000;">.</span><span style="color: #0000FF;">Message</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
        <span style="color: #008000;">&#125;</span></pre></div></div>

<p>This example is almost identical to the above, aside from the <em>client.DownloadFile</em> method.  This will save the file to disk, instead of returning a string&#8211;often what you would end up doing with the string anyway.</p>
<p><strong>But can I send POST data?<br />
</strong></p>
<p>Yes!  Using WebClient.UploadString or WebClient.UploadData you can POST data to the server easily.  I&#8217;ll show an example using UploadData, since UploadString is used in the same manner as DownloadString.</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;">            <span style="color: #6666cc; font-weight: bold;">byte</span><span style="color: #008000;">&#91;</span><span style="color: #008000;">&#93;</span> bret <span style="color: #008000;">=</span> client<span style="color: #008000;">.</span><span style="color: #0000FF;">UploadData</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;http://www.website.com/post.php&quot;</span>, <span style="color: #666666;">&quot;POST&quot;</span>,
                <span style="color: #000000;">System.<span style="color: #0000FF;">Text</span></span><span style="color: #008000;">.</span><span style="color: #0000FF;">Encoding</span><span style="color: #008000;">.</span><span style="color: #0000FF;">ASCII</span><span style="color: #008000;">.</span><span style="color: #0000FF;">GetBytes</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;field1=value1&amp;amp;field2=value2&quot;</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
            <span style="color: #6666cc; font-weight: bold;">string</span> sret <span style="color: #008000;">=</span> <span style="color: #000000;">System.<span style="color: #0000FF;">Text</span></span><span style="color: #008000;">.</span><span style="color: #0000FF;">Encoding</span><span style="color: #008000;">.</span><span style="color: #0000FF;">ASCII</span><span style="color: #008000;">.</span><span style="color: #0000FF;">GetString</span><span style="color: #008000;">&#40;</span>bret<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span></pre></div></div>

<p><em>UploadData</em> returns a byte array (byte[]) which contains the contents of the page.  This can easily be converted to a string by using the <em>System.Text.Encoding.ASCII.GetString()</em> method.  You can also use the other encoding types in <em>System.Text.Encoding</em> (such as UTF8) to do the same.</p>
<p>Also note the use of <em>GetBytes()</em>, the <em>UploadData</em> method will only take a byte array as a buffer for the upload.  This works well if you are uploading a binary file.</p>
<p><strong>What about the asynchronous methods of WebClient?</strong></p>
<p>WebClient provides asynchronous methods for fetching webpages as well, they are named similarly to the synchronous methods, DownloadFileAsync, DownloadDataAsync, DownloadStringAsync, UploadFileAsync, UploadDataAsync, UploadStringAsync, and UploadValuesAsync.</p>
<p>In order to execute an Async request, you will need to attach event handlers before the method is executed.  Without these event handlers you will not have any sense of when the operation finishes, or what stage it is at.</p>
<p><strong>C# WebClient Asynchronous call example:</strong></p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #6666cc; font-weight: bold;">void</span> do_upload<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
   WebClient client <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> WebClient<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
   <span style="color: #008080; font-style: italic;">// add event handlers for completed and progress changed</span>
   client<span style="color: #008000;">.</span><span style="color: #0000FF;">UploadProgressChanged</span> <span style="color: #008000;">+=</span> <span style="color: #008000;">new</span> UploadProgressChangedEventHandler<span style="color: #008000;">&#40;</span>client_UploadProgressChanged<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
   client<span style="color: #008000;">.</span><span style="color: #0000FF;">UploadFileCompleted</span> <span style="color: #008000;">+=</span> <span style="color: #008000;">new</span> UploadFileCompletedEventHandler<span style="color: #008000;">&#40;</span>client_UploadFileCompleted<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
  <span style="color: #008080; font-style: italic;">// carry out the operation as normal</span>
   client<span style="color: #008000;">.</span><span style="color: #0000FF;">UploadFileAsync</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;http://www.daveamenta.com/up.php&quot;</span>, <span style="color: #666666;">@&quot;c:\somefile.bin&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
<span style="color: #008000;">&#125;</span>
&nbsp;
<span style="color: #6666cc; font-weight: bold;">void</span> client_UploadProgressChanged<span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">object</span> sender, UploadProgressChangedEventArgs e<span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
   Console<span style="color: #008000;">.</span><span style="color: #0000FF;">WriteLine</span><span style="color: #008000;">&#40;</span>e<span style="color: #008000;">.</span><span style="color: #0000FF;">ProgressPercentage</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
<span style="color: #008000;">&#125;</span>
&nbsp;
<span style="color: #6666cc; font-weight: bold;">void</span> client_UploadFileCompleted<span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">object</span> sender, UploadFileCompletedEventArgs e<span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
   <span style="color: #0600FF; font-weight: bold;">if</span><span style="color: #008000;">&#40;</span>e<span style="color: #008000;">.</span><span style="color: #0000FF;">Result</span> <span style="color: #008000;">!=</span> <span style="color: #0600FF; font-weight: bold;">null</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
      Console<span style="color: #008000;">.</span><span style="color: #0000FF;">WriteLine</span><span style="color: #008000;">&#40;</span><span style="color: #000000;">System.<span style="color: #0000FF;">Text</span></span><span style="color: #008000;">.</span><span style="color: #0000FF;">Encoding</span><span style="color: #008000;">.</span><span style="color: #0000FF;">ASCII</span><span style="color: #008000;">.</span><span style="color: #0000FF;">GetString</span><span style="color: #008000;">&#40;</span>e<span style="color: #008000;">.</span><span style="color: #0000FF;">Result</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
   <span style="color: #008000;">&#125;</span>
<span style="color: #008000;">&#125;</span></pre></div></div>

<p>The call to <em>client.DownloadFileAsync</em> will no longer <em>block</em> the thread, and will execute in the background, periodically calling the event handlers to display progress.  The other methods can be used in this same way.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.daveamenta.com/2008-05/c-webclient-usage/feed/</wfw:commentRss>
		<slash:comments>15</slash:comments>
		</item>
		<item>
		<title>C#: Send text to a pastebin (HttpWebRequest POST example)</title>
		<link>http://www.daveamenta.com/2008-05/c-send-text-to-a-pastebin-httpwebrequest-post-example/</link>
		<comments>http://www.daveamenta.com/2008-05/c-send-text-to-a-pastebin-httpwebrequest-post-example/#comments</comments>
		<pubDate>Fri, 09 May 2008 04:03:37 +0000</pubDate>
		<dc:creator>Dave</dc:creator>
				<category><![CDATA[Code Snippets]]></category>
		<category><![CDATA[.NET]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[HTTP]]></category>
		<category><![CDATA[HttpWebRequest]]></category>
		<category><![CDATA[HttpWebResponse]]></category>
		<category><![CDATA[WebClient]]></category>

		<guid isPermaLink="false">http://www.daveamenta.com/?p=37</guid>
		<description><![CDATA[I&#8217;m working on a small application that hooks into the clipboard monitor chain and displays a list of options whenever text is saved to the clipbaord.  One of the options I wanted was to be able to send data to pastebin.com, a popular text/code sharing service.  This is a relatively mundane task, faking the postdata [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://www.daveamenta.com/wp-content/uploads/2008/05/paste.gif"><img align="right" class="alignright size-full wp-image-38" title="paste" src="http://www.daveamenta.com/wp-content/uploads/2008/05/paste.gif" alt="" width="123" height="281" /></a>I&#8217;m working on a small application that hooks into the clipboard monitor chain and displays a list of options whenever text is saved to the clipbaord.  One of the options I wanted was to be able to send data to <a href="http://www.pastebin.com" target="_blank">pastebin.com</a>, a popular text/code sharing service.  This is a relatively mundane task, faking the postdata for a webpage, and sending the requests as a browser.  There were some hicups though, mostly with <em>Finding the redirect URL using HttpWebRequest/HttpWebResponse</em>.</p>
<p><strong>A little background:</strong></p>
<p><em>HttpWebRequest</em> and <em>HttpWebResponse</em> live in the <em>System.Net</em> namespace, and provide the ability to quickly send HTTP requests to a webserver.  This is what a simple GET request would look like:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF; font-weight: bold;">try</span>
<span style="color: #008000;">&#123;</span>
&nbsp;
    HttpWebRequest request  <span style="color: #008000;">=</span> <span style="color: #008000;">&#40;</span>HttpWebRequest<span style="color: #008000;">&#41;</span> WebRequest<span style="color: #008000;">.</span><span style="color: #0000FF;">Create</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;http://davux.pastebin.com/pastebin.php&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
    HttpWebResponse res <span style="color: #008000;">=</span><span style="color: #008000;">&#40;</span>HttpWebResponse<span style="color: #008000;">&#41;</span> request<span style="color: #008000;">.</span><span style="color: #0000FF;">GetResponse</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
    <span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span>res<span style="color: #008000;">.</span><span style="color: #0000FF;">StatusCode</span> <span style="color: #008000;">==</span> HttpStatusCode<span style="color: #008000;">.</span><span style="color: #0000FF;">OK</span><span style="color: #008000;">&#41;</span>
    <span style="color: #008000;">&#123;</span>
          StreamReader reader <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> StreamReader<span style="color: #008000;">&#40;</span>res<span style="color: #008000;">.</span><span style="color: #0000FF;">GetResponseStream</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
          Console<span style="color: #008000;">.</span><span style="color: #0000FF;">WriteLine</span><span style="color: #008000;">&#40;</span>reader<span style="color: #008000;">.</span><span style="color: #0000FF;">ReadToEnd</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
    <span style="color: #008000;">&#125;</span> <span style="color: #0600FF; font-weight: bold;">else</span> <span style="color: #008000;">&#123;</span>
         <span style="color: #008080; font-style: italic;">// handle errors</span>
    <span style="color: #008000;">&#125;</span>
&nbsp;
    res<span style="color: #008000;">.</span><span style="color: #0000FF;">Close</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
<span style="color: #008000;">&#125;</span>
<span style="color: #0600FF; font-weight: bold;">catch</span> <span style="color: #008000;">&#40;</span>Exception ex<span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
    Console<span style="color: #008000;">.</span><span style="color: #0000FF;">WriteLine</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;Error: &quot;</span> <span style="color: #008000;">+</span> ex<span style="color: #008000;">.</span><span style="color: #0000FF;">Message</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
<span style="color: #008000;">&#125;</span></pre></div></div>

<p><strong>Note: </strong>For most simple tasks, it is wise to use <em>System.Net.WebClient</em>, which is not suitable for this task, because it does not expose the ability to disable automatic redirection.</p>
<p>To send new data to pastebin, it needs to be sent via HTTP POST.  Using Firefox <a href="http://livehttpheaders.mozdev.org/" target="_blank">LiveHTTPHeaders</a>, I determined what string needs to be sent in order to create a new entry.  This can also be determined by reading the source of the page, and looking at each of the fields contained in the <em>&lt;form&gt;</em> block.</p>

<div class="wp_syntax"><div class="code"><pre class="text" style="font-family:monospace;">string post = &quot;&amp;amp;parent_pid=&amp;amp;format=text&amp;amp;code2=&quot; + code_text_to_send + &quot;&amp;amp;poster=&quot; + poster_name + &quot;&amp;amp;paste=Send&amp;amp;expiry=&quot; + expiry + &quot;&amp;amp;email=&quot;;</pre></div></div>

<ul>
<li><strong>code_text_to_send</strong> &#8211; The data which should be used to create a new page.</li>
<li><strong>poster_name</strong> &#8211; The author of the post</li>
<li><strong>expiry</strong> &#8211; How long the data should be retained for.  Options are &#8216;d&#8217; for one day, &#8216;m&#8217; for one month, and &#8216;f&#8217; for indefinitely.</li>
</ul>
<p><strong>Note:</strong> Make sure to <a href="http://www.daveamenta.com/2008-05/c-urlencode-and-urldecode/" target="_blank">URLEncode</a> each field that is sent!</p>
<p>After further examination of the headers and data sent back, I decided that the best way to extract the link was to catch the <em>HTTP 302 Document Found</em> response.</p>

<div class="wp_syntax"><div class="code"><pre class="text" style="font-family:monospace;">HTTP/1.x 302 Found
Date: Thu, 08 May 2008 01:06:08 GMT
Server: Apache/1.3.33 (Debian GNU/Linux) mod_python/2.7.10 Python/2.3.4 PHP/4.3.10-22 mod_perl/1.29
X-Powered-By: PHP/4.3.10-22
Location: http://davux.pastebin.com/m10a794d6
Keep-Alive: timeout=15, max=100
Connection: Keep-Alive
Transfer-Encoding: chunked
Content-Type: text/html; charset=iso-8859-1</pre></div></div>

<p>HttpWebRequest, by default, has AllowAutoRedirect set to true, which means that, internally, it will resolve this message, and fetch the new URL.  This extra operation is needless in this circumstance, so it is best to disable it and extract the URL from the header.  The headers associated with each request or response are found in the <em>Headers</em> object for each class.  We wish to look at the <em>Location</em> field to find our URL.<br />
<strong>C# WebClient HTTP POST Example:</strong></p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF; font-weight: bold;">try</span>
<span style="color: #008000;">&#123;</span>
&nbsp;
    HttpWebRequest request <span style="color: #008000;">=</span> <span style="color: #008000;">&#40;</span>HttpWebRequest<span style="color: #008000;">&#41;</span>
        WebRequest<span style="color: #008000;">.</span><span style="color: #0000FF;">Create</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;http://davux.pastebin.com/pastebin.php&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
    request<span style="color: #008000;">.</span><span style="color: #0000FF;">AllowAutoRedirect</span> <span style="color: #008000;">=</span> <span style="color: #0600FF; font-weight: bold;">false</span><span style="color: #008000;">;</span>
    request<span style="color: #008000;">.</span><span style="color: #0000FF;">Method</span> <span style="color: #008000;">=</span> <span style="color: #666666;">&quot;POST&quot;</span><span style="color: #008000;">;</span>
&nbsp;
    <span style="color: #6666cc; font-weight: bold;">string</span> post <span style="color: #008000;">=</span> <span style="color: #666666;">&quot;&amp;amp;parent_pid=&amp;amp;format=text&amp;amp;code2=&quot;</span> <span style="color: #008000;">+</span> HttpUtility<span style="color: #008000;">.</span><span style="color: #0000FF;">UrlEncode</span><span style="color: #008000;">&#40;</span>text<span style="color: #008000;">&#41;</span> <span style="color: #008000;">+</span> <span style="color: #666666;">&quot;&amp;amp;poster=Dave&amp;amp;paste=Send&amp;amp;expiry=m&amp;amp;email=&quot;</span><span style="color: #008000;">;</span>
    <span style="color: #6666cc; font-weight: bold;">byte</span><span style="color: #008000;">&#91;</span><span style="color: #008000;">&#93;</span> data <span style="color: #008000;">=</span> <span style="color: #000000;">System.<span style="color: #0000FF;">Text</span></span><span style="color: #008000;">.</span><span style="color: #0000FF;">Encoding</span><span style="color: #008000;">.</span><span style="color: #0000FF;">ASCII</span><span style="color: #008000;">.</span><span style="color: #0000FF;">GetBytes</span><span style="color: #008000;">&#40;</span>post<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
    request<span style="color: #008000;">.</span><span style="color: #0000FF;">ContentType</span> <span style="color: #008000;">=</span> <span style="color: #666666;">&quot;application/x-www-form-urlencoded&quot;</span><span style="color: #008000;">;</span>
    request<span style="color: #008000;">.</span><span style="color: #0000FF;">ContentLength</span> <span style="color: #008000;">=</span> data<span style="color: #008000;">.</span><span style="color: #0000FF;">Length</span><span style="color: #008000;">;</span>
&nbsp;
    Stream response <span style="color: #008000;">=</span> request<span style="color: #008000;">.</span><span style="color: #0000FF;">GetRequestStream</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
    response<span style="color: #008000;">.</span><span style="color: #0000FF;">Write</span><span style="color: #008000;">&#40;</span>data,<span style="color: #FF0000;">0</span>,data<span style="color: #008000;">.</span><span style="color: #0000FF;">Length</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
    response<span style="color: #008000;">.</span><span style="color: #0000FF;">Close</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
    HttpWebResponse res <span style="color: #008000;">=</span><span style="color: #008000;">&#40;</span>HttpWebResponse<span style="color: #008000;">&#41;</span> request<span style="color: #008000;">.</span><span style="color: #0000FF;">GetResponse</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
    res<span style="color: #008000;">.</span><span style="color: #0000FF;">Close</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
    <span style="color: #008080; font-style: italic;">// note that there is no need to hook up a StreamReader and</span>
    <span style="color: #008080; font-style: italic;">// look at the response data, since it is of no need</span>
&nbsp;
    <span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span>res<span style="color: #008000;">.</span><span style="color: #0000FF;">StatusCode</span> <span style="color: #008000;">==</span> HttpStatusCode<span style="color: #008000;">.</span><span style="color: #0000FF;">Found</span><span style="color: #008000;">&#41;</span>
    <span style="color: #008000;">&#123;</span>
        Console<span style="color: #008000;">.</span><span style="color: #0000FF;">WriteLine</span><span style="color: #008000;">&#40;</span>res<span style="color: #008000;">.</span><span style="color: #0000FF;">Headers</span><span style="color: #008000;">&#91;</span><span style="color: #666666;">&quot;location&quot;</span><span style="color: #008000;">&#93;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
    <span style="color: #008000;">&#125;</span>
    <span style="color: #0600FF; font-weight: bold;">else</span>
    <span style="color: #008000;">&#123;</span>
        Console<span style="color: #008000;">.</span><span style="color: #0000FF;">WriteLine</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;Error&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
    <span style="color: #008000;">&#125;</span>
&nbsp;
<span style="color: #008000;">&#125;</span>
<span style="color: #0600FF; font-weight: bold;">catch</span> <span style="color: #008000;">&#40;</span>Exception ex<span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
    Console<span style="color: #008000;">.</span><span style="color: #0000FF;">WriteLine</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;Error: &quot;</span> <span style="color: #008000;">+</span> ex<span style="color: #008000;">.</span><span style="color: #0000FF;">Message</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
<span style="color: #008000;">&#125;</span></pre></div></div>

<p>(Some error checking and extra code have been removed for the sake of clarity)</p>
<p>The values for <em>expiry</em> and <em>author</em> have been hard-coded in order to save space.  This basic example will create a new Pastebin entry on my <a href="http://davux.pastebin.com" target="_blank">not so private pastebin</a>, and print the URL of the entry to the console.  If there is an error, it will print the exception message.  If the HTTP code returned is not 302/Found, it will print a generic error.  (If the server is not returning a 302 for this request, there is nothing codewise that can be fixed, and thus the only option for error is generic.)</p>
]]></content:encoded>
			<wfw:commentRss>http://www.daveamenta.com/2008-05/c-send-text-to-a-pastebin-httpwebrequest-post-example/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
	</channel>
</rss>

