/* * @(#)TestProxy.java (Metawerx customer example classes) * * Copyright (c) 1998-2005 Metawerx. All Rights Reserved. * * This software is the confidential and proprietary information of Metawerx * ("Confidential Information"). You shall not disclose such Confidential * Information and shall use it only in accordance with the terms of the * license agreement you entered into with Metawerx. * * METAWERX MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY * OF THE SOFTWARE OR THE SOURCE CODE, EITHER EXPRESS OR IMPLIED, INCLUDING * BUT NOT LIMITED TO THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR * A PARTICULAR PURPOSE, OR NON-INFRINGEMENT. METAWERX SHALL NOT BE LIABLE * FOR ANY DAMAGES SUFFERED BY ANY PARTY AS A RESULT OF USING, MODIFYING OR * DISTRIBUTING THIS SOFTWARE AND DOCUMENTATION OR ITS DERIVATIVES * * Synopsis * ===================================================================================== * This code connects to the specified proxy, to retrieve the specified site. * * Note that it is possible to also use the java.net.Proxy class in JDK1.5, but the * method below works for JDK1.4 and also supports authentication. * * See Also * ===================================================================================== * TestProxy2.java and ProxyAuth.java for examples without using an internal * Authenticator class * * History * ===================================================================================== * 6/19/2005 21:29 Neale Rudd * Created * */ import java.net.Authenticator; import java.net.URL; import java.net.URLConnection; import java.net.MalformedURLException; import java.io.BufferedReader; import java.io.InputStreamReader; import java.io.IOException; public class TestProxy { static String http_proxyHost = "PROXY HOST"; static String http_proxyPort = "PROXY PORT"; static String http_proxyUser = "USERNAME"; static String http_proxyPassword = "PASSWORD"; static String site = "SITE TO LOOK UP"; public static void main(String[] args) { if (args.length > 0) testProxy(http_proxyHost, http_proxyPort, site); } public static void testProxy(String host, String port, String urlString) { try { // Create a new class subclasses from Authenticator, which overrides getPasswordAuthentication() // to return the username and password. // Note: If you don't like this approach, please see TestProxy2.htm instead. Authenticator.setDefault(new Authenticator() { protected PasswordAuthentication getPasswordAuthentication() { return new PasswordAuthentication(http_proxyUser,http_proxyPassword.toCharArray()); }}); // Retrieve the page via the proxy URL lookupURL = new URL("HTTP", host, Integer.parseInt(port), urlString ); URLConnection connection = lookupURL.openConnection(); // Read the page, displaying it to the console with a line count BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream())); String line = null; int lineCount = 0; System.out.println("--- Reading from [" + urlString + "]"); while ((line = in.readLine()) != null) { System.out.print(line); lineCount++; } System.out.println(); System.out.println("--- Total lines [" + lineCount + "]"); // Cleanup in.close(); } catch (MalformedURLException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } }