Programmatically Pressing Buttons on a Web Page
.net, asp.net, button, c#
Solution
Can't check in VS now, but you will end with something like this :
string postDataStr = string.Format("resync=true&scanscheduled=&otherpara=xyz");
byte[] postData = Encoding.ASCII.GetBytes(postDataStr);
HttpWebRequest req = (HttpWebRequest)WebRequest.Create(new Uri("http://server1/rsyncwebgui.php"));
req.Method= "POST";
req.ContentType = "application/x-www-form-urlencoded";
req.ContentLength = postData.Length;
using(var reqStream = req.GetRequestStream())
{
reqStream.Write(postData, 0, postData.Length);
}
HttpWebResponse response = (HttpWebResponse )req.GetResponse();
Problem
There is a webpage on our intranet which resides at `http://server1/rsyncwebgui.php` which provides a quick way for us to trigger an rSync between two file shares. For uninteresting security reasons, this is the route we have to take. The web page looks like this: ``` <html> <head> <meta http-equiv="Content-Type" content="text/html;charset=utf-8"> <title>Rsync web gui</title> </head> <body> <script language="javascript"> window.onload = function () { setTimeout(submitForm, 10000); } function submitForm() { document.getElementById('myform').submit(); } </script> <h3> Execute rsync between server2 and server3</h3> <form id="myform" method="POST"> <input type="hidden" name="resync" value="false"> <input type="hidden" name="scanscheduled" value="false"> <input type="submit" value="Start sync"> </form> <p> <script language="javascript"> window.onload = function () { if (confirm('Are you sure to start the sync?')) { var formobj = document.getElementById('myform'); formobj.elements['resync'].value = 'true'; formobj.submit(); } } </script> </p> <p><a href="/rsyncwebgui.php">Refresh page</a></p> </body></html> ``` When a user clicks on the button, an "OK/Cancel" dialog pops up asking them to confirm. When OK is clicked, the page posts back and the rsync is triggered. How would I drive this interaction from a remote, C# console application?