【问题标题】:Webclient: The remote server returned an error: (401) UnauthorizedWebclient: 远程服务器返回错误: (401) Unauthorized
【发布时间】:2014-10-09 10:18:27
【问题描述】:

我正在尝试使用 WebClient 将文件上传到 ftp 服务器,但我不断收到相同的错误,如下所示: 远程服务器返回错误:(401) Unauthorized。

正如您在代码示例中看到的那样,我在 try catch 表单中尝试了 4 种不同的上传方法。你能帮我解决这个问题吗?

   Dim wc As New WebClient
            wc.Credentials = New NetworkCredential(ftp.username, ftp.password) 


            Try 'UploadFile Method (String, String)
                Dim dddd As Byte() = wc.UploadFile("https://ftp.something/something/something/", serverpath)
            Catch ex1 As Exception 
                Try 'UploadFile Method (Uri, String)
                    Dim dddd As Byte() = wc.UploadFile(ftp.myUri, serverpath)
                Catch ex2 As Exception
                    Try  'UploadFile Method(Of String, String, String)
                        Dim dddd As Byte() = wc.UploadFile("https://ftp.something/something/something/", "POST", serverpath)
                    Catch ex33 As Exception

                        Try 'UploadFile Method (Uri, String, String)
                            Dim dddd As Byte() = wc.UploadFile(ftp.myUri, "POST", serverpath)
                        Catch ex As Exception
                        End Try
                    End Try
                End Try

            End Try

【问题讨论】:

    标签: .net vb.net ftp webclient ftp-server


    【解决方案1】:

    问题的解决方案是。

     Dim wc As New WebClient()
                Dim credCache As New CredentialCache()
                credCache.Add(New Uri("https://ftp.something/something/something/"), "Basic", New NetworkCredential(ftp.username, ftp.password))
    
                wc.Credentials = credCache
    

    【讨论】:

      猜你喜欢
      • 2011-01-03
      • 1970-01-01
      • 1970-01-01
      • 2013-03-24
      • 2014-12-26
      相关资源
      最近更新 更多