FtpWebRequest无法识别URI

时间:2017-06-14 21:46:32

标签: vb.net ftp

我的问题是我似乎无法让uri工作。错误消息显示“无效的URI:无法确定URI的格式”。然后,当我尝试此行Dim ftpRequest As FtpWebRequest = CType(WebRequest.Create(ftpUri + ":" + port), FtpWebRequest)

时,我收到此错误消息“无法识别URI前缀”

这就是我所拥有的,并且在filezilla中测试时它非常完美:
主持人:sftp.icecream.com
用户名:softserve
密码:巧克力
港口:22
目标文件夹:SoManyFlavors

这是我的代码:

    Private Sub FtpUploadFile(ByVal fileToUpload As String, ByVal ftpUri As String, ByVal userName As String, ByVal password As String, ByVal port As String)

        Dim ftpRequest As FtpWebRequest = CType(WebRequest.Create(ftpUri), FtpWebRequest)

        Try
            ftpRequest.Method = WebRequestMethods.Ftp.UploadFile
            ftpRequest.Credentials = New NetworkCredential(userName, password)

            Dim bytes() As Byte = System.IO.File.ReadAllBytes(fileToUpload)

            ftpRequest.ContentLength = bytes.Length
            Using UploadStream As IO.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
    End Sub

我的电话:
FtpUploadFile("C:\Users\Desktop\icecream.txt", "sftp.icecream.com", "softserve", "chocolate", "22")
我也注意到ftp似乎还有一些选项,但我不确定哪一个最符合我的需求。

1 个答案:

答案 0 :(得分:3)

您需要specify the FTP protocol。将您的通话更改为FtpUploadFile("C:\Users\Desktop\icecream.txt", "ftp://sftp.icecream.com", "softserve", "chocolate", "22")

修复此错误后,您可能会收到其他错误。 .Net FTP方法不支持SFTP,这是您正在尝试连接的协议类型。