10

Can someone please take a look at the code below and tell me what I am doing wrong. I am just going in circles,,, any pointers greatly 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); //e.g.: 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;
        }
    }
}
Goran
  • 6,328
  • 6
  • 41
  • 86
vishal
  • 640
  • 1
  • 5
  • 21

3 Answers3

14
req.Proxy = new WebProxy(); // initialize this FtpWebRequest property
Sushanth --
  • 55,259
  • 9
  • 66
  • 105
HappyLee
  • 435
  • 4
  • 11
  • ok tried it,, now getting object reference not set to an instance ): – vishal Oct 21 '13 at 16:05
  • where did you add this line of code? post the code with the changes – HappyLee Oct 21 '13 at 16:07
  • req.Proxy = new WebProxy(); req.Proxy = null; req.Credentials = Credential; // FtpWebRequest req = (FtpWe req.UseBinary = true; req.UsePassive = true; req.KeepAlive = true; StreamWriter myStreamWriter = new StreamWriter(req.GetRequestStream()); – vishal Oct 21 '13 at 16:19
  • its not letting to post the full code because of character limitation – vishal Oct 21 '13 at 16:20
  • you could have edited the code in your post itself. Anyways - remove req.Proxy = null; and where do you get this object ref error? you must have not initialized an object before you used it – HappyLee Oct 21 '13 at 16:35
  • StreamWriter myStreamWriter = newStreamWriter(req.GetRequestStream()); – vishal Oct 21 '13 at 16:41
  • then definitely there is an error with 'remoteFile' . check if its right, – HappyLee Oct 21 '13 at 17:09
  • let me chk,, when u say error with remote? what are you basically referring to .. I checked the remote file it looks to be alrite – vishal Oct 21 '13 at 17:19
  • `req.Proxy = new WebProxy(); req.Proxy = null; ` what's the point of 2nd statement? – Lei Yang Nov 27 '19 at 08:14
8

It turns out that only the RETR, LIST, and NLST methods are supported by System.Net.FtpWebRequest when a HTTP proxy is configured and it doesn't matter that you are not setting a proxy in your code: if a HTTP proxy (not FTP proxy) is configured in the system proxy settings (in i.e. : Internet Options\Connections\LAN setting\Proxy Server\ Use a proxy server for your LAN), then you will get this error when trying to upload to the FTP server.

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

request.Proxy = null;
Irshad
  • 3,071
  • 5
  • 30
  • 51
AliKarimi
  • 1,849
  • 1
  • 13
  • 8
0

The exceptions itself is the answer - it is not supported. Probably you have some HTTP proxy that is preventing direct connection to FTP. According to MS documentation, if the specified proxy is an HTTP proxy, only the DownloadFile, ListDirectory, and ListDirectoryDetails commands are supported - so UploadFile is not.

Goran
  • 6,328
  • 6
  • 41
  • 86