简体   繁体   English

使用C#HttpWebRequest模拟Web表单按钮单击

[英]simulate a web form button click using C# HttpWebRequest

I'm trying to send a post request to simulate pressing a button that resides on a webpage form using C# HTMLWebRequest. 我正在尝试发送一个发布请求,以模拟按下使用C#HTMLWebRequest驻留在网页表单上的按钮。 The form on the webpage looks like the following: 该网页上的表单如下所示:

        <form method="post" action="HtmlAdaptor">
        <input type="hidden" name="action" value="invokeOp">
        <input type="hidden" name="name" 
          value='somevalue'>
        <input type="hidden" name="methodIndex" value="5">
        <hr align='left' width='80'>
        <h4>java.util.List methodName()</h4>
        <p>MBean Operation.</p>

        <input type="submit" value="Invoke">
        </form>

My code to send the HTML post and read HTML response: 我发送HTML帖子并阅读HTML响应的代码:

string webURL = "http://pageurl";
HttpWebRequest myRequest =(HttpWebRequest)WebRequest.Create(webURL);
myRequest.Method = "POST";
        byte[] lbPostBuffer = System.Text.Encoding.GetEncoding(1252).GetBytes(sb.ToString());
        myRequest.ContentType = "text/xml; charset=utf-8";
        myRequest.ContentLength = lbPostBuffer.Length;
        myRequest.Accept = "text/xml";

        Stream loPostData = myRequest.GetRequestStream();
        loPostData.Write(lbPostBuffer, 0, lbPostBuffer.Length);
        loPostData.Close();
        HttpWebResponse loWebResponse = (HttpWebResponse)myRequest.GetResponse();
        Encoding enc = System.Text.Encoding.GetEncoding(1252);
        StreamReader loResponseStream = new StreamReader(loWebResponse.GetResponseStream(), enc);
        string lcHtml = loResponseStream.ReadToEnd();

        loWebResponse.Close();

        loResponseStream.Close();

I have a string builder variable "sb" that gets converted to a byte array to be posted to the site. 我有一个字符串生成器变量“ sb”,该变量被转换为要发送到站点的字节数组。 My problem is I'm not sure what I should be sending in the sb.ToString() to submit the invoke button on that form. 我的问题是我不确定应该在sb.ToString()中发送什么内容以提交该表单上的调用按钮。

我已经通过提琴手找到了请求的格式。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM