<?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; HttpWebRequest</title>
	<atom:link href="http://www.daveamenta.com/tag/httpwebrequest/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#: 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>

