0

远程网站有一个页面,可以在表单中输入输入值,单击按钮并在输出表单中获得结果。我想向页面发送一个请求并填写必要的输入表单,提交它并获得一个填写了输出表单的结果页面。

所有具有相似主题的线程都给出了这样的代码示例:

// 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 文档?

4

1 回答 1

0

将 webresponse 部分更改为以下内容;

    WebResponse response = request.GetResponse ();                
    dataStream = response.GetResponseStream ();
    StreamReader reader = new StreamReader (dataStream);
    string responseFromServer = reader.ReadToEnd ();
于 2012-04-19T07:13:36.177 回答