使用FtpWebRequest将文件从天蓝色托管应用程序上载到外部ftp站点

时间:2016-09-27 18:57:20

标签: vb.net azure file-upload ftp ftpwebrequest

  

更新:我发现这不是天蓝色而不是问题   另一方实际上正在进行白名单上市   连接。

我实现了一个服务,open和ftp连接将文件上传到远程FTP服务器。 代码在我的桌面上运行完美(!),但是如果我将它发布到azure托管云并使用完全相同的参数执行,我会收到WebException并显示消息'远程服务器返回错误:(530)Not登录。'我在azure控制台中找不到任何相关设置。我也尝试了不同的设置,如开/关保持活着和被动,但结果是一样的。你们知道这可能是什么问题吗?

参考代码:

            Dim request As FtpWebRequest

            request = TryCast(WebRequest.Create($"ftp://{_host}/inventory/{_uploadFilename}"), FtpWebRequest)
            request.Method = WebRequestMethods.Ftp.UploadFile
            request.Credentials = New NetworkCredential(_username, _password)
            request.UsePassive = True
            request.UseBinary = True
            request.KeepAlive = False

            Using fs As FileStream = File.OpenRead(_fileName)
                Dim buffer As Byte() = New Byte(fs.Length - 1) {}
                fs.Read(buffer, 0, buffer.Length)
                fs.Close()
                Dim requestStream As Stream = request.GetRequestStream()
                requestStream.Write(buffer, 0, buffer.Length)
                requestStream.Flush()
                requestStream.Close()


                Dim response As FtpWebResponse = DirectCast(request.GetResponse(), FtpWebResponse)
                Return Ok($"Remote server response: {response.StatusDescription}")

            End Using

2 个答案:

答案 0 :(得分:1)

NetworkCredential从哪里获取凭据?

Windows身份验证(Kerberos / NTLM)在Azure中无法运行。您需要使用FTP登录(如USERPASS FTP命令)。

尝试从Azure进行FTP传输时,始终使用 PASSIVE 模式。

Imports System.IO
Imports System.Net

Module Module1

    Sub Main()
        Dim request As FtpWebRequest
        Dim _host = "remote.ftpserver.com"
        Dim _uploadFilename = "file.txt"
        Dim _filename = "file.txt"
        Dim _username = "Username"
        Dim _password = "PaSsw0rD"

        request = TryCast(WebRequest.Create($"ftp://{_host}/path/{_uploadFilename}"),
            FtpWebRequest)
        request.Method = WebRequestMethods.Ftp.UploadFile
        request.Credentials = New NetworkCredential(_username, _password)
        request.UsePassive = True
        request.UseBinary = True
        request.KeepAlive = False

        Using fs As FileStream = File.OpenRead(_filename)
            Dim buffer As Byte() = New Byte(fs.Length - 1) {}
            fs.Read(buffer, 0, buffer.Length)
            fs.Close()
            Dim requestStream As Stream = request.GetRequestStream()
            requestStream.Write(buffer, 0, buffer.Length)
            requestStream.Flush()
            requestStream.Close()
            Dim response As FtpWebResponse = DirectCast(request.GetResponse(),
                FtpWebResponse)
            Console.WriteLine($"Remote server response: {response.StatusCode}, 
                {response.StatusDescription}")
        End Using
    End Sub

End Module

如果格式遍布整个地方,请道歉。这是我写VB的第一次尝试。

为了清楚起见,在Kudu's DebugConsole中运行此功能 - 与Web App相同的沙箱:

Run-in-Kudu

在远程FTP服务器上:

ftp> ls file.txt
200 PORT command successful.
125 Data connection already open; Transfer starting.
file.txt
226 Transfer complete.
ftp: 13 bytes received in 0.02Seconds 0.81Kbytes/sec.

ftp> bin
200 Type set to I.

ftp> get file.txt
200 PORT command successful.
125 Data connection already open; Transfer starting.
226 Transfer complete.
ftp: 24 bytes received in 0.18Seconds 0.14Kbytes/sec.

ftp> !type file.txt
Upped with FtpWebRequest

答案 1 :(得分:0)

我发现对于天蓝色而不是另一方实际上在传入连接上进行白名单时,这不是问题。