2

I'm trying to connect to an FTP server through a proxy using org.apache.commons.net.ftp.FTPClient. Pretty sure the system properties are getting set correctly as per following:

Properties props = System.getProperties();
props.put("ftp.proxySet", "true");
// dummy details
props.put("ftp.proxyHost", "proxy.example.server");
props.put("ftp.proxyPort", "8080");

Creating a connection raises a UnknownHostException which I'm pretty sure means the connection isn't making it past the proxy.

How can user credentials be passed through to the proxy with this connection type.

BTW, I can successfully create a URLConnection through the same proxy using the following; is there an equivalent for the Apache FTPClient?

conn = url.openConnection();
String password = "username:password";
String encodedPassword = new String(Base64.encodeBase64(password.getBytes()));
conn.setRequestProperty("Proxy-Authorization", encodedPassword);
skaffman
  • 398,947
  • 96
  • 818
  • 769
ceej23
  • 119
  • 1
  • 2
  • 9
  • what I checked from oracle java docs, the ftp proxy host named: `ftp.proxHost`. not `ftp.proxyHost`. there is no `y`. I don't know why and I can't test it since I have not ftp proxy. the [java networking and proxies](https://docs.oracle.com/javase/8/docs/technotes/guides/net/proxies.html) – cinqS Aug 09 '16 at 16:09

2 Answers2

2

How about using the FTPHTTPClient when you want to use a proxy;

if(proxyHost !=null) {
  System.out.println("Using HTTP proxy server: " + proxyHost);
  ftp = new FTPHTTPClient(proxyHost, proxyPort, proxyUser, proxyPassword);
}
else {
  ftp = new FTPClient();
} 
Gabriel
  • 21
  • 2
0

I think you need to use an Authenticator:

private static class MyAuthenticator extends Authenticator {
    private String username;
    private String password;
    public MyAuthenticator(String username, String password) {
        super();
        this.username = username;
        this.password = password;
    }
    @Override
    protected PasswordAuthentication getPasswordAuthentication() {
        return new PasswordAuthentication(username, password.toCharArray());
    }
}

public static void main(String[] args) {
    Authenticator.setDefault(new MyAuthenticator("foo", "bar"));
    System.setProperty("...", "...");
}
MarcoS
  • 13,386
  • 7
  • 42
  • 63
  • 1
    Thanks heaps for this. Tried this approach and received the exact same exception/s. Decided to try it.sauronsoftware.ftp4j.FTPClient and am able to successfully connect using their HTTPTunnelConnector. – ceej23 Jul 08 '11 at 03:23
  • ah ... that's because your proxy is probably not an FTP proxy, but an HTTP proxy tunneling FTP over HTTP. Good that you found a solution! – MarcoS Jul 08 '11 at 06:40