dhanya
dhanya

Reputation: 1

Problem with uploading files using FTP

I am working on windows application with c#.net . I am using FTPwebrequest class for upload file on ftp.it works fine when there is no proxy between application and internet. but it doesn't work in proxy.

FtpWebRequest reqFTP; if i use reqFTP.Proxy=new webproxy("proxyservername",21); then it will display error message "FTP command doen't support in HTTP proxy".

my code is like this

FtpWebRequest reqFTP;
reqFTP = (FtpWebRequest)FtpWebRequest.Create("ftp://uri");

reqFTP.Credentials = new NetworkCredential("username", "pwd");

reqFTP.KeepAlive = false;

reqFTP.Method = WebRequestMethods.Ftp.UploadFile;
reqFTP.UseBinary = true;
reqFTP.UsePassive = true;
int buffLength = 2048;

byte[] buff = new byte[buffLength];
int contentLen;

FileStream fs = fileInf.OpenRead();

Stream strm = reqFTP.GetRequestStream();

contentLen = fs.Read(buff, 0, buffLength);

while (contentLen != 0)
{
    strm.Write(buff, 0, contentLen);
    contentLen = fs.Read(buff, 0, buffLength);
    prbUpload.Value += contentLen;
}

strm.Close();
fs.Close();

Upvotes: 0

Views: 2145

Answers (3)

Naveenk
Naveenk

Reputation: 78

Whether it's uploadFile, DownloadFile, ListDirectory, or ListDirectoryDetails, The following code resolves all the problems.

reqFTP.Proxy = new WebProxy();

It will initializes Proxy to empty instance of the WebProxy Class which will internally skip the proxy.

Upvotes: 0

Frédéric Hamidi
Frédéric Hamidi

Reputation: 263137

From the FtpWebRequest.Proxy property documentation:

If the specified proxy is an HTTP proxy, only the DownloadFile, ListDirectory, and ListDirectoryDetails commands are supported.

So you cannot use FtpWebRequest to upload a file using FTP through an HTTP proxy. Other solutions might be available, see this question that discusses the same problem.

Upvotes: 1

Sarwar Erfan
Sarwar Erfan

Reputation: 18058

You are using HTTP proxy. May be you use this proxy to browse internet. You cannot use http proxy for ftp. To use that proxy for ftp, you need to configure the proxy server for ftp.

Bottom line is, ftp proxy is required for ftp, http proxy is required for browsing etc..

So, you are sending ftp commands through proxy, but that proxy supports only http. So, I think the error message is now more meaningful to you

"FTP command doen't support in HTTP proxy"

Upvotes: 0

Related Questions