36

Using C# and ASP.NET I want to programmatically fill in some values (4 text boxes) on a web page (form) and then 'POST' those values. How do I do this?

Edit: Clarification: There is a service (www.stopforumspam.com) where you can submit ip, username and email address on their 'add' page. I want to be able to create a link/button on my site's page that will fill in those values and submit the info without having to copy/paste them across and click the submit button.

Further clarification: How do automated spam bots fill out forms and click the submit button if they were written in C#?

Guy
  • 65,082
  • 97
  • 254
  • 325

6 Answers6

49

The code will look something like this:

WebRequest req = WebRequest.Create("http://mysite/myform.aspx");
string postData = "item1=11111&item2=22222&Item3=33333";

byte[] send = Encoding.Default.GetBytes(postData);
req.Method = "POST";
req.ContentType = "application/x-www-form-urlencoded";
req.ContentLength = send.Length;

Stream sout = req.GetRequestStream();
sout.Write(send, 0, send.Length);
sout.Flush();
sout.Close();

WebResponse res = req.GetResponse();
StreamReader sr = new StreamReader(res.GetResponseStream());
string returnvalue = sr.ReadToEnd();
Bob Kaufman
  • 12,864
  • 16
  • 78
  • 107
Ryan Farley
  • 11,315
  • 4
  • 46
  • 43
  • Could you please explain how to collect the returned data from the request ? – Barbaros Alp Mar 27 '09 at 21:39
  • 3
    In the sample above, the returnvalue contains the data returned from the request. – Ryan Farley Mar 28 '09 at 02:39
  • in your code return value is string. What if it is regular page that you want your user to browse? – jlp Sep 07 '11 at 11:52
  • 1
    @jlp Do you mean you want to just display the page to the user for them to browse? If that is the case you'd not do this, but use the Browser control or shell the URL to open instead. The return is a string because you're getting back whatever the server sends back which is a string, typically HTML. You could load that HTML in a browser control if you really wanted to. – Ryan Farley Sep 07 '11 at 15:24
  • When I am doing actual form post to another website, the request go through and website redirects(302) in response to next URL. But when using above, it is giving Forbidden (403). What could possibly be wrong? How can i receive 302 in WebResponse? – Himalaya Garg Dec 22 '20 at 14:12
4

You can use the UploadValues method on WebClient - all it requires is passing a URL and a NameValueCollection. It is the easiest approach that I have found, and the MS documentation has a nice example:
http://msdn.microsoft.com/en-us/library/9w7b4fz7.aspx

Here is a simple version with some error handling:

var webClient = new WebClient();
Debug.Info("PostingForm: " + url);
try
{
     byte [] responseArray = webClient.UploadValues(url, nameValueCollection);
     return new Response(responseArray, (int) HttpStatusCode.OK);
}
catch (WebException e)
{
     var response = (HttpWebResponse)e.Response;
     byte[] responseBytes = IOUtil.StreamToBytes(response.GetResponseStream());
     return new Response(responseBytes, (int) response.StatusCode);
}  

The Response class is a simple wrapper for the response body and status code.

John Kelvie
  • 858
  • 1
  • 7
  • 16
1

View the source of the page and use the WebRequest class to do the posting. No need to drive IE. Just figure out what IE is sending to the server and replicate that. Using a tool like Fiddler will make it even easier.

Ryan Rinaldi
  • 4,119
  • 2
  • 22
  • 22
0

I had a situation where I needed to post free text from a html textarea programmatically and I had issues where I was getting <br /> in my param list i was building.

My solution was a replace of the br tags with linebreak characters and htmlencoding just to be safe.

Regex.Replace( HttpUtility.HtmlDecode( test ), "(<br.*?>)", "\r\n" ,RegexOptions.IgnoreCase);
John
  • 3,512
  • 2
  • 36
  • 53
0

Where you encode the string:

Encoding.Default.GetBytes(postData);

Use Ascii instead for the google apis:

Encoding.ASCII.GetBytes(postData);

this makes your request the same as and equivalent "curl --data "..." [url]" request

0

you can send a post/get request with many ways. Different types of library is there to help. I found it is confusing to choose which one I should use and what are the differences among them.

After surfing stack overflow this is the best answer I found. this thread explains all

https://stackoverflow.com/a/4015346/1999720

Community
  • 1
  • 1
Sadid Khan
  • 1,836
  • 20
  • 35