我目前正在尝试使用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
答案 0 :(得分:0)
DownloadFile
的第一个参数必须是您要下载的文件的完整网址。在这里看起来你正在传递一个目录。如果要下载文件“file_name.dat”并将其作为“file_name.dat”保存到当前工作目录,则应写入:
webConnection.DownloadFile("https://ftp.sitename.com/content/file_name.dat", "file_name.dat")
答案 1 :(得分:0)
这适用于我的情况" https://" -download(我在WebClient& Co.中是新的,所以代码的某些部分可能是多余的,或者适合于这个场景只...), 我需要将位图作为对象,因此这里没有实现保存流:
Public Shared Function GetImage(url As string) As Bitmap
Dim x As Bitmap = Nothing
Try
Dim urlpath = $"{url}"
Dim cred = $"user:password"
Dim req = WebRequest.Create(urlpath)
req.Headers("Authorization") = "Basic " + Convert.ToBase64String(Encoding.ASCII.GetBytes(cred))
req.ContentType = "application/json"
Dim wr As WebResponse = req.GetResponse
Dim recstr = wr.GetResponseStream
x = New Bitmap(recstr)
Catch ex As WebException
End Try
Return x
End Function