Webclient:远程服务器返回错误:(401)未经授权

时间:2014-10-09 10:18:28

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

我正在尝试使用WebClient将文件上传到ftp服务器,但我一直得到同样的错误,如下所示: 远程服务器返回错误:(401)未经授权。

正如您在代码示例中所看到的,我在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

1 个答案:

答案 0 :(得分:0)

问题的解决方案是。

 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