import java.io.*; import java.net.*; /** * A client to retrieve a web page using the URL package, instead * of raw socket.

* * @author Xiannong Meng * @date September 8, 2004 * Revised February 4, 2005 * */ public class URLClient { final static int MAXBUF = 4096; // max num of chars read one time public static void main(String argv[]) throws Exception { if (argv.length != 1) { System.err.println("usage : java URLClient url"); System.exit(1); } String content = readAPage(argv[0]); System.out.println(content); } // read a web page static public String readAPage(String urlStr) { HttpURLConnection uC = null; int responseCode = -1; InputStream input; BufferedReader remote; String strBuffer = ""; if (Crawler.DEBUG == true) System.err.println("url:" + urlStr); try { // urlStr is in the form "http://www.yahoo.com/" URL u = new URL(urlStr); // connet to the site uC = (HttpURLConnection)u.openConnection(); // System.out.println("port " + u.getPort()); responseCode = uC.getResponseCode(); } catch (Exception ex) { System.err.println("first : " + ex.getMessage()); } if (responseCode != HttpURLConnection.HTTP_OK) { System.err.println(("HTTP response code : " + String.valueOf(responseCode))); } try { input = uC.getInputStream(); remote = new BufferedReader(new InputStreamReader(input)); strBuffer = readPage(remote); // System.out.println(strBuffer); } catch (Exception ex) { System.err.println(ex.getMessage()); } /** System.out.println(" host : " + u.getHost()); System.out.println(" protocol : " + u.getProtocol()); System.out.println(" port : " + u.getPort()); System.out.println(" allow interaction : " + uC.getAllowUserInteraction()); System.out.println(" allow interaction default : " + uC.getDefaultAllowUserInteraction()); System.out.println("URL : " + uC.getURL()); System.out.println("header : " + uC.getHeaderField("http")); **/ return strBuffer; } // given a BufferedReader created from a URL, read the whole page static String readPage(BufferedReader inReader) throws IOException { String strBuffer = ""; char[] data = new char[MAXBUF]; int charRead = inReader.read(data, 0, MAXBUF); while (charRead > 0) { String localStr = new String(data, 0, charRead); strBuffer += localStr; charRead = inReader.read(data, 0, MAXBUF); } return strBuffer; } }