How to Programmatically Fill in a Form and 'Post' a Web Page

How do you programmatically fill in a form and 'POST' a web page?

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);

WebResponse res = req.GetResponse();
StreamReader sr = new StreamReader(res.GetResponseStream());
string returnvalue = sr.ReadToEnd();

c# - programmatically form fill and submit login

yes some sites block request. but you can check the login with auth cookie. Use HttpWebRequest/HttpWebResponse

HttpWebRequest request = (HttpWebRequest)WebRequest.Create(new Uri(url));

byte[] data = Encoding.Default.GetBytes("item1=11111&item2=22222&Item3=33333");
request.Method = "POST";
request.ContentLength = data.Length;

Stream sout = request.GetRequestStream();
sout.Write(data, 0, data.Length);

request.CookieContainer = new CookieContainer();
HttpWebResponse response = (HttpWebResponse) request.GetResponse();

now check response.Cookies for auth cookie

Filling out a web form automatically with a text file

You can write a json object, then a javascript function that iterate that json and figure out by each value the selector of the correct input to use and set the correct value.
Then when the page open, use the dev console.
paste the json object, paste the function.
Run the function and the form should get fill out.

Fill in web form from an app and submit

I have used something like this in the past. However you will have to load a webview (though you dont need to show it.) then do something like below. Keep in mind that if the site changes the id down the road this approach will not work.

name=document.getElementById('nameInput').value='" yourNameValue "'");

write a code to fill a form on the web and read one of the elements

Yes we can.. one way to do it using VBS is here:

Dim objIE

Set objIE = CreateObject("InternetExplorer.Application")
objIE.Visible = 1

objIE.navigate "<login url>"
WScript.Echo "Opening login page..."
WScript.Sleep 100
Do while objIE.busy
Wscript.sleep 200

WScript.Echo "Setting credentials..."
objIE.Document.getElementByID("ap_email").Value = user 'id of the input element

objIE.Document.getElementByID("ap_password").Value = pass

WScript.Echo "Email and password set!"

Call objIE.Document.Forms(0).submit


Related Topics

Leave a reply