<?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>Daniel Banck &#187; post data</title>
	<atom:link href="http://dbanck.de/tag/post-data/feed/" rel="self" type="application/rss+xml" />
	<link>http://dbanck.de</link>
	<description>Webentwicklung - Webdevelopment</description>
	<lastBuildDate>Sat, 09 Jan 2010 00:19:53 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>POST Data with .net Html Agility Pack</title>
		<link>http://dbanck.de/2010/01/09/post-data-with-net-html-agility-pack/</link>
		<comments>http://dbanck.de/2010/01/09/post-data-with-net-html-agility-pack/#comments</comments>
		<pubDate>Sat, 09 Jan 2010 00:19:53 +0000</pubDate>
		<dc:creator>Daniel Banck</dc:creator>
				<category><![CDATA[C#]]></category>
		<category><![CDATA[.net]]></category>
		<category><![CDATA[bug]]></category>
		<category><![CDATA[post data]]></category>

		<guid isPermaLink="false">http://dbanck.de/?p=111</guid>
		<description><![CDATA[I&#8217;m using the Html Agility Pack for getting and parsing HTML pages. It offers many possibilities, including XPath selectors.
Today I had a problem with posting data to a webpage. It&#8217;s possible to call the HtmlWeb Load function with a second &#8220;POST&#8221; parameter, but it offers no easy way to add values to be posted.
After a [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;m using the <a href="http://htmlagilitypack.codeplex.com/">Html Agility Pack</a> for getting and parsing HTML pages. It offers many possibilities, including XPath selectors.</p>
<p>Today I had a problem with posting data to a webpage. It&#8217;s possible to call the <code>HtmlWeb</code> <code>Load</code> function with a second &#8220;POST&#8221; parameter, but it offers no easy way to add values to be posted.<br />
After a while of searching I came across this solution: add this two functions to the <code>HtmlWeb</code> class. (You can get the source in download section, too)</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF;">public</span> HtmlDocument SubmitFormValues <span style="color: #000000;">&#40;</span>NameValueCollection fv, <span style="color: #FF0000;">string</span> url<span style="color: #000000;">&#41;</span>
<span style="color: #000000;">&#123;</span>
	<span style="color: #008080; font-style: italic;">// Attach a temporary delegate to handle attaching</span>
	<span style="color: #008080; font-style: italic;">// the post back data</span>
	PreRequestHandler handler <span style="color: #008000;">=</span> <span style="color: #FF0000;">delegate</span><span style="color: #000000;">&#40;</span>HttpWebRequest request<span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
		<span style="color: #FF0000;">string</span> payload <span style="color: #008000;">=</span> <span style="color: #0600FF;">this</span>.<span style="color: #0000FF;">AssemblePostPayload</span> <span style="color: #000000;">&#40;</span>fv<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
			<span style="color: #FF0000;">byte</span><span style="color: #000000;">&#91;</span><span style="color: #000000;">&#93;</span> buff <span style="color: #008000;">=</span> Encoding.<span style="color: #0000FF;">ASCII</span>.<span style="color: #0000FF;">GetBytes</span> <span style="color: #000000;">&#40;</span>payload.<span style="color: #0000FF;">ToCharArray</span> <span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
			request.<span style="color: #0000FF;">ContentLength</span> <span style="color: #008000;">=</span> buff.<span style="color: #0000FF;">Length</span><span style="color: #008000;">;</span>
			request.<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>
			<span style="color: #000000;">System.<span style="color: #0000FF;">IO</span></span>.<span style="color: #0000FF;">Stream</span> reqStream <span style="color: #008000;">=</span> request.<span style="color: #0000FF;">GetRequestStream</span> <span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
			reqStream.<span style="color: #0000FF;">Write</span> <span style="color: #000000;">&#40;</span>buff, <span style="color: #FF0000;">0</span>, buff.<span style="color: #0000FF;">Length</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
			<span style="color: #0600FF;">return</span> true<span style="color: #008000;">;</span>
	<span style="color: #000000;">&#125;</span>
	<span style="color: #0600FF;">this</span>.<span style="color: #0000FF;">PreRequest</span> <span style="color: #008000;">+=</span> handler<span style="color: #008000;">;</span>
	HtmlDocument doc <span style="color: #008000;">=</span> <span style="color: #0600FF;">this</span>.<span style="color: #0000FF;">Load</span> <span style="color: #000000;">&#40;</span>url, <span style="color: #666666;">&quot;POST&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
	<span style="color: #0600FF;">this</span>.<span style="color: #0000FF;">PreRequest</span> <span style="color: #008000;">-=</span> handler<span style="color: #008000;">;</span>
	<span style="color: #0600FF;">return</span> doc<span style="color: #008000;">;</span>
<span style="color: #000000;">&#125;</span>
&nbsp;
<span style="color: #0600FF;">private</span> <span style="color: #FF0000;">string</span> AssemblePostPayload <span style="color: #000000;">&#40;</span>NameValueCollection fv<span style="color: #000000;">&#41;</span>
<span style="color: #000000;">&#123;</span>
	StringBuilder sb <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> StringBuilder <span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
	<span style="color: #0600FF;">foreach</span> <span style="color: #000000;">&#40;</span><span style="color: #FF0000;">String</span> key <span style="color: #0600FF;">in</span> fv.<span style="color: #0000FF;">AllKeys</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
		sb.<span style="color: #0000FF;">Append</span> <span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;&amp;&quot;</span> <span style="color: #008000;">+</span> key <span style="color: #008000;">+</span> <span style="color: #666666;">&quot;=&quot;</span> <span style="color: #008000;">+</span> fv.<span style="color: #0000FF;">Get</span> <span style="color: #000000;">&#40;</span>key<span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
	<span style="color: #000000;">&#125;</span>
	<span style="color: #0600FF;">return</span> sb.<span style="color: #0000FF;">ToString</span> <span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>.<span style="color: #0000FF;">Substring</span> <span style="color: #000000;">&#40;</span><span style="color: #FF0000;">1</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

<p>Source: <a href="http://htmlagilitypack.codeplex.com/Thread/View.aspx?ThreadId=14255">http://htmlagilitypack.codeplex.com/Thread/View.aspx?ThreadId=14255</a></p>
<p>Now posting is easy. Example:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;">NameValueCollection postData <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> NameValueCollection <span style="color: #000000;">&#40;</span><span style="color: #FF0000;">1</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
postData.<span style="color: #0000FF;">Add</span> <span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;name&quot;</span>, <span style="color: #666666;">&quot;value&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
doc <span style="color: #008000;">=</span> <span style="color: #0600FF;">this</span>.<span style="color: #0000FF;">hw</span>.<span style="color: #0000FF;">SubmitFormValues</span> <span style="color: #000000;">&#40;</span>postData, url<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span></pre></div></div>

<p>(<code>hw</code> is an <code>HtmlWeb</code> object and <code>doc</code> an <code>HtmlDocument</code> object)</p>
<p>If you need to use cookies, too, you need to modify the <code>HtmlWeb</code> a little more, because there is a little bug.<br />
First add a new <code>private</code> variable:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF;">private</span> CookieContainer _ck <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> CookieContainer<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span></pre></div></div>

<p>Then find this section:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF;">if</span> <span style="color: #000000;">&#40;</span>_useCookies<span style="color: #000000;">&#41;</span> 
<span style="color: #000000;">&#123;</span>
        req.<span style="color: #0000FF;">CookieContainer</span> <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> CookieContainer<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

<p>And change it into:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF;">if</span> <span style="color: #000000;">&#40;</span>_useCookies<span style="color: #000000;">&#41;</span> 
<span style="color: #000000;">&#123;</span>
        req.<span style="color: #0000FF;">CookieContainer</span> <span style="color: #008000;">=</span> <span style="color: #0600FF;">this</span>._ck<span style="color: #008000;">;</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

<p>Now you just have to activate the usage of cookies in your <code>HtmlWeb</code> object:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF;">this</span>.<span style="color: #0000FF;">hw</span> <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> HtmlWeb <span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
<span style="color: #0600FF;">this</span>.<span style="color: #0000FF;">hw</span>.<span style="color: #0000FF;">UseCookies</span> <span style="color: #008000;">=</span> true<span style="color: #008000;">;</span></pre></div></div>

<p>That&#8217;s it! Enjoy easy form posting with a full cookie jar (:</p>
]]></content:encoded>
			<wfw:commentRss>http://dbanck.de/2010/01/09/post-data-with-net-html-agility-pack/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
