FTP upload file The requested FTP command is not supported when using an HTTP proxy

Can someone please take a look at the code below and tell me what I'm doing wrong. I'm just going in circles, any pointers really appreciated

public class FtpWebRequestUtil { private static string RemoteHost; private static string RemoteFtpPath; public static NetworkCredential Credential = new NetworkCredential(); public FtpWebRequestUtil() { } public FtpWebRequestUtil(string RemoteAddress, string RemotePath, string RemoteUser, string RemotePwd) { Credential.UserName = RemoteUser; Credential.Password = RemotePwd; RemoteHost = RemoteAddress; RemoteFtpPath = RemotePath; } public string UploadFile(string localFilePath) { int startTime = Environment.TickCount; // Console.WriteLine("Uploading File " + localFilePath); try { FileInfo localFile = new FileInfo(localFilePath); //eg: c:\\Test.txt byte[] buf = new byte[2048]; int iWork; string remoteFile = "ftp://" + RemoteHost + "/" + RemoteFtpPath + "/" + localFile.Name; FtpWebRequest req = (FtpWebRequest) FtpWebRequest.Create(remoteFile); // req.Proxy = req.Credentials = Credential; // FtpWebRequest req = (FtpWe req.UseBinary = true; req.KeepAlive = true; req.Method = WebRequestMethods.Ftp.UploadFile; StreamWriter myStreamWriter = new StreamWriter(req.GetRequestStream()); myStreamWriter.Write(new StreamReader("TestFiles\\" + localFile.Name).ReadToEnd()); myStreamWriter.Close(); FtpWebResponse myFtpWebResponse = (FtpWebResponse) req.GetResponse(); Console.WriteLine("Upload File Complete, status: " + myFtpWebResponse.StatusDescription); myFtpWebResponse.Close(); return "SUCCESS"; } catch (Exception ex) { Console.WriteLine("There was an error connecting to the FTP Server."); Console.WriteLine(ex.Message); throw ex; } Console.WriteLine("Time taken for downloading file is " + (Environment.TickCount - startTime).ToString()); return "FAILURE"; } ************************ ********************************* FtpWebRequestUtil ftpClient = new FtpWebRequestUtil(FtpUrl, InputFolder, FtpUser, FtpPassword); try { Thread.Sleep(5000); ftpClient.UploadFile(UploadingFileName); } catch (Exception exception) { Assert.Fail(exception.Message); } finally { ftpClient = null; } } } 
+6
source share
3 answers
 req.Proxy = new WebProxy(); // initialize this FtpWebRequest property 
+8
source

It turns out that only the RETR , LIST and NLST methods are supported by System.Net.FtpWebRequest when the HTTP proxy is configured, and it does not matter that you do not set the proxy server in your code: if the proxy server is an HTTP proxy (not an FTP proxy ) is configured in the proxy settings of the system (for example: Internet Options \ Connections \ LAN Settings \ Proxy Server \ Use a proxy server for your local network), you will receive this error when trying to load the FTP server.

The workaround uses IE to change system settings to disable the use of an HTTP proxy. However, if you have access to the affected code, the solution should set the Proxy property of the request to null, for example:

 request.Proxy = null; 
+2
source

Exceptions per se - the answer - it is not supported. Perhaps you have an HTTP proxy that prevents a direct connection to FTP. According to MS documentation , if the specified proxy server is an HTTP proxy server, only the DownloadFile, ListDirectory and ListDirectoryDetails commands are supported, therefore UploadFile is not supported.

0
source

Source: https://habr.com/ru/post/956419/


All Articles