import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.net.HttpURLConnection; import java.net.URL; import java.net.URLConnection; import java.util.Properties; /* * */ /** * */ public class TestProxy { private String port; private String host; private String URLString; private static final String DEFAULT_URL_STRING = "http://www.google.com"; public static void main(String[] args) { String inputHost; String inputPort; String inputURL; try { // get input data (host and port) if (args.length < 3) { inputHost = readInput("Please enter the hostname of the proxy server:"); inputPort = readInput( "Please enter the port number where the proxy is running on " + inputHost); inputURL = readInput( "Please enter the URL we will connect to or ENTER to use the default: " + DEFAULT_URL_STRING); } else { inputHost = args[0]; inputPort = args[1]; inputURL = args[2]; } // test if we should exit boolean exit = false; if (inputHost == null || "".equals(inputHost.trim())) { log("Unable to process, the Hostname is null or empty. Please check the hostname and try again"); exit = true; } if (inputPort == null || "".equals(inputPort.trim())) { log("Unable to process, the Port number is null or empty. Please check the port and try again"); exit = true; } if (!exit) { if (inputURL == null || "".equals(inputURL.trim())) { log( "The inputURL is null or empty. We will use the default one: " + DEFAULT_URL_STRING); inputURL = DEFAULT_URL_STRING; } } if (!exit) { TestProxy test = new TestProxy(inputHost, inputPort, inputURL); test.runTest(); } } catch (Exception e) { log(e); } System.exit(0); } /** * @param e */ private static void log(Exception e) { log(e.getMessage()); e.printStackTrace(); } /** * */ private void runTest() throws Exception { URL url = new URL(URLString); // set proxy properties // DproxySet=true -DproxyHost=http-proxy -DproxyPort=9001 foo Properties prop = System.getProperties(); prop.setProperty("proxySet", "true"); prop.setProperty("http.proxyHost", host); prop.setProperty("http.proxyport", port); System.setProperties(prop); // read stream InputStream in = null; URLConnection connection = null; try { connection = url.openConnection(); if (connection instanceof HttpURLConnection) { HttpURLConnection httpConnection = (HttpURLConnection) connection; int code = httpConnection.getResponseCode(); String msg = httpConnection.getResponseMessage(); log("Received: [" + code + "] " + msg); if (code == HttpURLConnection.HTTP_OK) { log("CONNECTION SUCCESSFULL. We were able to get an OK answer."); } else { log("UNSUCCESSFULL CONNECTION. Check code, maybe it is a valid error from the server"); } } in = connection.getInputStream(); BufferedReader stdin = new BufferedReader(new InputStreamReader(in)); log("READING:" + stdin.readLine()); } catch (Exception e) { log("ERROR when connecting..."); log(e); } finally { if (in != null) { try { in.close(); } catch (Exception e) { }; } } } /** * @param inputHost * @param inputPort */ public TestProxy(String inputHost, String inputPort, String inputURL) { host = inputHost; port = inputPort; URLString = inputURL; log("Creating test for proxy " + host + ":" + port); log("We will attempt to connect to : " + URLString); } /** * @param string */ private static void log(String string) { System.out.println("TEST PROXY: " + string); } /** * @param string * @return */ private static String readInput(String string) throws IOException { System.out.println(string); BufferedReader stdin = new BufferedReader(new InputStreamReader(System.in)); String result = stdin.readLine(); return result; } }