如何使用https:// sites运行以下代码。 ? [英] How to run below code with https:// sites. ?
本文介绍了如何使用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
的凭据
属性 [ ^ ]到aNetworkCredential
实例 [ ^ ]包含正确的凭据。
The code to download a file from anhttps
site is exactly the same; you just pass thehttps
URL as the first parameter to theDownloadFile
method.
Thecsharpindepth.com
domain does not have a valid SSL certificate, so you can't access that site overhttps
.
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 theWebClient
'sCredentials
property[^] to aNetworkCredential
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屋!
查看全文