Hello I have connected to the FTP using URLConnection
with just the IP address with not specifing the login credentials.
URLConnection conn = new URLConnection(new URL("ftp://222.101.102.1/etc"));
It works, with no credentials because the server probably allows annonymous connection.
But for practice I wanted to make an ftp connection from scratch with Socket
client, and I created a new socket connection to that IP:
Socket socket = new Socket("222.101.102.1", 21);
if (socket != null) {
BufferedReader reader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
String version = reader.readLine();
System.out.println(version);
}
This prints 220 vsFTPd (2.0.5)
but when I try to do command like LS \r\n
I get 530 Please login with USER and PASS
Why is it asking me for login if with URLConnection
it did not?
This code was not compiled because it was memorized from my other computer so let me know if you notice something wrong and I'll edit right away.
I managed to figure it out.
URL.openConnection()
basically returns a class that implements URLStreamHandler
based on the protocol. So in this case the protocol is ftp
and it returns FtpURLConnection
.
So there is a check that if the username is null (not specified in the URL), and if it is so it sets it to anonymous and uses a password of the java version with @
e.g Java1.8.0@
not sure for what purpose, maybe for login history. But you can use any password to login with anonymous unless it's configured for something else I think.
if(this.user == null) {
this.user = "anonymous";
String var11 = (String)AccessController.doPrivileged(new GetPropertyAction("java.version"));
this.password = (String)AccessController.doPrivileged(new GetPropertyAction("ftp.protocol.user", "Java" + var11 + "@"));
}
So I basically logged in with Anonymous
and no password.
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments