【问题标题】:DownloadFile via System.Net.WebClient doesn't works通过 System.Net.WebClient 下载文件不起作用
【发布时间】:2012-09-22 07:39:49
【问题描述】:

我无法使用以下代码下载任何文件:

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
    DownloadFile("http://mirror.switch.ch/ftp/mirror/videolan/vlc/2.0.3/vlc-2.0.3.tar.xz", "C:\TEST\vlc-2.0.3.tar.xz")
End Sub

Public Sub DownloadFile(ByVal _URL As String, ByVal _SaveAs As String)
        Dim _WebClient As New System.Net.WebClient()
        _WebClient.DownloadFile(_URL, _SaveAs)
End Sub

会弹出以下错误:(从德语翻译成英语):

来自 Microsoft 错误弹出窗口:

==> System.dll 中出现“System.Net.WebException”类型的未处理异常。

在调试器中:

==> 异常:已触发:“已指定无效参数”(System.Net.Sockets.SocketException)已触发 System.Net.Sockets.SocketException。 "指定了一个无效的参数"

完整代码:

Imports System
Imports System.Net

Public Class Form2
    Dim str_hd As String

    Private Sub Form2_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load


        For Each drive As System.IO.DriveInfo In My.Computer.FileSystem.Drives
            Dim dbl_capacity As Double
            Dim dbl_free As Double
            Dim dbl_used As Double

            If drive.IsReady Then
                dbl_capacity = drive.TotalSize / 1073741824
                dbl_free = drive.TotalFreeSpace / 1073741824
                dbl_used = dbl_capacity - dbl_free

                dbl_capacity = Fix(dbl_capacity * 1) / 1
                dbl_free = Fix(dbl_free * 10) / 10
                dbl_used = Fix(dbl_used * 10) / 10


                str_hd = str_hd & "Laufwerk " & drive.Name & "  -  " _
                        & drive.VolumeLabel & vbCrLf _
                        & "Benutzt: " & dbl_used & " GB" & " / " & dbl_capacity & " GB" & vbCrLf & "Frei: " _
                        & dbl_free & "GB" & vbCrLf & "-------------------------------" & vbCrLf
            End If
        Next

        txtbox_hd.Text = str_hd

    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Form1.Close()
    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        DownloadFile("http://www.aerztekammer-hamburg.de/berufsrecht/Berufsordnung.pdf", "C:\TEST\Berufsordnung.pdf")
    End Sub



    Public Sub DownloadFile(ByVal _URL As String, ByVal _SaveAs As String)
            Dim _WebClient As New System.Net.WebClient()
            _WebClient.DownloadFile(_URL, _SaveAs)
    End Sub

End Class

【问题讨论】:

    标签: vb.net downloadfile


    【解决方案1】:

    文件夹C:\TEST 是否存在?另外,从文档中:

    在中间层应用程序(例如 ASP.NET 页面)中使用此方法时,如果执行应用程序的帐户没有访问文件的权限,您将收到错误消息。

    Wenn Sie dieese Methode in einer Anwendung der Middle-Tear verwenden, z. B. einer ASP.NET-Seite, erhalten Sie eine Fehlermeldung, wenn das Konto, unter dem die Anwendung ausgeführt wrd, nicht über die Berechtigung zum Zugriff auf die Datei verfügt。

    【讨论】:

    • 是的,文件夹“C:\TEST”存在。我该如何为此设置一个帐户?当您在浏览器中键入给定的 url 时,该文件将在没有权限检查的情况下下载...
    • 您正在创建 ASP.NET 页面吗?如果 Internet 信息服务没有对​​该文件夹的写访问权限,上述内容只会产生错误。
    • 不,我不创建任何 ASP.NET 页面。我只尝试从其 ftp 服务器下载该文件。我将在 1 分钟内添加完整的代码..
    • 啊,我从列表中的其他异常中看到创建 WebProxy 时发生错误。可能是您尝试使用网络代理来下载文件吗?代码可以在不需要使用代理的网络上运行吗?
    • 谢谢Thorsten...实际上我们公司有一个代理... ;) 现在工作得很好:))
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-01-11
    • 2013-04-12
    • 2011-03-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多