Monday, December 2, 2013

Create Your Own Proxy Checker In Java

Being able to check whether a proxy is alive or not is a good advantage if you want speed you're ever wanting. So in this post I'm gonna show you how you would be able to create one in Java.

Create a project in Eclipse (I'm pretty sure you already know this, if not, please proceed to my other posts regarding to this matter) and paste this code:


import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.Proxy;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.List;
 


public class ClassicPC {
 
 public static List check(List proxies){  
  List wProxies = new ArrayList();
  for(JProxy p : proxies){
   try{
    System.out.println("Checking " + p.getHost() + ":" + p.getPort() + " ...");
    String h = p.getHost(); // "122.192.166.70"; 
    int port = Integer.parseInt(p.getPort()); 
    Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress(h, port));
    URL url = new URL("http://www.google.com");
    URLConnection http = url.openConnection(proxy);
    http.connect(); 
    System.out.println("SUCCESS!");
    wProxies.add(new JProxy(h, String.valueOf(port))); 
   }catch(Exception e){
    System.out.println("ERROR: " + e.getMessage());
   } 
  }   
  return wProxies;
 }

 public static void main(String args[]) throws IOException{
  List proxies = readList();
  List wProxies = check(proxies);
  System.out.println("WORKING PROXIES:");
  BufferedWriter writer = new BufferedWriter(
    new FileWriter(new File("C:\\wProxies.txt")));
  for(JProxy p : wProxies){
   writer.write(p.getHost() + ":" + p.getPort());
   writer.write(System.getProperty("line.separator"));
  }
  writer.close();
 }
 
 public static List readList(){
  try{
   File f = new File("C:\\tocheck.txt");
   List proxies = new ArrayList();
   BufferedReader reader = new BufferedReader(
     new FileReader(f));
   String proxy = null;
   while( (proxy = reader.readLine()) != null){ 
    //System.out.println("Proxy: " + proxy);
    proxies.add(new JProxy(proxy.split(":")[0]
                          ,proxy.split(":")[1]));
   }
   reader.close();
   return proxies;
  }catch(Exception e){
   e.printStackTrace();
  }
  return null;
 }
 
 static class JProxy{
  public JProxy(String h, String p){
   host = h;
   port = p;
  }
  private String host, port;
  public String getHost() { return host; }
  public String getPort() { return port; }
 }
}

Let's start off with the main function:
We have create proxies instance to read a list of proxies inside a text file called tocheck.txt.
Then we have create a wProxies instance to get all the working proxies when the check() method arrives.

So what's the check method has and how will it find out which proxies are alive?
What it does is that, it first loops thru the proxies we have just get from the text file and fetched to get the ip and the port. Then created a Proxy instance and a URL instance to open a connection to google.com with the proxy specified. If it successfully opens the Google.com website then the proxy is said to be alive and being populated to the wProxies. Right after all the proxies to check is finished it will then be returned back to the calling variable with only proxies alive! Finally, the program will create a new file called wProxies.txt with all the alive proxies.

If you have any questions, please comment here. Thanks for reading my posts.

No comments:

Post a Comment