简体   繁体   English

C#HTTP编程

[英]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... 填写表格...

  1. Find all of the INPUT or TEXTAREA elements that you want to fill out. 找到要填写的所有INPUT或TEXTAREA元素。
  2. Build the data string that you are going to send back to the server. 构建要发送回服务器的数据字符串。 The string is formatted like "name1=value1&name2=value2" (just like in the querystring). 该字符串的格式类似于“ name1 = value1&name2 = value2”(就像在查询字符串中一样)。 Each value will need to be URL encoded. 每个值都需要进行URL编码。
  3. If the form's "method" attribute is "GET", then take the URL in the "action" attribute, add a "?" 如果表单的“方法”属性为“ GET”,则在“操作”属性中获取URL,并添加“?”。 and the data string, then make a "GET" web request to the URL. 和数据字符串,然后对URL进行“ GET” Web请求。
  4. If the form's "method" is "POST", then the data is submitted in a different area of the web request. 如果表单的“方法”为“ POST”,则数据将在Web请求的其他区域中提交。 Take a look at this page for the C# code. 此页面上查看C#代码。

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... 假设您正在使用内容不熟悉的表单,则可能...

  1. pull the page with the form via an HttpWebRequest. 通过HttpWebRequest拉表单的页面。
  2. load it into an XmlDocument 加载到XmlDocument中
  3. Use XPath to traverse/select the form elements. 使用XPath遍历/选择表单元素。
  4. Build your query string/post data based on the elements. 根据这些元素构建查询字符串/发布数据。
  5. Send the data with HttWebRequest 使用HttWebRequest发送数据

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: 参考文献:

If you don't want to go the HttpWebRequest route, I would suggest WatiN . 如果您不想走HttpWebRequest路线,我建议您使用WatiN Makes it very easy to automate IE or Firefox and not worry about the internals of the HTTP requests. 使IE或Firefox自动化非常容易,而不必担心HTTP请求的内部。

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

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