All pastes #370694 Raw Edit

Unnamed

public java v1 · immutable
#370694 ·published 2007-02-24 20:19 UTC
rendered paste body
import java.io.*;import java.net.*;import java.util.LinkedList;public class ProxyTester {    public static void main(String[] args) throws IOException {    	try {    		outputList(tryProxies(getList(args[0])), args[1]);    	} catch(IOException e) {    		System.out.print("IOException: "+e.toString());    	}    }         private static LinkedList<String> getList(String inputFilename) throws IOException {    	LinkedList<String> list = new LinkedList<String>();    	    	BufferedReader in = new BufferedReader(new FileReader(inputFilename));    	String inputProxies;    	while ((inputProxies = in.readLine()) != null) {    		list.add(inputProxies);    	}    	in.close();    	return list;    }        private static LinkedList<String> tryProxies(LinkedList<String> inputProxies) throws IOException {        LinkedList<String> workingProxies = new LinkedList<String>();                for(String currentProxy : inputProxies) {            String tmp[] = currentProxy.split(":");            if(connect(tmp[0], Integer.parseInt(tmp[1]))) {            	workingProxies.add(currentProxy);            }        }           return workingProxies;    }        private static void outputList(LinkedList<String> workingProxies, String outputFilename) throws IOException {    	BufferedWriter out = new BufferedWriter(new FileWriter(outputFilename));    	    	for(String currentProxy : workingProxies) {    		out.write(currentProxy+"\n");    	}    	out.close();    }        private static boolean connect(String proxy, int port) {    	boolean working = false;        Socket proxySocket = null;        PrintWriter out = null;        BufferedReader in = null;               try {  	           proxySocket = new Socket(proxy, port);           out = new PrintWriter(proxySocket.getOutputStream(), true);           in = new BufferedReader(new InputStreamReader(                                    proxySocket.getInputStream()));                        out.println("GET http://u1.swirve.com/ HTTP/1.1 \n");                        String currentLine;            while((currentLine = in.readLine()) != null) {                if(currentLine.equals("HTTP/1.1 OK") || currentLine.equals("HTTP/1.0 OK")) {                    working = true;                }            }        } catch (IOException e) {            working = false;        } finally {    		try {    			out.close();    			in.close();    			proxySocket.close();    		} catch(IOException e) {}    	}        return working;    }}