Cannot connect to ftp server with FtpWebRequest from Azure app

Viewed 1096

I can upload files into azure ftp server with local asp.net app but that not works after publishing the app into azure it gives me not logged in

Here is my code which connect to the ftp server but when deployed to azure it does not work

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();
        }
    }
1 Answers

It should work on Azure WebApp, I test your code on my side, it works correctly. The following is my test code.In your case, you could remote debug the WebApp to get the detail error info. If we want to know more things about Azure WebApp we could refer to 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

Related