[英]C# HTTP programming
i want to build a piece of software that will process some html forms, the software will be a kind of bot that will process some forms on my website automatically. 我想构建一个可以处理某些html表单的软件,该软件将是一种可以自动处理我网站上的某些表单的bot。
Is there anyone who can give me some basic steps how to do this job...Any tutorials, samples, books or whatever can help me. 有谁可以给我一些基本的步骤来完成这项工作...任何教程,示例,书籍或任何可以帮助我的东西。
Can some of you post an working code with POST method ? 你们中的一些人可以使用POST方法发布有效的代码吗?
Check out How to: Send Data Using the WebRequest Class . 查看如何:使用WebRequest类发送数据 。 It gives an example of how create a page that posts to another page using the HttpWebRequest class .
它提供了一个示例,说明如何使用HttpWebRequest类创建一个页面发布到另一个页面。
To fill out the form... 填写表格...
To expand on David and JP's answers': 扩展David和JP的答案:
Assuming you're working with forms whose contents you're not familiar with, you can probably... 假设您正在使用内容不熟悉的表单,则可能...
If the form's structure is known in advance, you can really just start at #4. 如果表单的结构是事先已知的,那么您真的可以从#4开始。
(untested) example (my XPath is not great so the syntax is almost certainly not quite right): (未经测试的)示例(我的XPath不好,所以语法几乎肯定不太正确):
HttpWebRequest request;
HttpWebResponse response;
XmlDocument xml = new XmlDocument();
string form_url = "http://...."; // you supply this
string form_submit_url;
XmlNodeList element_nodes;
XmlElement form_element;
StringBuilder query_string = new StringBuilder();
// #1
request = (HttpWebRequest)WebRequest.Create(form_url));
response = (HttpWebResponse)request.GetResponse();
// #2
xml.Load(response.GetResponseStream());
// #3a
form_element = xml.selectSingleNode("form[@name='formname']");
form_submit_url = form_element.GetAttribute("action");
// #3b
element_nodes = form_element.SelectNodes("input,select,textarea", nsmgr)
// #4
foreach (XmlNode input_element in element_nodes) {
if (query_string.length > 0) { query_string.Append("&"); }
// MyFormElementValue() is a function/value you need to provide/define.
query_string.Append(input_element.GetAttribute("name") + "=" + MyFormElementValue(input_element.GetAttribute("name"));
}
// #5
// This is a GET request, you can figure out POST as needed, and deduce the submission type via the <form> element's attribute.
request = (HttpWebRequest)WebRequest.Create(form_submit_url + "?" + query_string.ToString()));
References: 参考文献:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.