How to check if proxy is working in Java?

14,886

Ok I found a solution and it is easy.

What I used it InetAddress.isReachable() method along with some HttpClient by Apache. For proxy checking I used blanksite.com because all I need is check connectability and not speed of proxies.

So here is the code(Including input from file, but it 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();
    }
}
Share:
14,886
user1021229
Author by

user1021229

Updated on June 26, 2022

Comments

  • user1021229
    user1021229 almost 2 years

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

    My program needs to obtain proxies from a selected file(I got that done using java gui FileChooser class and RandomAccessFile)

    Then I need to verify the proxies starting with the one that is first in the txt file. It will try to connect to some site or port to verify if the connection was successful.If the connection was successful (I got a positive response) it will add the proxy to a list of proxies and then get and check next one in the list until it is done.

    I know how to do this but I got a little problem. My Problem is that this process needs to be independent of connection speed because someone may set 15000(milliseconds) timeout for the connection to be dealt with and set 100 threads and then none of the proxies would come out working because connection is too slow.

    I heard of a method called pinging to check proxies,but I do not know how to use it in java.

    Could anyone give me solution or at least classes I could use.

  • Iordanis
    Iordanis over 10 years
    client.getParams().setParameter(ConnRoutePNames.DEFAULT_PROX‌​Y, ,proxy); is missing a parameter.