C# - 如何提交 2 种不同的表单并下载 PDF 文件



我正在尝试提交表单(通过WebClient - C# WindowsForms(,但我还没有得到它。

我的HTTP页面顺序是:Username_Form> Password_Form>下载PDF文件

<!-- Username_Form.html -->
<form ... action="/AgenciaWeb/autenticar/autenticar.do">
    <input type="text" name="username" />
    <input type="submit" value="Continue" />
</form>
<!-- Password_Form.html -->
<form ... action="/AgenciaWeb/autenticar/validarSenha.do">
    <input type="password" name="password" />
    <input type="submit" value="Login" />
</form>
<!-- Password_Form.html -->
<div>
    <a href="PDF_download.pdf">Download</a>
</div>
所以我应该填写第一页中的所有表格,以

重定向到第二页以填写所有表格,然后获得最后一页并开始下载我的PDF文件。

问题是我没有在第一页收到提交。我正在做:

private void button1_Click(object sender, EventArgs e)
        {
            var url = "http://test_something.com:8080/AgenciaWeb/autenticar/autenticar.do";
            using (var client = new WebClient())
            {
                var values = new NameValueCollection();
                values["username"] = "user01";
                var response = client.UploadValues(url, "POST", values);
                var responseString = Encoding.Default.GetString(response);
                Console.WriteLine(responseString);
            }
        }

但是,我的"responseString"是完全相同的页面,填写了所有表单(输入类型="文本"名称=">用户名"值="user01">(,并且提交没有发生,就像我看不到第二页或与之交互一样(Password_Form.html(。

我错过了什么问题吗?

您可能需要检查响应,看看它是否返回 302 状态。如果是,则可以转到下一页,在服务器端成功设置值。您在此处执行的操作需要您手动遵循重定向。

相关内容

最新更新