2014-10-09 155 views
1

我試圖上傳文件到FTP服務器使用WebClient,但我不斷收到同樣的錯誤如下: 遠程服務器返回一個錯誤: (401)未經授權。Webclient:遠程服務器返回一個錯誤:(401)未授權

正如您可以在代碼示例中看到的,我嘗試了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 

回答

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 
相關問題