C# 以编程方式按网页上的按钮

C# 以编程方式按网页上的按钮,c#,asp.net,.net,button,C#,Asp.net,.net,Button,我们的内部网上有一个网页位于http://server1/rsyncwebgui.php它为我们提供了一种在两个文件共享之间触发rSync的快速方法。出于无趣的安全原因,这是我们必须走的路线 该网页如下所示: <html> <head> <meta http-equiv="Content-Type" content="text/html;charset=utf-8"> <title>Rsync web gui</title> &

我们的内部网上有一个网页位于
http://server1/rsyncwebgui.php
它为我们提供了一种在两个文件共享之间触发rSync的快速方法。出于无趣的安全原因,这是我们必须走的路线

该网页如下所示:

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

rsyncwebgui
window.onload=function(){setTimeout(submitForm,10000);}
函数submitForm(){document.getElementById('myform').submit();}
在server2和server3之间执行rsync

window.onload=函数(){
如果(确认('您确定要启动同步吗?')){
var formobj=document.getElementById('myform');
formobj.elements['resync'].value='true';
formobj.submit();
}
}

当用户单击按钮时,会弹出一个“确定/取消”对话框,要求他们确认。单击“确定”后,页面将发回并触发rsync


如何从远程C#控制台应用程序驱动此交互?

现在无法签入VS,但您将以以下内容结束:

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

使用自动化库,如或。两者都是免费的、非常相似的API,并且都有.Net库

它们用于网络测试,但也适用于任何网络自动化(我使用它们提交和修改易趣拍卖:)


两者都有多个浏览器的驱动程序。

+1用于WatiN或Selenium,HiTech Magic称之为。你也可以试试Telerik。(披露:我是测试工作室的传道者,所以我有点偏见…)


您还可以使用Watir,它可以让您创建一个简单的Ruby脚本来从命令行调用。我使用Ruby和Watir处理类似的各种情况。

您尝试了什么?看来你必须发出Http Post请求嗨,史蒂夫B,我什么都没试过。我不熟悉.Net的这个领域。我猜我会做一些HTTP POST,但除此之外,我不知道我需要做什么。看起来不错。谢谢你,史蒂夫。刚刚更改:
req.Method=“POST”