使用 WebClient 通过 https 下载文件

发布于 2024-11-08 02:53:09 字数 869 浏览 0 评论 0原文

我目前正在尝试使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

感情洁癖 2024-11-15 02:53:09

DownloadFile 的第一个参数必须是要下载的文件的完整 URL。在这里,您似乎正在传递一个目录。如果你想下载文件“file_name.dat”并将其保存到当前工作目录作为“file_name.dat”,你可以这样写:

webConnection.DownloadFile("https://ftp.sitename.com/content/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:

webConnection.DownloadFile("https://ftp.sitename.com/content/file_name.dat", "file_name.dat")
对你而言 2024-11-15 02:53:09

这在我的情况下适用于“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

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:

    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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文