How to post html form data with C# .NET.
In a program I made in C# .NET I needed the ability to post data to a PHP script. I wanted the data to be posted as if I was filling out the form on a web page. After trying a few different methods of doing this, mostly just putting the data in a query string, I found this method to work the best. It uses a true HTML POST.
The code below uses the UploadValues method from the WebClient class. The NameValueCollection hold the keys and values for the data you want to post to the script.
WebClient WC = new WebClient();
NameValueCollection nv = new NameValueCollection();
nv.Add(“first_name”, “Bob”);
nv.Add(“last_name”, “Jones”);
nv.Add(“zip_code”, 22121);
nv.Add(“username”, “bjones”);
byte[] retData = WC.UploadValues(“http://www.somedomain/script.php”, “POST”, nv);
In your PHP script you would get the values like you would if the data was posted from a web page.
<php
$first_name = $_REQUEST['first_name'];
$last_name = $_REQUEST['last_name'];
$zip_code = $_REQUEST['zip_code'];
$username = $_REQUEST['username'];
?>
