使用.NET(dotnet)通过HTTPS下载文件 [英] Download file over HTTPS using .NET (dotnet)
问题描述
我想使用VB.NET(最好)或通过HTTPS下载一个文件。
I would like to download a file using VB.NET (preferably) or C# via HTTPS.
我有这个代码可以通过纯HTTP下载文件: / p>
I have this code to download a file over plain HTTP:
Dim client As WebClient = New WebClient()
Dim wp As WebProxy = New WebProxy("[IP number of our proxy server]", [port number of our proxy server])
wp.Credentials = CredentialCache.DefaultCredentials
client.Proxy = wp
client.DownloadFile("http://sstatic.net/so/img/logo.png", "c:\logo.png")
如何更改此代码以下载存储在HTTPS服务器上的文件?我想这与添加凭证或其他东西有关。
How do I change this code to download a file that is stored on an HTTPS-server? I guess it has something to do with adding credentials or something.
推荐答案
你只需要点该地址到您的HTTPS资源并通知您的凭据:
You just need to point that address to your HTTPS resource and to inform your credential:
client.Credentials = new NetworkCredential("username", "password");
client.DownloadFile("https://your.resource.here", @"localfile.jog")
您正在谈论如何登录受HTML表单登录保护的网站。我之前写过这段代码,你可以修改它登录你的远程站点: Orkut登录代码
You're talking about how to log into a site protected by a HTML form login. I wrote this code sometime ago and you could to adapt it to login into your remote site: Orkut Login Code
需要注意的事项:
- 如果这是一个ASP.NET网站,您需要首先调用它来获取
__ EVENTTARGET
和__ EVENTARGUMENT
值,因为它们需要处理您的登录回发。如果没有,请跳过此步骤。 - 您需要确定网站用于填写用户名和密码的名称
- 您必须添加一个
的CookieContainer
。它保留您的登录cookie,所以后续的呼叫使用该身份验证的上下文。 - 完成之后,您应该能够获取远程资源并下载
- If that's an ASP.NET site, you need to call it first to get
__EVENTTARGET
and__EVENTARGUMENT
values, as they're required to process your login postback. If it's not, skip this step. - You need to identify that names that site uses to fill your username and password
- You must to add a
CookieContainer
. It keeps your login cookie, so subsequent calls uses that authenticated context. - After all that, you should be able to get your remote resource and to download it
这篇关于使用.NET(dotnet)通过HTTPS下载文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!