How to check if proxy server works in Java?

I searched google, this site and JavaRanch and I can not find the answer.

My program should get proxies from the selected file (I got this using the Java Classic Choos class and RandomAccessFile)

Then I need to check the proxy, starting with the first one, which is in the txt file. He will try to connect to some site or port to check if the connection was successful. If the connection was successful (I received a positive response), it will add the proxy server to the proxy list, and then it will receive and check the next one on the list until this is done.

I know how to do this, but I have a little problem. My problem is that this process should be independent of the connection speed, because someone can set a timeout of 15000 (milliseconds) for the connection to be processed, and set 100 threads, and then none of the proxies will exit out of work because the connection is too slow.

I heard about the pinging method for checking proxies, but I don't know how to use it in java.

Can someone give me a solution or at least classes that I could use.

+7
source share
1 answer

Ok, I found a solution, and it's easy.

That I used the InetAddress.isReachable() method along with some Apache HttpClient . I used blanksite.com to check the proxy, because all I need to do is check connectivity, not proxy speed.

So, here is the code (including input from a file, but this is not gui, YET):

 /* compile with java -cp .;httpclient-4.5.1.jar;httpcore-4.4.3.jar ProxyMat run with java -cp .;httpclient-4.5.1.jar;httpcore-4.4.3.jar;commons-logging-1.2.jar ProxyMat put one proxy to check per line in the proxies.txt file in the form some.host.com:8080 */ import java.io.File; import java.io.FileNotFoundException; import java.io.RandomAccessFile; import java.net.InetAddress; import org.apache.http.params.CoreConnectionPNames; import org.apache.http.HttpEntity; import org.apache.http.HttpHost; import org.apache.http.HttpResponse; import org.apache.http.client.HttpClient; import org.apache.http.client.methods.HttpGet; import org.apache.http.conn.params.ConnRoutePNames; import org.apache.http.impl.client.DefaultHttpClient; public class ProxyMat{ File file=null; static RandomAccessFile read=null; public ProxyMat(){ file=new File("proxies.txt"); try { read=new RandomAccessFile(file,"rw"); } catch (FileNotFoundException e) { e.printStackTrace(); } } public void checkproxies(){ try{ String line; for(int i=0;i<25;i++){ if((line=read.readLine())!=null){ System.out.println(line); String[] hp=line.split(":"); InetAddress addr=InetAddress.getByName(hp[0]); if(addr.isReachable(5000)){ System.out.println("reached"); ensocketize(hp[0],Integer.parseInt(hp[1])); } } } }catch(Exception ex){ex.printStackTrace();} } public void ensocketize(String host,int port){ try{ File pros=new File("working.txt"); HttpClient client=new DefaultHttpClient(); HttpGet get=new HttpGet("http://blanksite.com/"); HttpHost proxy=new HttpHost(host,port); client.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY, proxy); client.getParams().setParameter(CoreConnectionPNames.SO_TIMEOUT, 15000); HttpResponse response=client.execute(get); HttpEntity enti=response.getEntity(); if(response!=null){ System.out.println(response.getStatusLine()); System.out.println(response.toString()); System.out.println(host+":"+port+" @@ working"); } }catch(Exception ex){System.out.println("Proxy failed");} } public static void main(String[] args){ ProxyMat mat=new ProxyMat(); mat.checkproxies(); } } 
+9
source