![](/img/trans.png)
[英]Create HTTP post request and receive response using C# console application
[英]Create HTTP post request and receive response in C# application and parsing the response
我正在尝试编写一个将http表单发布到CGI脚本并获得响应的C#程序。
响应是网页,需要解析,因为我只需要解析其中的一部分。
有什么方法可以获取响应网页并将其解析为C#吗?
您可以结合使用WebClient
发送表单和检索响应,以及结合使用HtmlAgilityPack来分析此任务的结果。
您可以使用WebClient类:
using System.Collections.Specialized;
using System.Net;
class Program
{
static void Main()
{
using (var client = new WebClient())
{
var values = new NameValueCollection();
values["foo"] = "bar";
values["bar"] = "baz";
var url = "http://foo.bar/baz.cgi";
byte[] result = client.UploadValues(url, values);
// TODO: do something with the result
// for example if it represents text you could
// convert this byte array into a string using the proper
// encoding: string sResult = Encoding.UTF8.GetString(result);
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.