远程网站中有一个页面,可以在表单中输入输入值,单击按钮并在输出表单中获得结果。 我想向页面发送填写必要输入表单的请求,提交并获得填写输出表单的结果页面。
所有具有类似主题的线程都会提供如下代码示例:
// Create a request using a URL that can receive a post.
WebRequest request = WebRequest.Create("http://www.site.com");
// Set the Method property of the request to POST.
request.Method = "POST";
// Create POST data and convert it to a byte array.
string postData = string.Format("inputParam=value");
byte[] byteArray = Encoding.UTF8.GetBytes(postData);
// Set the ContentType property of the WebRequest.
request.ContentType = "application/x-www-form-urlencoded";
// Set the ContentLength property of the WebRequest.
request.ContentLength = byteArray.Length;
// Get the request stream.
Stream dataStream = request.GetRequestStream();
// Write the data to the request stream.
dataStream.Write(byteArray, 0, byteArray.Length);
// Close the Stream object.
dataStream.Close();
// Get the response.
WebResponse response = request.GetResponse();
// Get the stream containing content returned by the server.
dataStream = response.GetResponseStream();
response.Close();
它返回一个带有填充输入表单的页面,但是输出字段仍然是空白的,就像它没有单击提交按钮一样。
如何从C#代码提交并接收带输出数据的html文档?
答案 0 :(得分:0)
将webresponse部分更改为以下内容;
WebResponse response = request.GetResponse ();
dataStream = response.GetResponseStream ();
StreamReader reader = new StreamReader (dataStream);
string responseFromServer = reader.ReadToEnd ();