Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2102
  • Last Modified:

How to upload a file with ftp protocol through a HTTP PROXY

Hello,

How can I upload a file with my below code through a http proxy ? My code doesnt work...

public static void UploadFile(FtpWebRequest req, string localFilePath, Int32 bufferSize)
        {
 
            if (bufferSize < 1) bufferSize = defaultBufferSize;
 
            FileStream tempStream = new FileStream(localFilePath, FileMode.Open);
              
 
            byte[] buffer = new byte[bufferSize];
            Int64 noOfBuffers = tempStream.Length / Convert.ToInt64(bufferSize);//rundet automatisch ab
            Int32 lastBufferSize = Convert.ToInt32(tempStream.Length - noOfBuffers * bufferSize);
 
            req.UsePassive = true;
            var webProxy = new WebProxy("http://192.168.0.111", 8080);
            req.Proxy = webProxy;
 
            req.Method = WebRequestMethods.Ftp.UploadFile;
            
            Stream ftpStream = req.GetRequestStream();
 
...
...
}

Open in new window

0
MSFanboy
Asked:
MSFanboy
  • 7
  • 7
  • 2
1 Solution
 
tcs224694Commented:
Hello MSFanboy,

Does it throw any error...

Thanks,
TCS
0
 
MSFanboyAuthor Commented:
the requested ftp command is not supported when using http proxy
0
 
abelCommented:
Your code is correct, but you are trying to use an HTTP proxy with FTP. That will not work, afaik. You can try an FTP proxy, which could work, but I don't know of a .net FTP proxy.

If you use an FTP client like FileZilla, can you access the FTP using the proxy?
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
tcs224694Commented:
Here u can get the filezilla ...

http://filezilla-project.org/

Thanks
TCS
0
 
MSFanboyAuthor Commented:
I tried filezilla before and that works of course, because firefox has the http proxy already set and filezilla is using this setting probably so the upload from filezilla to another company through http proxy works, but I want to reach that with code as I calculate sth and want to have uploaded it after its calculated!
0
 
abelCommented:
> and filezilla is using this setting probably

no, it is not. FileZilla and MoZilla are not related, although there name might imply this. If you use the ftp command on the commandline, can you open a connection to the ftp site? In other words, are you sure you need a proxy here? Ftp proxies are very rare, because there's not so much to proxy or cache.
0
 
MSFanboyAuthor Commented:
again in our LAN we have a proxy / firewall. This proxy url is entered in mozilla firefox networksettings in the http field.

so why could I upload a file with filezilla to the other companies ftp if filezilla doesnt consider the http proxy settings in firefox?

I just tried to connect on the command line and that worked , I could list everything in the dir ??

Why does my code bring that exception see image!

ftpproxyerror.JPG
0
 
abelCommented:
> so why could I upload a file with filezilla to the other companies ftp if filezilla doesnt consider the http proxy settings in firefox?

and how would filezilla know that, even if firefox is not installed? Filezilla is monogamous. But most proxies have an auto discover feature, which is probably why FileZilla finds it.  And why would filezilla want to lookup the settings for an http proxy in the first place? It uses ftp...

> I just tried to connect on the command line and that worked , I could list everything in the dir ??
this means that both our firewall AND your proxy are just keeping port 21 open. In other words, you should not try to use the proxy, it will not work. And FileZilla is correct in that it does not use a proxy either. That Firefox uses a proxy, is for HTTP, which is a whole different story.

So:
Remove the proxy part. After that we will have a look at any remaining errors.
0
 
abelCommented:
> both our firewall AND your proxy

must read, of course, as: both your firewall AND your proxy
0
 
MSFanboyAuthor Commented:
funny now without using the proxy crap I get sth like:

. Requested action not taken. File name not allowed. .. error code 553

I upload the exactly same file I uploaded with filezilla manually. Why is the uploading of the same file not allowed with c# ??
0
 
abelCommented:
> . Requested action not taken. File name not allowed. .. error code 553

that's good news! That's an answer from the server, which is a good thing. That means you and the FTP server can talk...

> Why is the uploading of the same file not allowed with c# ??

it is, you're looking in the wrong direction here and blaming the wrong guy (C#). You need to give the file a name, currently (at least in the code above) you do not specify a name... Can you show the whole code so that I can help you further?
0
 
MSFanboyAuthor Commented:
of course... see code attached



private void ftpButton_Click(object sender, EventArgs e)
        {
            StartUpload(_ftp, _username, _password, LoadSettings() + "\\" + GetFileNameCsv());
        }
 
public static void StartUpload(String ftp, String user, String password, String filepath)
        {
            var req = (FtpWebRequest) WebRequest.Create("ftp://" + ftp);
            req.Credentials = new NetworkCredential(user, password);
            FtpHelper.UploadFile(req, filepath, -1);
        }
 
private static String LoadSettings()
        {
            XElement doc = XElement.Load(@"..\..\Data\settings.xml");
            XElement result = (from ele in doc.Descendants("SourceDirectory")
                               select ele).SingleOrDefault();
            //dirLabel.Text = result.Value;
            return result.Value;
        }
 
public String GetFileNameCsv()
        {
            string dateFinal = data.YearCsv + data.Month;  
            string fileNameFinal = Regex.Replace(csvFileName, @"JJMM", dateFinal);
            return fileNameFinal;
        }

Open in new window

0
 
abelCommented:
this line:

StartUpload(_ftp, _username, _password, LoadSettings() + "\\" + GetFileNameCsv());


will create something that includes a backslash. Which is not allowed on FTP servers (it doesn't matter if the underlying system of the FTP server is windows). I don't know what LoadSettings will return, nor do I know what GetFileNameCsv returns, but from the look of it, it will not be a FTP-valid directory.

To give a hint: you need something like: "/rootdir/subdir/filename.csv"

If all is correct (the path must exists! not the filename), but not the forward slashes, try it with:

FtpHelper.UploadFile(req, filepath.Replace("\\", "/"), -1);

-- Abel --
0
 
MSFanboyAuthor Commented:
your filepath.Replace did not work still same error message I get :(
do I have to create something like this?
ftp://username:password@IP


0
 
MSFanboyAuthor Commented:
ok the problem was rather I had to state my filename also to this method:

WebRequest.Create("ftp://" + ftp+"/"+"mycsv.csv"); etc...
0
 
abelCommented:
Missed the follow-up mail, apologies. Glad you found that the root directory was apparently incorrect. I tried to explain in my story that the path must be correct and must be existing. Apparently it wasn't. The replace was only meant for removing the backslashes and replacing them with forward slashes. Only you could know the real path...
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

  • 7
  • 7
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now