1

I'm trying to upload a file to an ftp server using WebClient, but I keep getting the same error are listed below: The remote server returned an error: (401) Unauthorized.

As you can see in the code example, I have tried 4 different uploads methods in try catch form. Can you help me with this problem?

   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
Zaz
  • 1,074
  • 3
  • 17
  • 29

1 Answers1

0

The solution for the problem was.

 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
Zaz
  • 1,074
  • 3
  • 17
  • 29