3

There is a specific server IP address that requires authentication I usually access through cmd line "ftp x.y.z.0" enter my u/n, enter my p/w and I'm in. I want to have my program ask for user credentials, check if they can log in to this server/IP address and simply return true if they can, false if not. I'm don't own this server or know really much about it, I just need to know if the end user of my application has access to it or not. I don't want to use any external packages, only what is available in the standard java library if at all possible. Any help with this is appreciated.

My most recent attempt:

 URL url = new URL("ftp://1.0.0.0/");
 URLConnection uc = url.openConnection();
 String userpass = username + ":" + password;
 String basicAuth = "Basic " + javax.xml.bind.DatatypeConverter.printBase64Binary(userpass.getBytes());
 uc.setRequestProperty ("Authorization", basicAuth);
 InputStream in = uc.getInputStream();

Not sure exactly what it's doing but it seems to give same response no matter what I put in for the username and password strings.

Jonathan Hall
  • 75,165
  • 16
  • 143
  • 189
sgr123
  • 45
  • 5
  • 1
    I know you mentioned you don't want to use external packages, but here's a similar question that uses an Apache commons-net library, might be useful: http://stackoverflow.com/questions/13836989/properly-check-ftp-server-connection – nerdherd Feb 20 '13 at 22:46
  • 1
    This is the way to implement HTTP basic auth, but you're trying to connect to an FTP server. – entonio Feb 20 '13 at 22:47

1 Answers1

1

For checking login via trying to connect to FTP server you will need to use sockets and do lot of parsing of FTP protocol and authentication stuff. So I'd suggest to use Commons Net, which also would allow you to deal with other authentication stuff in over HTTP/NNTP/etc.

jdevelop
  • 12,176
  • 10
  • 56
  • 112
  • If the @Op still wants to do it by hand, then he can refer to the implementation of the [FTPClient](http://commons.apache.org/net/api-3.2/org/apache/commons/net/ftp/FTPClient.html) – prav Feb 20 '13 at 22:54
  • Yep, he can borrow some code, but I guess it's easier to re-use something existing. – jdevelop Feb 20 '13 at 22:56
  • Every search I do on this matter seems its easiest using apache commons net package. Looks like I'm going to have to use it since it is much simpler to just check log in ability. I don't have anything to actually DO on the server once connected, it is simply a check. Thanks for your help, it seems that it'd be too cumbersome without an external package and somewhat a waste of time. – sgr123 Feb 21 '13 at 13:52