Hacker Forums

Archive for May, 2007

HTML POST with C# .NET

by The Uni-Hacker on May.26, 2007, under Programming

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.
$first_name = $_REQUEST['first_name'] so on and so forth.

4 Comments more...

the process cannot access the file because it is being used by another process.

by The Uni-Hacker on May.13, 2007, under Misc

This message appeared while I was trying to read a text file that was locked. I was very frustrated because notepad and other text editors could open it no problem, but my simple StreamReader couldn’t open it. The code below was giving me this error and I had to do a little tweaking and fiddling to get it to work.

Broken Code

StreamReader fr = new StreamReader(this.eveFile);

Working Code
FileStream fs = new FileStream(this.eveFile, FileMode.Open, FileAccess.Read, FileShare.ReadWrite );
StreamReader sr = new StreamReader(fs);

Apparently you have to open the file specifically for Read access, thus the “FileAccess.Read”. It appears that StreamReader opens in ReadWrite no matter what, and you can’t change the FileAccess in StreamReader. No problem though as this is only one more line of code.

Leave a Comment :, , , more...

Looking for something?

Use the form below to search the site:

Still not finding what you're looking for? Drop a comment on a post or contact us so we can take care of it!

Visit our friends!

A few highly recommended friends...