I've wrote a code that open Tor. I also have a list of exit nodes that automatically update the torrc file. Sometime, the ip is not working but tor is still loading trying to connect with it. Is there any way to check or have a return if the IP is not working or too long to load a page ? (P.S: sorry for my english, I'm french)
Thank you for your help.
public void openTor1()
{
}
You can use this code to check if a host is reachable:
import java.net.InetAddress;
private boolean isHostReachable(String host)
{
try
{
return InetAddress.getByName(host).isReachable(500);
}
catch (IOException e)
{
System.out.err("Error while checking if host is reachable: " + host + ", error is: " + e.getMessage());
return false;
}
}
If needed, you can check connection speed to that host. You'll need some HttpClient / Connection object for that. Idea is to send Http request, wait for response and measure elapsed time. Pseudocode to do it:
long startTime = System.currentTimeMillis();
//Write this above before your httprequest
HttpResponse httpResponse = httpClient.execute(httpGet);
HttpEntity httpEntity = httpResponse.getEntity();
is = httpEntity.getContent();
//After you get response
long elapsedTime = System.currentTimeMillis() - startTime;
System.out.println("Total elapsed http request/response time in milliseconds: " + elapsedTime);
Basic tutorial for Http messaging: Using java.net.URLConnection to fire and handle HTTP requests