I would want to use resolved IP address to connect to server instead of hostname. Here is my code snippets:
// Get domain name from URL
String domainName = url.substring("http://".length(),
url.indexOf("/", 8));
// Get IP address as string
InetAddress inet = null;
try {
inet = InetAddress.getByName(domainName);
} catch (UnknownHostException e) {
Log.i(TAG, "The IP address cannot be resolved for " + domainName);
}
resolvedIP = inet.getHostAddress();
Here i'm able to successfully get IP ADDRESS. Now i try to replace my url as below:
url = url.replace(domainName, resolvedIP);
Now I connect to server:
URL download = new URL(url);
conn = (HttpURLConnection) url.openConnection();
conn.getInputStream(); //Throws IO Exception
I'm able to successfully connect if I use URL as it is (without replacing domain name with IP Address).
Please let me know if I'm doing things correctly.