gpt4 book ai didi

c# - 无法从 Azure 应用程序使用 FtpWebRequest 连接到 ftp 服务器

转载 作者:太空宇宙 更新时间:2023-11-03 12:21:14 26 4
gpt4 key购买 nike

我可以使用本地 asp.net 应用程序将文件上传到 azure ftp 服务器,但在将应用程序发布到 azure 后不起作用,它让我无法登录

这是我连接到 ftp 服务器的代码,但部署到 azure 时它不起作用

private void ExecuteRequest(string url, string action, string fileToUpload = null)
{
var request = (FtpWebRequest)WebRequest.Create(url);
request.Method = action;
request.UseBinary = true;
request.UsePassive = true;
request.KeepAlive = true;
request.Credentials = new NetworkCredential(_settings.Username, _settings.Password);
WebResponse response = null;
try
{
if (action == WebRequestMethods.Ftp.UploadFile)
{
StreamReader sourceStream = new StreamReader(fileToUpload);
byte[] fileContents = Encoding.UTF8.GetBytes(sourceStream.ReadToEnd());
sourceStream.Close();
request.ContentLength = fileContents.Length;

Stream requestStream = request.GetRequestStream();
requestStream.Write(fileContents, 0, fileContents.Length);
requestStream.Close();
}
response = request.GetResponse();
}
catch (WebException exp)
{
var ftpResponse = (FtpWebResponse)exp.Response;
if (request.Method== WebRequestMethods.Ftp.MakeDirectory && ftpResponse.StatusCode != FtpStatusCode.ActionNotTakenFileUnavailable)
DiosException.Throw(exp);
}
finally
{
if (response != null)
response.Close();
}
}

最佳答案

它应该可以在 Azure WebApp 上运行,我在我这边测试了你的代码,它运行正常。以下是我的测试代码。在您的情况下,您可以 remote debug the WebApp获取详细的错误信息。如果我们想了解更多关于Azure WebApp的内容可以引用WebApp Sanbox .

  var fileName = Guid.NewGuid().ToString() + ".txt";
var url = $"ftp://xxxxx.ftp.azurewebsites.windows.net/site/wwwroot/{fileName}";
var action = WebRequestMethods.Ftp.UploadFile;
var request = (FtpWebRequest)WebRequest.Create(url);
var fileToUpload = @"D:\home\site\wwwroot\tom.txt"; // make sure the file is existing and file can be accessed
var userName = @"xxxxxx";
var password = "xxxxxxx";
request.Method = action;
request.UseBinary = true;
request.UsePassive = true;
request.KeepAlive = true;
request.Credentials = new NetworkCredential(userName, password);
WebResponse response = null;
try
{
if (action == WebRequestMethods.Ftp.UploadFile)
{
var sourceStream = new StreamReader(fileToUpload);
byte[] fileContents = Encoding.UTF8.GetBytes(sourceStream.ReadToEnd());
sourceStream.Close();
request.ContentLength = fileContents.Length;
var requestStream = request.GetRequestStream();
requestStream.Write(fileContents, 0, fileContents.Length);
requestStream.Close();
}
response = request.GetResponse();
}
catch (WebException exp)
{
var ftpResponse = (FtpWebResponse)exp.Response;
//if (request.Method == WebRequestMethods.Ftp.MakeDirectory && ftpResponse.StatusCode != FtpStatusCode.ActionNotTakenFileUnavailable)
// DiosException.Throw(exp);
}
finally
{
response?.Close();
}

enter image description here

关于c# - 无法从 Azure 应用程序使用 FtpWebRequest 连接到 ftp 服务器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47090610/

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