<?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/"
	xmlns:georss="http://www.georss.org/georss" xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#" xmlns:media="http://search.yahoo.com/mrss/"
	>

<channel>
	<title>Anhonga&#039;s Blog</title>
	<atom:link href="http://anhonga.wordpress.com/feed/" rel="self" type="application/rss+xml" />
	<link>http://anhonga.wordpress.com</link>
	<description>Random tech stuff</description>
	<lastBuildDate>Thu, 29 Sep 2011 01:48:21 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.com/</generator>
<cloud domain='anhonga.wordpress.com' port='80' path='/?rsscloud=notify' registerProcedure='' protocol='http-post' />
<image>
		<url>http://0.gravatar.com/blavatar/88246e281e6790582b31b09abb6734da?s=96&#038;d=http%3A%2F%2Fs2.wp.com%2Fi%2Fbuttonw-com.png</url>
		<title>Anhonga&#039;s Blog</title>
		<link>http://anhonga.wordpress.com</link>
	</image>
	<atom:link rel="search" type="application/opensearchdescription+xml" href="http://anhonga.wordpress.com/osd.xml" title="Anhonga&#039;s Blog" />
	<atom:link rel='hub' href='http://anhonga.wordpress.com/?pushpress=hub'/>
		<item>
		<title>Using WebClient with UploadValues and UploadString to simulate POST</title>
		<link>http://anhonga.wordpress.com/2010/05/06/using-webclient-with-uploadvalues-and-uploadstring-to-simulate-post/</link>
		<comments>http://anhonga.wordpress.com/2010/05/06/using-webclient-with-uploadvalues-and-uploadstring-to-simulate-post/#comments</comments>
		<pubDate>Thu, 06 May 2010 19:57:47 +0000</pubDate>
		<dc:creator>anhonga</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[Multiple]]></category>
		<category><![CDATA[NameValueCollection]]></category>
		<category><![CDATA[POST]]></category>
		<category><![CDATA[UploadString]]></category>
		<category><![CDATA[UploadValues]]></category>
		<category><![CDATA[WebClient]]></category>

		<guid isPermaLink="false">http://anhonga.wordpress.com/?p=139</guid>
		<description><![CDATA[I was recently tasked with converting a vendor-provided web form to a more robust ASP.Net page. This form would be public facing, would POST user data from the form to the vendor, and would require extensive validations/manipulations before posting. This needed to happen without the use of javascript or any client-side validation. To keep it [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=anhonga.wordpress.com&amp;blog=10387985&amp;post=139&amp;subd=anhonga&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>I was recently tasked with converting a vendor-provided web form to a more robust ASP.Net page. This form would be public facing, would POST user data from the form to the vendor, and would require extensive validations/manipulations before posting. This needed to happen without the use of javascript or any client-side validation. To keep it simple, I created a new ASP.Net page and reconstructed the vendor form using all ASP.Net controls. The “Submit” button called a code-behind method which performed the validations, then built a NameValueCollection from the form controls and submitted it using WebClient.UploadValues, like so:</p>
<pre>System.Net.WebClient myWebClient = new System.Net.WebClient();
myWebClient.Headers.Add("Charset", "text/html; charset=UTF-8");

NameValueCollection frm = new NameValueCollection();

// Perform server-side validations
if (this.F_Name.Text.Length == 0 || this.L_Name.Text.Length == 0)
{ AppendError("First and Last name must be provided"); }
…

// Add the user-provided name values
frm.Add("last_name", this.L_Name.Text);
frm.Add("first_name", this.F_Name.Text);
frm.Add("address", this.Address.Text);
…

// Add the Toppings
foreach (ListItem item in this.ToppingsChkBoxList.Items)
{
if (item.Selected)
    {
        Frm.Add("Toppings", item.Value.ToString());
    }
}

myWebClient.UploadValues("http://www.Destination.com/...?encoding=UTF-8", "POST", frm);</pre>
<p>This solution worked like a charm, except for one small problem. For the Toppings field, the vendor allowed for multiple selections. In their sample form, they used the <strong>&lt;SELECT MULTIPLE… /&gt;</strong> control to gather multiple values from the user. In Fiddler, multiple selections for this field looked something like this:</p>
<pre>Toppings=Extra+cheese&amp;Toppings=Mushrooms&amp;Toppings=Pepperoni&amp;Toppings=Olives</pre>
<p>NameValueCollection does not handle multiple values for the same key in this way. Instead, the NameValueCollection would read my Toppings control as:</p>
<pre>&amp;Toppings=Extra+cheese,Mushrooms,Pepperoni,Olives</pre>
<p>The vendor’s import process was reading this as a single value, rather than multiple values as intended, causing the upload to fail. To get around this, I used WebClient.UploadString instead to build an uploadable string from scratch. Now, my upload process looks like this:</p>
<pre>StringBuilder _strBld = new StringBuilder();
int _intItemCount = 0;

protected void btnSubmit_Click(object sender, EventArgs e)
{
    System.Net.WebClient myWebClient = new System.Net.WebClient();
    myWebClient.Headers.Add("Charset", "text/html; charset=UTF-8");
    myWebClient.Headers.Add("Content-Type", "application/x-www-form-urlencoded"); // ◄  This line is essential

// Perform server-side validations (same as before)
if (this.F_Name.Text.Length == 0 || this.L_Name.Text.Length == 0)
{ AppendError("First and Last name must be provided"); }
…

// Add the user-provided name values
AppendUploadString("last_name", this.L_Name.Text);
AppendUploadString ("first_name", this.F_Name.Text);
AppendUploadString ("address", this.Address.Text);

// Add the Toppings
foreach (ListItem item in this.ToppingsChkBoxList.Items)
{
if (item.Selected)
    {
        AppendUploadString("Toppings", item.Value.ToString());
    }
}

myWebClient.UploadString("https http://www.Destination.com/...?encoding=UTF-8", "POST", _strBld.ToString());
}

private void AppendUploadString(string strName, string strValue)

{
    _intItemCount++;
    <span style="text-decoration:line-through;">_strBld.Append((intItemCount == 1 ? "" : "&amp;") + strName + "=" + strValue);
</span>    _strBld.Append((intItemCount == 1 ? "" : "&amp;") + strName + "=" + System.Web.HttpUtility.UrlEncode(strValue));
<strong>    // Update: Use UrlEncode to ensure that the special characters are included in the submission</strong>
}
</pre>
<p>To get around the NameValueCollection restriction, I built my own string and submitted is using WebClient.UploadString.</p>
<p>A concise description of WebClient can be found <a href="http://www.daveamenta.com/2008-05/c-webclient-usage/">here</a>.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/anhonga.wordpress.com/139/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/anhonga.wordpress.com/139/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/anhonga.wordpress.com/139/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/anhonga.wordpress.com/139/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/anhonga.wordpress.com/139/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/anhonga.wordpress.com/139/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/anhonga.wordpress.com/139/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/anhonga.wordpress.com/139/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/anhonga.wordpress.com/139/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/anhonga.wordpress.com/139/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/anhonga.wordpress.com/139/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/anhonga.wordpress.com/139/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/anhonga.wordpress.com/139/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/anhonga.wordpress.com/139/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=anhonga.wordpress.com&amp;blog=10387985&amp;post=139&amp;subd=anhonga&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://anhonga.wordpress.com/2010/05/06/using-webclient-with-uploadvalues-and-uploadstring-to-simulate-post/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/9383c60887c3079f7bde4639a230ca29?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">anhonga</media:title>
		</media:content>
	</item>
	</channel>
</rss>
