I try to connect to an FTP-Server (Which is accessed over the Internet). My Company-Network is behind a Proxy, so I have to go through that somehow.
I've tried lots of things:
What I found out is, that the FtpWebRequest-Class doesn't support FTP-Upload behind Proxy (I guess to solve that I'll try the TCP-Client, but first things first) but it should support a Direcotry-List etc.
Nothing seems to work. According to my Network-Administrator, the Proxy-address is httpname.gr.ch with port 8080, but after I checked out the FileZilla-Settings there is another Proxy: ftpname.gr.ch:2121 so I assume that this one is the one I haven't get something to run properly.
I was only able to get a WebRequest with Proxy to run without an exception (no FTP). Here is the code-snippet that worked with the http-proxy:
WebProxy a = new WebProxy("httpname.gr.ch:8080", true);
a.Credentials = CredentialCache.DefaultNetworkCredentials;
WebRequest b = WebRequest.Create("https://www.heise.de/ct/hotline/Proxy-mit-Name-und-Passwort-316900.html");
b.Proxy = a;
WebResponse c = b.GetResponse();
StreamReader d = new StreamReader(c.GetResponseStream());
d.ReadToEnd();
However after I was able to get out of our network, I tried to read the FTP-Directory (which should work with Proxy). Here is my code-snipped to Check the Connection:
public void CheckConnection()
{
try
{
WebProxy a = new WebProxy("ftpname.gr.ch:2121", true);
a.Credentials = CredentialCache.DefaultNetworkCredentials;
FtpWebRequest.DefaultWebProxy = null;
FtpWebRequest ftpWebRequest = (FtpWebRequest)FtpWebRequest.Create(new Uri("ftp://" + this.Adress));
ftpWebRequest.Credentials = new NetworkCredential(this.User, this.Password);
ftpWebRequest.UsePassive = true;
ftpWebRequest.Proxy = a;
//Als Methode muss ListDirectory gewählt werden!
ftpWebRequest.Method = WebRequestMethods.Ftp.ListDirectory;
WebResponse webResponse = ftpWebRequest.GetResponse();
}
catch (WebException ex)
{
var ftpResponse = (FtpWebResponse)ex.Response;
}
}
When I try to run that, I get this Error: "The server has run a protocol violation.. Section=ResponseStatusLine".
If I try without Proxy (ftpWebRequest.Proxy = null;), I get this Error: "The Underlying Connection Was Closed. Could Not Establish Trust Relationship with Remote Server.".
If I try with the http-Proxy, this is the Error: "The remote server returned an error: (400) Bad Request".
Everything I tried was stopped at "ftpWebRequest.GetResponse();".
Does anyone have an idea or a hint what or where the problem is? Am I totally wrong? Please help me. Thanks.
EDIT:
Here is the Filezilla Log (German):
2018-07-24 13:34:35 580 1 Status: Verbinde mit host über FTP-Proxy
2018-07-24 13:34:35 580 1 Status: Auflösen der IP-Adresse für ftpname.gr.ch
2018-07-24 13:34:35 580 1 Status: Verbinde mit ip:port...
2018-07-24 13:34:35 580 1 Status: Verbindung hergestellt, warte auf Willkommensnachricht...
2018-07-24 13:34:35 580 1 Antwort: 220 Frox transparent ftp proxy. Login with username[@host[:port]]
2018-07-24 13:34:35 580 1 Befehl: AUTH TLS
2018-07-24 13:34:35 580 1 Antwort: 530 Please login with USER first
2018-07-24 13:34:35 580 1 Befehl: AUTH SSL
2018-07-24 13:34:35 580 1 Antwort: 530 Please login with USER first
2018-07-24 13:34:35 580 1 Status: Unsicherer Server; er unterstützt kein FTP über TLS.
2018-07-24 13:34:35 580 1 Befehl: USER user@host@ftp
2018-07-24 13:34:35 580 1 Antwort: 331 User user@host OK. Password required
2018-07-24 13:34:35 580 1 Befehl: PASS ************
2018-07-24 13:34:35 580 1 Antwort: 230 OK. Current restricted directory is /
2018-07-24 13:34:35 580 1 Befehl: SYST
2018-07-24 13:34:35 580 1 Antwort: 215 UNIX Type: L8
2018-07-24 13:34:35 580 1 Befehl: FEAT
2018-07-24 13:34:35 580 1 Antwort: 502 Command not implemented.
2018-07-24 13:34:35 580 1 Status: Der Server unterstützt keine Nicht-ASCII-Zeichen.
2018-07-24 13:34:35 580 1 Status: Angemeldet
2018-07-24 13:34:35 580 1 Status: Empfange Verzeichnisinhalt...
2018-07-24 13:34:35 580 1 Befehl: PWD
2018-07-24 13:34:35 580 1 Antwort: 257 "/" is your current location
2018-07-24 13:34:35 580 1 Befehl: TYPE I
2018-07-24 13:34:36 580 1 Antwort: 200 TYPE is now 8-bit binary
2018-07-24 13:34:36 580 1 Befehl: PASV
2018-07-24 13:34:36 580 1 Antwort: 227 Entering Passive Mode (193,247,16,24,103,35)
2018-07-24 13:34:36 580 1 Befehl: LIST
2018-07-24 13:34:36 580 1 Antwort: 150 Accepted data connection
2018-07-24 13:34:36 580 1 Antwort: 226-Options: -a -l
2018-07-24 13:34:36 580 1 Antwort: 226 48 matches total
2018-07-24 13:34:36 580 1 Status: Anzeigen des Verzeichnisinhalts für "/" abgeschlossen
Here is the Program-Log (With ftpname.gr.ch as Proxy, like Filezilla):
System.Net Information: 0 : [7264] FtpWebRequest#7746814::.ctor(ftp://host/)
System.Net Information: 0 : [7264] Der aktuelle Betriebssysteminstallationstyp ist "Client".
System.Net Information: 0 : [7264] FtpWebRequest#7746814::GetResponse(Methode = NLST.)
System.Net Information: 0 : [7264] Associating HttpWebRequest#54246671 with ServicePoint#63840421
System.Net Information: 0 : [7264] Associating FtpWebRequest#7746814 with HttpWebRequest#54246671
System.Net Information: 0 : [7264] Associating Connection#59408853 with HttpWebRequest#54246671
System.Net Information: 0 : [7264] Connection#59408853 - Die Verbindung von hostip:port mit targetip:port wurde hergestellt.
System.Net Information: 0 : [7264] Associating HttpWebRequest#54246671 with ConnectStream#56152722
System.Net Information: 0 : [7264] HttpWebRequest#54246671 - Request: GET ftp://username/ HTTP/1.1
System.Net Information: 0 : [7264] ConnectStream#56152722 - Header
{
Host: host
Proxy-Connection: Keep-Alive
} werden gesendet.
System.Net Error: 0 : [7264] Exception in HttpWebRequest#54246671:: - Der Server hat eine Protokollverletzung ausgeführt.. Section=ResponseStatusLine.
System.Net Error: 0 : [7264] Exception in HttpWebRequest#54246671::GetResponse - Der Server hat eine Protokollverletzung ausgeführt.. Section=ResponseStatusLine.
System.Net Error: 0 : [7264] Exception in FtpWebRequest#7746814::GetResponse - Der Server hat eine Protokollverletzung ausgeführt.. Section=ResponseStatusLine.
bei System.Net.FtpWebRequest.GetResponse()
Solution
I tried many possibilities (thanks to Martin Prikryl for his help) and finally I made it trough the proxy to the FTP-Server :)
The trick is to have an FTP-Proxy (what I do have, as it's written in my question). You can check if it works with the command line and ftp.exe (Windows):
ftp.exe
open proxyadress port <- if it is not an FTP-Proxy, nothing happens <- crtl+c
ftpusername@host <- Because special domain and stuff I had to do: ftpusername@host@webhost
ftppassword <- It will not be displayed
230 OK. <- Success!
After I was able to access the FTP-Server behind a Proxy from my command line I could try the FtpWebRequest-Class to do the same and it worked:
FtpWebRequest request = (FtpWebRequest)WebRequest.Create("ftp://ftpproxy:proxyport");
request.Credentials = new NetworkCredential("ftpuser@ftphost@webhost", "ftppassword");
request.Method = WebRequestMethods.Ftp.MakeDirectory;
request.Proxy = null;
request.EnableSsl = false;
request.UsePassive = true;
FtpWebResponse response = (FtpWebResponse)request.GetResponse();
Console.WriteLine("The content length is {0}", response.ContentLength);