1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 |
import java.net.URL; import java.net.URLConnection; import java.net.HttpURLConnection; import java.io.InputStream; import java.io.BufferedReader; import java.io.InputStreamReader; public class ProxyDemo { // Set to true if you want to see verbose output. private final static boolean bDebug = false; /** * This function makes an HTTP GET request of the specified URL using a proxy if provided. * If successfully, the HTTP response headers are printed out. * If the MIME type of the response is text/html, then the number of lines of text * is printed as well. * * @param strURL - A string representing the URL to request, eg, "http://javac.in/" * @param strProxy - A string representing either the IP address or host name of the proxy server. * @param iProxyPort - An integer that indicates the proxy port or -1 to indicate the default port for the protocol. * @return rc is true if the request succeeded and false otherwise. */ static boolean doURLRequest(String strURL, String strProxy, int iProxyPort) { boolean rc = false; URL url = null; URLConnection c = null; try { System.out.println("\nHTTP Request: " + strURL); URL urlOriginal = new URL(strURL); if ((null != strProxy) && (0 < strProxy.length())) { URL urlProxy = new URL(urlOriginal.getProtocol(), strProxy, iProxyPort,// A value of -1 means use the default port for the specified protocol. strURL);// The original URL is passed as "the file on the host". System.out.println("Using Proxy: " + strProxy); if (-1 != iProxyPort) { System.out.println("Using Proxy Port: " + iProxyPort); } url = urlProxy; } else { url = urlOriginal; } c = url.openConnection(); // In this example, we only consider HTTP connections. if (c instanceof HttpURLConnection)// instanceof returns true only if the object is not null. { HttpURLConnection h = (HttpURLConnection) c; h.connect(); String strStatus = h.getResponseMessage() + " (" + h.getResponseCode() + ")"; System.out.println("HTTP Status: " + strStatus); System.out.println("HTTP Response Headers: "); // Evidently, index 0 always returns null, so we start with index 1. for (int i = 1; ; i++) { String strKey = h.getHeaderFieldKey(i); if (null == strKey) { break; } System.out.println(i + ": " + strKey + ": " + h.getHeaderField(i)); } // Normally at this point, one would download data from the connection. // For example, if the MIME type is html, then download the string and display it. String strContentType = h.getContentType(); if ((null != strContentType) && (0 == strContentType.compareTo("text/html"))) { if (bDebug) System.out.println("Received text/html:["); int iNumLines = 0; try { InputStream in = h.getInputStream(); BufferedReader data = new BufferedReader(new InputStreamReader(in)); String line = null; while((line = data.readLine()) != null) { if (bDebug) System.out.println(line); iNumLines++; } } catch(Exception exc2) { System.out.println("**** IO failure: " + exc2.toString()); } finally { if (bDebug) System.out.println("]"); System.out.println("Received text/html has " + iNumLines + " lines"); } } h.disconnect(); } else { System.out.println("**** No download: connection was not HTTP"); } rc = true; } catch(Exception exc) { System.out.println("*** Connection Failure: " + exc.toString()); } finally { // Do cleanup here. c = null; url = null; return rc; } } public static void main(String[] args) { // Simple request, not using a proxy server. ProxyDemo.doURLRequest("http://javac.in/", null, -1); ProxyDemo.doURLRequest("http://javac.in/", null, -1); // Request, using a proxy server. ProxyDemo.doURLRequest("http://javac.in/", "0.0.0.0", -1); // Change this line to use a valid proxy. } } |