使用 WebClient 通过 https 下载文件
我目前正在尝试使用 System.Net.WebClient 通过 https 下载文件
该文件是在本地创建的,但是当我打开它时,它只有文本“虚拟用户 xxxxxx 已登录”。其中 xxxxxx 是发送的用户名。
我已经尝试让它工作一段时间了,但结果相同。我想知道其他人是否也遇到过这个问题,如果有的话,你能解决这个问题吗?
这是我尝试下载的代码。
Private Sub btnDownload_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDownload.Click
Dim webConnection As New WebClient()
Dim credentials As New NetworkCredential("xxxxxx", "password")
Try
webConnection.BaseAddress = "https://ftp.sitename.com/content/"
webConnection.Credentials = credentials
Catch ex As Exception
MsgBox(ex.Message)
End Try
Try
webConnection.DownloadFile("https://ftp.sitename.com/content/source_file.dat", "destination_file.dat")
MsgBox("File Downloaded!")
Catch ex As Exception
MsgBox(ex.Message)
End Try
I am currently trying to download a file over https using System.Net.WebClient
The file gets created locally, but when I open it, it just has the text 'Virtual user xxxxxx logged in.' where xxxxxx is the username that was sent in.
I have been trying to get this working for a while with the same results. I wanted to find out if anyone else ever had this issue, and if so, were you able to move past it?
Here is the code where I try to do the download.
Private Sub btnDownload_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDownload.Click
Dim webConnection As New WebClient()
Dim credentials As New NetworkCredential("xxxxxx", "password")
Try
webConnection.BaseAddress = "https://ftp.sitename.com/content/"
webConnection.Credentials = credentials
Catch ex As Exception
MsgBox(ex.Message)
End Try
Try
webConnection.DownloadFile("https://ftp.sitename.com/content/source_file.dat", "destination_file.dat")
MsgBox("File Downloaded!")
Catch ex As Exception
MsgBox(ex.Message)
End Try
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
DownloadFile
的第一个参数必须是要下载的文件的完整 URL。在这里,您似乎正在传递一个目录。如果你想下载文件“file_name.dat”并将其保存到当前工作目录作为“file_name.dat”,你可以这样写:The first parameter to
DownloadFile
has to be the full URL of the file you want to download. Here it looks like you're passing a directory. If you want to download the file "file_name.dat" and save it to the current working directory as "file_name.dat", you would write:这在我的情况下适用于“https://”-download(我是 WebClient & Co. 的新人,因此代码的某些部分可能是多余的,或者仅适合这种情况......),
我需要位图作为对象,因此此处未实现保存流:
This worked in my case for "https : //"-download (I'm new in WebClient & Co., so some parts of the code might be redundant, or fit to this scenario only...),
I needed the bitmap as object, so saving the stream is not implemented here: