出于安全原因,我已经在底部更改了凭据等,但是有人可以提供任何有关为什么不起作用的建议吗?
我在FileZilla上使用这些凭据没有任何问题。
代码:
Imports System.Net
Imports System.IO
Public Class Form1
Private Sub FtpUploadFile(ByVal filetoupload As String, ByVal ftpuri As String, ByVal ftpusername As String, ByVal ftppassword As String)
' Create a web request that will be used to talk with the server and set the request method to upload a file by ftp.
Dim ftpRequest As FtpWebRequest = CType(WebRequest.Create(ftpuri), FtpWebRequest)
Try
ftpRequest.Method = WebRequestMethods.Ftp.UploadFile
' Confirm the Network credentials based on the user name and password passed in.
ftpRequest.Credentials = New NetworkCredential(ftpusername, ftppassword)
' Read into a Byte array the contents of the file to be uploaded
Dim bytes() As Byte = System.IO.File.ReadAllBytes(filetoupload)
' Transfer the byte array contents into the request stream, write and then close when done.
ftpRequest.ContentLength = bytes.Length
Using UploadStream As Stream = ftpRequest.GetRequestStream()
UploadStream.Write(bytes, 0, bytes.Length)
UploadStream.Close()
End Using
Catch ex As Exception
MessageBox.Show(ex.Message)
Exit Sub
End Try
MessageBox.Show("Process Complete")
End Sub
Private Sub Upload_Click(sender As Object, e As EventArgs) Handles Upload.Click
FtpUploadFile("D:\Directory\Filename.csv", "ftp://www.****.com/test.csv", "****@*****.com", "*********")
End Sub
End Class
按照要求我尝试使用WebClient参见下面的代码 尝试
Dim up As New WebClient
up.Credentials = New NetworkCredential("username", "password")
up.UploadFile("ftp://www.domain.com/test.csv", "D:\test\test.csv")
MsgBox("Done")
up.Dispose()
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub
结果是一样的; 远程服务器返回错误:(530)(未登录)
答案 0 :(得分:0)
试试这个:
Try
Dim ftp_webrequest As FtpWebRequest = DirectCast(WebRequest.Create("ftp://domainname/filename"), FtpWebRequest)
With ftp_webrequest
.EnableSsl = False
.UsePassive = False
.Credentials = New NetworkCredential("username", "password")
.Method = WebRequestMethods.Ftp.UploadFile
End With
Dim file() As Byte = System.IO.File.ReadAllBytes("filepath")
Dim strz As System.IO.Stream = ftp_webrequest.GetRequestStream()
strz.Write(file, 0, file.Length)
strz.Close()
strz.Dispose()
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try