/* * @(#)TestProxy2.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. * * 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 TestProxy2 { public static void main(String[] s) { if (s.length < 5) System.out.println("TestProxy proxy port user pass site"); else testProxy(s[0], s[1], s[2], s[3], s[4]); } public static void testProxy(String host, String port, String username, String password, String urlString) { try { // Create an Authenticator to hold the username/password ProxyAuth auth = new ProxyAuth(); auth.setUser(username); auth.setPass(password); // Set this Authenticator as the default Authenticator.setDefault(auth); // 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(); } } }