如何使用https:// sites运行以下代码。 ? [英] How to run below code with https:// sites. ?

查看:120
本文介绍了如何使用https:// sites运行以下代码。 ?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何为https网站下载pdf文件?





How to do this for https sites for download pdf file??


using (WebClient client = new WebClient())
{
    client.DownloadFile("http://csharpindepth.com/About.aspx", 
                        @"c:\Users\Jon\Test\foo.txt");
}









提前感谢





thanks in advance

推荐答案

https 站点下载文件的代码完全相同;您只需将 https URL作为第一个参数传递给 DownloadFile 方法。

< br $>
csharpindepth.com 域没有有效的SSL证书,因此您无法通过 https <访问该网站/ code>。



您在评论中提到的401错误意味着您要求的网址需要身份验证,但您尚未提供身份验证细节。设置 WebClient 凭据属性 [ ^ ]到a NetworkCredential 实例 [ ^ ]包含正确的凭据。

The code to download a file from an https site is exactly the same; you just pass the https URL as the first parameter to the DownloadFile method.

The csharpindepth.com domain does not have a valid SSL certificate, so you can't access that site over https.

The 401 error you mentioned in the comments means you're requesting a URL which requires authentication, but you haven't provided the authentication details. Set the WebClient's Credentials property[^] to a NetworkCredential instance[^] containing the correct credentials.
using (WebClient client = new WebClient())
{
    client.Credentials = new NetworkCredential("username", "password");
    client.DownloadFile("https://.../file.pdf", @"c:\Users\Jon\Test\downloaded-file.pdf");
}


这篇关于如何使用https:// sites运行以下代码。 ?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆