如何在 csharp 上提交后获得响应页面

本文关键字:响应 提交 csharp | 更新日期: 2023-09-27 18:31:31

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

所有具有类似主题的线程都给出了如下代码示例:

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

如何在 csharp 上提交后获得响应页面

将网络响应部分更改为以下内容;

    WebResponse response = request.GetResponse ();                
    dataStream = response.GetResponseStream ();
    StreamReader reader = new StreamReader (dataStream);
    string responseFromServer = reader.ReadToEnd ();