简体   繁体   English

c#webclient.DownloadFileTaskAsync下载损坏的1KB PDF

[英]c# webclient.DownloadFileTaskAsync downloads a corrupted 1KB PDF

I have a WebClient created in a WebBrowser_Navigating event handler. 我在WebBrowser_Navigating事件处理程序中创建了一个WebClient。 It stops navigation (to prevent manual filedownload dialog) and passes the referred URL to the webclient's DownloadFileTaskAsync method. 它将停止导航(以防止手动文件下载对话框),并将引用的URL传递到Web客户端的DownloadFileTaskAsync方法。

await client.DownloadFileTaskAsync(e.Url, AppDomain.CurrentDomain.BaseDirectory + "\\SUCCESS.pdf");

I have already set the SecurityProtocolType to Tls12 and passed all cookies and other headers to the webclient. 我已经将SecurityProtocolType设置为Tls12,并将所有cookie和其他标头传递给了webclient。

The file expected is about 11 MB large. 预期文件约为11 MB。

I'll assume that you download the HTML website instead of the actual file. 我假设您下载的是HTML网站,而不是实际的文件。 If that is the case you will need to scrape the download link by using an HTML Parser and XPath to navigate inside the HTML (for example the HTML Agility Pack ). 如果是这种情况,您将需要使用HTML解析器和XPath来在HTML(例如HTML Agility Pack )中导航来刮取下载链接。

If that's not the case, can you maybe print out what exactly e.Url contains to see what URL you are trying to access with await client.DownloadFileTaskAsync(...) . 如果不是这种情况,您是否可以打印出e.Url确切包含的内容,以查看您尝试通过await client.DownloadFileTaskAsync(...)访问的URL。

Maybe another problem could be that you don't correctly dispose your HttpClient which might interfere with your file creation. 也许另一个问题是您没有正确配置HttpClient ,这可能会干扰文件创建。 It would be generous of you to add more information about your code to your question. 您很乐意向您的问题添加有关代码的更多信息。

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

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