I have a simple html file such as
<form action="http://www.someurl.com/page.php" method="POST"> <input type="text" name="test"><br/> <input type="submit" name="submit"> </form>
Edit: I may not have been clear enough with the question
I want to write C# code which submits this form in the exact same manner that would occur had I pasted the above html into a file, opened it with IE and submitted it with the browser.
The <input type="submit"> defines a button for submitting the form data to a form-handler. The form-handler is typically a file on the server with a script for processing input data. The form-handler is specified in the form's action attribute.
get( url, [data], [callback], [type] ) method loads data from the server using a GET HTTP request. data − This optional parameter represents key/value pairs that will be sent to the server. callback − This optional parameter represents a function to be executed whenever the data is loaded successfully.
Form data is information provided by the user through interaction with an element in a HTML form, such as a text input box, button, or check box. The information is transmitted as a series of name and value pairs.
Here is a sample script that I recently used in a Gateway POST transaction that receives a GET response. Are you using this in a custom C# form? Whatever your purpose, just replace the String fields (username, password, etc.) with the parameters from your form.
private String readHtmlPage(string url) { //setup some variables String username = "demo"; String password = "password"; String firstname = "John"; String lastname = "Smith"; //setup some variables end String result = ""; String strPost = "username="+username+"&password="+password+"&firstname="+firstname+"&lastname="+lastname; StreamWriter myWriter = null; HttpWebRequest objRequest = (HttpWebRequest)WebRequest.Create(url); objRequest.Method = "POST"; objRequest.ContentLength = strPost.Length; objRequest.ContentType = "application/x-www-form-urlencoded"; try { myWriter = new StreamWriter(objRequest.GetRequestStream()); myWriter.Write(strPost); } catch (Exception e) { return e.Message; } finally { myWriter.Close(); } HttpWebResponse objResponse = (HttpWebResponse)objRequest.GetResponse(); using (StreamReader sr = new StreamReader(objResponse.GetResponseStream()) ) { result = sr.ReadToEnd(); // Close and clean up the StreamReader sr.Close(); } return result; }
Your HTML file is not going to interact with C# directly, but you can write some C# to behave as if it were the HTML file.
For example: there is a class called System.Net.WebClient with simple methods:
using System.Net; using System.Collections.Specialized; ... using(WebClient client = new WebClient()) { NameValueCollection vals = new NameValueCollection(); vals.Add("test", "test string"); client.UploadValues("http://www.someurl.com/page.php", vals); }
For more documentation and features, refer to the MSDN page.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With