Connecting to FTP server in Android - java

Im new to android programming. Im trying to build a basic app whice tries to connect to an ftp server. I have no problems connecting to the server in other ways. I hope you guys could tell me what I'm doing wrong.
This is my code:
public boolean ftpConnect(String host, String username,
String password, int port) {
try {
client = new FTPClient();
client.connect(InetAddress.getByName(host), 21);
if (FTPReply.isPositiveCompletion(client.getReplyCode())) {
boolean status = client.login(username, password);
client.setFileType(FTP.BINARY_FILE_TYPE);
client.enterLocalPassiveMode();
return status;
}
} catch (Exception e) {
Log.d(TAG, "Error: could not connect to host " + host);
}
return false;
}
the function is called from my main activity OnCreate() method.
When I run I just recieve the "could not connect to host" tag.
Thanks in advance!

Related

FtpClient.login() returns false to existing user

I'm trying to connect into FTP server, the problem is that wehn I try to log into the server with specific user it returns false.
This is the link for the ftp website: https://url.publishedprices.co.il/
The user name is: DorAlon
The password should be empty
When I'm connecting through the website it's working, it works too when i'm connecting from FileZilla, but when I do so from Java it returns false for this user. There are several more users that logged in successfully to this server, and only this user doesn't work.
Here's my code:
String server = "url.retail.publishedprices.co.il";
int port = 21;
String pass = " ";
FtpClient ftpClient = new FTPClient();
try {
ftpClient.connect(server, port);
boolean a = ftpClient.login("xxx", "");
boolean b = ftpClient.login("yyy", "");
} catch (IOException ex) {
System.out.println("Error: " + ex.getMessage());
ex.printStackTrace();
} finally {
try {
// Checking whether the client is connected - in that case we'll logout and disconnect from the server
if (ftpClient.isConnected()) {
ftpClient.logout();
ftpClient.disconnect();
}
} catch (IOException ex) {
ex.printStackTrace();
}
}
In the end the boolean a holds false, and b holds true.
It's mean the connection with the server is okay and also the login, but for the specific user it doesn't work.
How could this be possible?
I would like for some help, Thanks.

Verify connection via AS400

My method must trying to connect to some other DB server. When I run my application on Linux Server - there was all OK. When I run it on Windows OS - I have java.net.SocketTimeoutException: Read timed out
private boolean pingServer(String host) {
String username = "username";
String password = "password";
boolean successful;
AS400 as400 = new AS400(host, username, password);
SocketProperties socketProperties = as400.getSocketProperties();
socketProperties.setLoginTimeout(TIMEOUT_MILLISEC);
socketProperties.setSoTimeout(TIMEOUT_MILLISEC);
as400.setSocketProperties(socketProperties);
try{
successful = as400.validateSignon();
} catch (AS400SecurityException e) {
successful = true;
} catch (IOException e) {
successful = false;
} catch (TransactionException e) {
successful = false;
}
as400.disconnectAllServices();
as400 = null;
return successful;
}
Can anybody give me some advice?
Thanks.
AS/400 connections use a lot of network ports which must be available to you.
Try disabling all firewalls between you and the server and try again. This include the software firewall (may be part of the antivirus suite) on the Windows system

Cannot connect using hostname from an Android device

I am trying to connect from my android device to a linux PC using TCP protocol.
Both devices are on the same network.
When I use a simple java code like this on my other PC on the same network, it works and I get an output
class Server {
public static void main ( String[] args ) throws IOException
{
String hostname = "MY_COMPUTER_NAME";
try
{
InetAddress ipaddress = InetAddress.getByName(hostname);
System.out.println("IP address: " + ipaddress.getHostAddress());
}
catch ( UnknownHostException e )
{
System.out.println("Could not find IP address for: " + hostname);
}
}
}
Output:
IP address: 192.168.1.3
When I use this code to connect to a socket on my android phone, it just doesn't work. The handler also updates another TextView for debugging. When I replace MY_COMPUTER_NAME with the actual IP that I can see on my router settings, everything works, the socket is created and the TextView is updated.
//Some variables
String hostname = "MY_COMPUTER_NAME";
private static final int SERVERPORT = 5001;
InetAddress ipaddress = null;
String address = null;
#Override
public void run() {
while(true){
try {
ipaddress = InetAddress.getByName(hostname);
address = ipaddress.getHostAddress();
socket = new Socket(ipaddress, SERVERPORT);
myHandler.post(updateRunnable);
break;
} catch (UnknownHostException e1) {
e1.printStackTrace();
} catch (IOException e1) {
e1.printStackTrace();
}
}
}
What could be the problem, and what am I missing? Thanks.
I think Android has problems with NetBIOS names, but can lookup names that are registered at the DNS Server. see here: Host is unresolved in LAN

FTPS client behind a proxy server using apache commons net

Our network team only allow us to connect to our third party client thru proxy server.
Is there a way to add a proxy server to FTPS client of apache commons net?
If it is not possible, can you tell a way on how to do it.
By the way here's the code that is working outside of the company network
String server = "ftp.xxxx.com";
String username = "username";
String password = "password";
String remoteFile = "xmlSR.xml";
String localFile = "c:/downloadedfile.xml";
String protocol = "TLS"; // TLS / SSL
int timeoutInMillis = 3000;
boolean isImpicit = false;
FTPSClient client = new FTPSClient(protocol, isImpicit);
client.enterLocalActiveMode();
client.setRemoteVerificationEnabled(false);
client.setActivePortRange(50000, 50200);
client.setDataTimeout(timeoutInMillis);
client.addProtocolCommandListener(new PrintCommandListener(
new PrintWriter(System.out)));
client.setTrustManager(TrustManagerUtils.getAcceptAllTrustManager());
try {
int reply;
client.connect(server);
client.login(username, password);
client.setFileType(FTP.BINARY_FILE_TYPE);
client.execPBSZ(0);
client.execPROT("P");
System.out.println("Connected to " + server + ".");
reply = client.getReplyCode();
if (!FTPReply.isPositiveCompletion(reply)) {
client.disconnect();
System.err.println("FTP server refused connection.");
System.exit(1);
}
client.listFiles();
boolean retrieved = client.retrieveFile(remoteFile,
new FileOutputStream(localFile));
} catch (Exception e) {
if (client.isConnected()) {
try {
client.disconnect();
} catch (IOException ex) {
ex.printStackTrace();
}
}
System.err.println("Could not connect to server.");
e.printStackTrace();
return;
} finally {
System.out.println("# client disconnected");
client.disconnect();
}
even we tried to add some system property for proxyHost and proxyPort
System.setProperty("http.proxyPort", "80");
System.setProperty("http.proxyHost", "yyyy.com");
System.setProperty("ftp.proxyPort", "80");
System.setProperty("ftp.proxyHost", "yyyy.com");
System.setProperty("socksProxyPort", "80");
System.setProperty("socksProxyHost", "yyyy.com");
error message
Could not connect to server.
java.net.UnknownHostException: ftp.xxxx.com
at java.net.Inet4AddressImpl.lookupAllHostAddr(Native Method)
at java.net.InetAddress$1.lookupAllHostAddr(InetAddress.java:849)
at java.net.InetAddress.getAddressFromNameService(InetAddress.java:1202)
at java.net.InetAddress.getAllByName0(InetAddress.java:1153)
at java.net.InetAddress.getAllByName(InetAddress.java:1083)
at java.net.InetAddress.getAllByName(InetAddress.java:1019)
at java.net.InetAddress.getByName(InetAddress.java:969)
at org.apache.commons.net.SocketClient.connect(SocketClient.java:192)# client disconnected
at org.apache.commons.net.SocketClient.connect(SocketClient.java:285)
at com.ti.itg.peit.bom.TestingApache.main(TestingApache.java:44)
Thank you very much.
Gerald
Its possible..see the link below....
http://grepcode.com/file/repository.grepcode.com/java/root/jdk/openjdk/6-b14/java/net/ProxySelector.java
Well using Apache's common lib to access FTP using proxy server, please set the RemoteVerificationEnabled to false after creation of FTPClient object.
eg:
FTPClient fc = new FTPClient();
fc.setRemoteVerificationEnabled(false);
You can use java.net.Proxy class which is for Java 1.5 and above, this
is used to set or unset the Proxy per connection basis
By using the java.net.ProxySelector, will determine a Proxy for each Connection.

Android How to connect to FTP?

i am trying to connect to a server through a apache FTP:
public boolean ftpConnect(String host, String user, String pass){
try {
ftpClient = new FTPClient();
ftpClient.connect(host);
if(FTPReply.isPositiveCompletion(ftpClient.getReplyCode())){
boolean status = ftpClient.login(user, pass);
ftpClient.enterLocalPassiveMode();
return status;
}
} catch (SocketException e) {
Log.d("FTP", "Error: could not connect to socket " + host );
} catch (IOException e) {
Log.d("FTP", "Error: could not connect to host " + host );
}
return false;
}
If i am connected to internet through WI-FI, the above code is working, but if i am connected through 3G is not working. I've already added the permission for Internet on the
manifest. I haven't found an explanation for this on google.
try {
FTPClient ftpClient = new FTPClient();
ftpClient.connect(InetAddress.getByName(Your host Url));
ftpClient.login(loginName, password);
System.out.println("status :: " + ftpClient.getStatus());
} catch (Exception e) {
e.printStackTrace();
}
Also, see my answer here: Track FTP upload data in android?.

Categories

Resources