gpt4 book ai didi

vb.net - 使用 FtpWebRequest 将文件从 azure 托管应用程序上传到外部 ftp 站点

转载 作者:行者123 更新时间:2023-12-03 05:56:59 25 4
gpt4 key购买 nike

UPDATE: I found out that it is not an issue with azure rather the other party who are actually doing white-listing on incoming connection.

我实现了一个服务,它打开 ftp 连接来将文件上传到远程 FTP 服务器。 代码在我的桌面上运行完美(!),但是如果我将其发布到 azure 托管云并使用完全相同的参数执行,我会收到 WebException 并显示消息“远程服务器返回错误:(530)不”已登录。' 我在 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

最佳答案

NetworkCredential 从哪里获取凭据?

Windows 身份验证 (Kerberos/NTLM) 在 Azure 中不起作用。您需要使用 FTP 登录(如 USERPASS FTP 命令)。

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

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 应用程序相同的沙箱:

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

关于vb.net - 使用 FtpWebRequest 将文件从 azure 托管应用程序上传到外部 ftp 站点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39732350/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com