简体   繁体   English

使用C#中的WebBrowser控制ASPX页面中文件的下载

[英]Control download of file in aspx pages with WebBrowser in C#

I'm facing with a problem, How can I automatically download a file with webbrowser in C#? 我面临一个问题,如何在C#中使用webbrowser自动下载文件? I'm creating a automation tool, that makes a authentication on site and need to download a file on an aspx page, looking the action fired by the download in the chrome console button I can see the post data. 我正在创建一个自动化工具,该工具可以在站点上进行身份验证,并且需要在aspx页面上下载文件,在chrome控制台按钮中查看由下载触发的操作,我可以看到发布数据。 My challenge is, download the file without open download dialog. 我的挑战是,在没有打开下载对话框的情况下下载文件。 Any suggestion of how handle post response? 关于如何处理后期回复的任何建议? Or another way to do this stuff works? 或做这件事的另一种方法可行?

EDIT 编辑

I'm using HttpWebRequest to make the post solicitation but when I handle the Response I only can see the html page instead of the attachment file. 我正在使用HttpWebRequest进行post请求,但是当我处理Response我只能看到html页面而不是附件文件。 Using google chrome dev tools I receive the following information about the download file: 使用google chrome开发工具,我收到有关下载文件的以下信息:

Response headers 响应头

Cache-Control:private
content-disposition:attachment; filename=numerosserieteste_D.txt
Content-Type:text/plain
Date:Mon, 29 Feb 2016 13:25:53 GMT
Server:Microsoft-IIS/7.5
Transfer-Encoding:chunked
X-AspNet-Version:4.0.30319
X-UA-Compatible:IE=EmulateIE8

How can I use this information in my application? 如何在我的应用程序中使用此信息?

use WebClient instead of WebBrowser: 使用WebClient代替WebBrowser:

using System.Net;



WebClient client = new WebClient();
        client.Credentials = new NetworkCredential("username", "password");
        client.DownloadFile("address of file", "local filename");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM