{"id":10192,"date":"2015-08-24T07:29:15","date_gmt":"2015-08-24T11:29:15","guid":{"rendered":"http:\/\/bangla.salearningschool.com\/?p=10192"},"modified":"2015-08-01T09:16:45","modified_gmt":"2015-08-01T13:16:45","slug":"urlretriever-java-accepts-an-url-from-the-command-line-parses-the-host-port-and-uri-components-from-the-url-and-then-retrieves-the-document-requires-the-following-classes","status":"publish","type":"post","link":"http:\/\/bangla.sitestree.com\/?p=10192","title":{"rendered":"UrlRetriever.java Accepts an URL from the command line, parses the host, port, and URI components from the URL and then retrieves the document. Requires the following classes"},"content":{"rendered":"<pre>UrlRetriever.java\u00a0 Accepts an URL from the command line, parses the host, port, and URI components from the URL and then retrieves the document. Requires the following classes: \r\n\r\n\r\nimport java.util.*;\r\n\r\n\/** This parses the input to get a host, port, and file, then\r\n\u00a0*\u00a0 passes these three values to the UriRetriever class to\r\n\u00a0*\u00a0 grab the URL from the Web.\r\n\u00a0*\r\n\u00a0*\u00a0 Taken from Core Web Programming from\r\n\u00a0*\u00a0 Prentice Hall and Sun Microsystems Press,\r\n\u00a0*\u00a0 .\r\n\u00a0*\u00a0 \u00a9 2001 Marty Hall and Larry Brown;\r\n\u00a0*\u00a0 may be freely used or adapted.\r\n\u00a0*\/\r\n\r\npublic class UrlRetriever {\r\n\u00a0 public static void main(String[] args) {\r\n\u00a0\u00a0\u00a0 checkUsage(args);\r\n\u00a0\u00a0\u00a0 StringTokenizer tok = new StringTokenizer(args[0]);\r\n\u00a0\u00a0\u00a0 String protocol = tok.nextToken(\":\");\r\n\u00a0\u00a0\u00a0 checkProtocol(protocol);\r\n\u00a0\u00a0\u00a0 String host = tok.nextToken(\":\/\");\r\n\u00a0\u00a0\u00a0 String uri;\r\n\u00a0\u00a0\u00a0 int port = 80;\r\n\u00a0\u00a0\u00a0 try {\r\n\u00a0\u00a0\u00a0\u00a0\u00a0 uri = tok.nextToken(\"\");\r\n\u00a0\u00a0\u00a0\u00a0\u00a0 if (uri.charAt(0) == ':') {\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 tok = new StringTokenizer(uri);\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 port = Integer.parseInt(tok.nextToken(\":\/\"));\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 uri = tok.nextToken(\"\");\r\n\u00a0\u00a0\u00a0\u00a0\u00a0 }\r\n\u00a0\u00a0\u00a0 } catch(NoSuchElementException nsee) {\r\n\u00a0\u00a0\u00a0\u00a0\u00a0 uri = \"\/\";\r\n\u00a0\u00a0\u00a0 }\r\n\u00a0\u00a0\u00a0 UriRetriever uriClient = new UriRetriever(host, port, uri);\r\n\u00a0\u00a0\u00a0 uriClient.connect();\r\n\u00a0 }\r\n\r\n\u00a0 \/** Warn user if the URL was forgotten. *\/\r\n\r\n\u00a0 private static void checkUsage(String[] args) {\r\n\u00a0\u00a0\u00a0 if (args.length != 1) {\r\n\u00a0\u00a0\u00a0\u00a0\u00a0 System.out.println(\"Usage: UrlRetriever \");\r\n\u00a0\u00a0\u00a0\u00a0\u00a0 System.exit(-1);\r\n\u00a0\u00a0\u00a0 }\r\n\u00a0 }\r\n\r\n\u00a0 \/** Tell user that this can only handle HTTP. *\/\r\n\r\n\u00a0 private static void checkProtocol(String protocol) {\r\n\u00a0\u00a0\u00a0 if (!protocol.equals(\"http\")) {\r\n\u00a0\u00a0\u00a0\u00a0\u00a0 System.out.println(\"Don't understand protocol \" + protocol);\r\n\u00a0\u00a0\u00a0\u00a0\u00a0 System.exit(-1);\r\n\u00a0\u00a0\u00a0 }\r\n\u00a0 }\r\n}\r\n\r\n\r\n\r\n\r\nUriRetriever.java\u00a0 Given a host, port, and URI, retrieves the document from the HTTP server. \r\n\r\nimport java.net.*;\r\nimport java.io.*;\r\n\r\n\/** Retrieve a URL given the host, port, and file as three \r\n\u00a0*\u00a0 separate command-line arguments. A later class \r\n\u00a0*\u00a0 (UrlRetriever) supports a single URL instead.\r\n\u00a0*\r\n\u00a0*\u00a0 Taken from Core Web Programming from \r\n\u00a0*\u00a0 Prentice Hall and Sun Microsystems Press,\r\n\u00a0\r\n\u00a0*\u00a0 \u00a9 2001 Marty Hall and Larry Brown;\r\n\u00a0*\u00a0 may be freely used or adapted. \r\n\u00a0*\/\r\n\r\npublic class UriRetriever extends NetworkClient {\r\n\u00a0 private String uri;\r\n\r\n\u00a0 public static void main(String[] args) {\r\n\u00a0\u00a0\u00a0 UriRetriever uriClient\r\n\u00a0\u00a0\u00a0\u00a0\u00a0 = new UriRetriever(args[0], Integer.parseInt(args[1]),\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 args[2]);\r\n\u00a0\u00a0\u00a0 uriClient.connect();\r\n\u00a0 }\r\n\r\n\u00a0 public UriRetriever(String host, int port, String uri) {\r\n\u00a0\u00a0\u00a0 super(host, port); \r\n\u00a0\u00a0\u00a0 this.uri = uri;\r\n\u00a0 }\r\n\r\n\u00a0 \/** Send one GET line, then read the results one line at a\r\n\u00a0\u00a0 *\u00a0 time, printing each to standard output.\r\n\u00a0\u00a0 *\/\r\n\r\n\u00a0 \/\/ It is safe to use blocking IO (readLine), since\r\n\u00a0 \/\/ HTTP servers close connection when done, resulting\r\n\u00a0 \/\/ in a null value for readLine.\r\n\u00a0 \r\n\u00a0 protected void handleConnection(Socket uriSocket)\r\n\u00a0\u00a0\u00a0\u00a0\u00a0 throws IOException {\r\n\u00a0\u00a0\u00a0 PrintWriter out = SocketUtil.getWriter(uriSocket);\r\n\u00a0\u00a0\u00a0 BufferedReader in = SocketUtil.getReader(uriSocket);\r\n\u00a0\u00a0\u00a0 out.println(\"GET \" + uri + \" HTTP\/1.0\\r\\n\");\r\n\u00a0\u00a0\u00a0 String line;\r\n\u00a0\u00a0\u00a0 while ((line = in.readLine()) != null) {\r\n\u00a0\u00a0\u00a0\u00a0\u00a0 System.out.println(\"&gt; \" + line);\r\n\u00a0\u00a0\u00a0 }\r\n\u00a0 }\r\n}\r\n\r\n\r\n\r\n\r\n\r\nNetworkClient.java\r\n\r\nimport java.net.*;\r\nimport java.io.*;\r\n\r\n\/** A starting point for network clients. You'll need to\r\n\u00a0*\u00a0 override handleConnection, but in many cases connect can\r\n\u00a0*\u00a0 remain unchanged. It uses SocketUtil to simplify the\r\n\u00a0*\u00a0 creation of the PrintWriter and BufferedReader.\r\n\u00a0*\r\n\u00a0*\u00a0 Taken from Core Web Programming from\r\n\u00a0*\u00a0 Prentice Hall and Sun Microsystems Press,\r\n\u00a0*\u00a0 .\r\n\u00a0*\u00a0 \u00a9 2001 Marty Hall and Larry Brown;\r\n\u00a0*\u00a0 may be freely used or adapted.\r\n\u00a0*\/\r\n\r\npublic class NetworkClient {\r\n\u00a0 protected String host;\r\n\u00a0 protected int port;\r\n\r\n\u00a0 \/** Register host and port. The connection won't\r\n\u00a0\u00a0 *\u00a0 actually be established until you call\r\n\u00a0\u00a0 *\u00a0 connect.\r\n\u00a0\u00a0 *\/\r\n\r\n\u00a0 public NetworkClient(String host, int port) {\r\n\u00a0\u00a0\u00a0 this.host = host;\r\n\u00a0\u00a0\u00a0 this.port = port;\r\n\u00a0 }\r\n\r\n\u00a0 \/** Establishes the connection, then passes the socket\r\n\u00a0\u00a0 *\u00a0 to handleConnection.\r\n\u00a0\u00a0 *\/\r\n\r\n\u00a0 public void connect() {\r\n\u00a0\u00a0\u00a0 try {\r\n\u00a0\u00a0\u00a0\u00a0\u00a0 Socket client = new Socket(host, port);\r\n\u00a0\u00a0\u00a0\u00a0\u00a0 handleConnection(client);\r\n\u00a0\u00a0\u00a0 } catch(UnknownHostException uhe) {\r\n\u00a0\u00a0\u00a0\u00a0\u00a0 System.out.println(\"Unknown host: \" + host);\r\n\u00a0\u00a0\u00a0\u00a0\u00a0 uhe.printStackTrace();\r\n\u00a0\u00a0\u00a0 } catch(IOException ioe) {\r\n\u00a0\u00a0\u00a0\u00a0\u00a0 System.out.println(\"IOException: \" + ioe);\r\n\u00a0\u00a0\u00a0\u00a0\u00a0 ioe.printStackTrace();\r\n\u00a0\u00a0\u00a0 }\r\n\u00a0 }\r\n\r\n\u00a0 \/** This is the method you will override when\r\n\u00a0\u00a0 *\u00a0 making a network client for your task.\r\n\u00a0\u00a0 *\u00a0 The default version sends a single line\r\n\u00a0\u00a0 *\u00a0 (\"Generic Network Client\") to the server,\r\n\u00a0\u00a0 *\u00a0 reads one line of response, prints it, then exits.\r\n\u00a0\u00a0 *\/\r\n\r\n\u00a0 protected void handleConnection(Socket client)\r\n\u00a0\u00a0\u00a0 throws IOException {\r\n\u00a0\u00a0\u00a0 PrintWriter out = SocketUtil.getWriter(client);\r\n\u00a0\u00a0\u00a0 BufferedReader in = SocketUtil.getReader(client);\r\n\u00a0\u00a0\u00a0 out.println(\"Generic Network Client\");\r\n\u00a0\u00a0\u00a0 System.out.println\r\n\u00a0\u00a0\u00a0\u00a0\u00a0 (\"Generic Network Client:\\n\" +\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \"Made connection to \" + host +\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \" and got '\" + in.readLine() + \"' in response\");\r\n\u00a0\u00a0\u00a0 client.close();\r\n\u00a0 }\r\n\r\n\u00a0 \/** The hostname of the server we're contacting. *\/\r\n\r\n\u00a0 public String getHost() {\r\n\u00a0\u00a0\u00a0 return(host);\r\n\u00a0 }\r\n\r\n\u00a0 \/** The port connection will be made on. *\/\r\n\r\n\u00a0 public int getPort() {\r\n\u00a0\u00a0\u00a0 return(port);\r\n\u00a0 }\r\n}\r\n\r\nSocketUtil.java\u00a0 Provides utilities for wrapping a BufferedReader\u00a0 and PrintWriter around the Socket's input and output streams, respectively. \r\n\r\nimport java.net.*;\r\nimport java.io.*;\r\n\r\n\/** A shorthand way to create BufferedReaders and\r\n\u00a0*\u00a0 PrintWriters associated with a Socket.\r\n\u00a0*\r\n\u00a0*\u00a0 Taken from Core Web Programming from\r\n\u00a0*\u00a0 Prentice Hall and Sun Microsystems Press,\r\n\u00a0*\u00a0 .\r\n\u00a0*\u00a0 \u00a9 2001 Marty Hall and Larry Brown;\r\n\u00a0*\u00a0 may be freely used or adapted.\r\n\u00a0*\/\r\n\r\npublic class SocketUtil {\r\n\u00a0 \/** Make a BufferedReader to get incoming data. *\/\r\n\r\n\u00a0 public static BufferedReader getReader(Socket s)\r\n\u00a0\u00a0\u00a0\u00a0\u00a0 throws IOException {\r\n\u00a0\u00a0\u00a0 return(new BufferedReader(\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 new InputStreamReader(s.getInputStream())));\r\n\u00a0 }\r\n\r\n\u00a0 \/** Make a PrintWriter to send outgoing data.\r\n\u00a0\u00a0 *\u00a0 This PrintWriter will automatically flush stream\r\n\u00a0\u00a0 *\u00a0 when println is called.\r\n\u00a0\u00a0 *\/\r\n\r\n\u00a0 public static PrintWriter getWriter(Socket s)\r\n\u00a0\u00a0\u00a0\u00a0\u00a0 throws IOException {\r\n\u00a0\u00a0\u00a0 \/\/ Second argument of true means autoflush.\r\n\u00a0\u00a0\u00a0 return(new PrintWriter(s.getOutputStream(), true));\r\n\u00a0 }\r\n}<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>UrlRetriever.java\u00a0 Accepts an URL from the command line, parses the host, port, and URI components from the URL and then retrieves the document. Requires the following classes: import java.util.*; \/** This parses the input to get a host, port, and file, then \u00a0*\u00a0 passes these three values to the UriRetriever class to \u00a0*\u00a0 grab the &hellip; <\/p>\n<p><a class=\"more-link btn\" href=\"http:\/\/bangla.sitestree.com\/?p=10192\">Continue reading<\/a><\/p>\n","protected":false},"author":130,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_feature_clip_id":0,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_post_was_ever_published":false},"categories":[1417,1424],"tags":[1464,285],"class_list":["post-10192","post","type-post","status-publish","format-standard","hentry","category-code-programming-samples--","category-javaj2eej2me","tag-java-code","tag-285","item-wrap"],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack-related-posts":[{"id":26591,"url":"http:\/\/bangla.sitestree.com\/?p=26591","url_meta":{"origin":10192,"position":0},"title":"UrlRetriever.java  Accepts an URL from the command line, parses the host, port, and URI components from the URL and then retrieves the document. Requires the following classes: #Programming Code Examples #Java\/J2EE\/J2ME #Network Programming","author":"Author-Check- Article-or-Video","date":"April 29, 2021","format":false,"excerpt":"UrlRetriever.java Accepts an URL from the command line, parses the host, port, and URI components from the URL and then retrieves the document. Requires the following classes: import java.util.*; \/** This parses the input to get a host, port, and file, then * passes these three values to the UriRetriever\u2026","rel":"","context":"In &quot;FromSitesTree.com&quot;","block_context":{"text":"FromSitesTree.com","link":"http:\/\/bangla.sitestree.com\/?cat=1917"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":26489,"url":"http:\/\/bangla.sitestree.com\/?p=26489","url_meta":{"origin":10192,"position":1},"title":"UriRetriever.java  Accepts a host, port, and file from the command line and retrieves the implied URL from the HTTP server by issuing a GET request. Uses the following classes: #Programming Code Examples #NULL #Network Programming","author":"Author-Check- Article-or-Video","date":"April 26, 2021","format":false,"excerpt":"UriRetriever.java Accepts a host, port, and file from the command line and retrieves the implied URL from the HTTP server by issuing a GET request. Uses the following classes: import java.net.*; import java.io.*; \/** Retrieve a URL given the host, port, and file as three * separate command-line arguments. A\u2026","rel":"","context":"In &quot;FromSitesTree.com&quot;","block_context":{"text":"FromSitesTree.com","link":"http:\/\/bangla.sitestree.com\/?cat=1917"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":10196,"url":"http:\/\/bangla.sitestree.com\/?p=10196","url_meta":{"origin":10192,"position":2},"title":"UrlTest.java Demonstrates the ease in which the various components of an URL can be determined (host, port, protocol, etc.)","author":"","date":"August 25, 2015","format":false,"excerpt":"\u099c\u09be import java.net.*; \/** Read a URL from the command line, then print \u00a0*\u00a0 the various components. \u00a0* \u00a0*\u00a0 Taken from Core Web Programming from \u00a0*\u00a0 Prentice Hall and Sun Microsystems Press, \u00a0*\u00a0 . \u00a0*\u00a0 \u00a9 2001 Marty Hall and Larry Brown; \u00a0*\u00a0 may be freely used or adapted. \u00a0*\/\u2026","rel":"","context":"In &quot;Code . Programming Samples . \u09aa\u09cd\u09b0\u09cb\u0997\u09cd\u09b0\u09be\u09ae \u0989\u09a6\u09be\u09b9\u09b0\u09a8&quot;","block_context":{"text":"Code . Programming Samples . \u09aa\u09cd\u09b0\u09cb\u0997\u09cd\u09b0\u09be\u09ae \u0989\u09a6\u09be\u09b9\u09b0\u09a8","link":"http:\/\/bangla.sitestree.com\/?cat=1417"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":26595,"url":"http:\/\/bangla.sitestree.com\/?p=26595","url_meta":{"origin":10192,"position":3},"title":"UrlTest.java  Demonstrates the ease in which the various components of an URL can be determined (host, port, protocol, etc.). #Programming Code Examples #Java\/J2EE\/J2ME #Network Programming","author":"Author-Check- Article-or-Video","date":"April 29, 2021","format":false,"excerpt":"UrlTest.java Demonstrates the ease in which the various components of an URL can be determined (host, port, protocol, etc.). import java.net.*; \/** Read a URL from the command line, then print * the various components. * * Taken from Core Web Programming from * Prentice Hall and Sun Microsystems Press,\u2026","rel":"","context":"In &quot;FromSitesTree.com&quot;","block_context":{"text":"FromSitesTree.com","link":"http:\/\/bangla.sitestree.com\/?cat=1917"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":26583,"url":"http:\/\/bangla.sitestree.com\/?p=26583","url_meta":{"origin":10192,"position":4},"title":"NetworkClientTest.java  Makes a simple connection to the host and port specified on the command line. Uses the following classes: #Programming Code Examples #Java\/J2EE\/J2ME #Network Programming","author":"Author-Check- Article-or-Video","date":"April 29, 2021","format":false,"excerpt":"NetworkClientTest.java Makes a simple connection to the host and port specified on the command line. Uses the following classes: \/** Make simple connection to host and port specified. * * Taken from Core Web Programming from * Prentice Hall and Sun Microsystems Press, * . * \u00a9 2001 Marty Hall\u2026","rel":"","context":"In &quot;FromSitesTree.com&quot;","block_context":{"text":"FromSitesTree.com","link":"http:\/\/bangla.sitestree.com\/?cat=1917"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":10206,"url":"http:\/\/bangla.sitestree.com\/?p=10206","url_meta":{"origin":10192,"position":5},"title":"RMI Example &#8211; Message, illustrates retrieving a message from an object located on a remote server. Requires the following classes","author":"","date":"August 25, 2015","format":false,"excerpt":"Rem.java\u00a0 Establishes which methods the client can access in the remote object. import java.rmi.*; \/** The RMI client will use this interface directly. The RMI \u00a0*\u00a0 server will make a real remote object that implements this, \u00a0*\u00a0 then register an instance of it with some URL. \u00a0* \u00a0*\u00a0 Taken from\u2026","rel":"","context":"In &quot;Code . Programming Samples . \u09aa\u09cd\u09b0\u09cb\u0997\u09cd\u09b0\u09be\u09ae \u0989\u09a6\u09be\u09b9\u09b0\u09a8&quot;","block_context":{"text":"Code . Programming Samples . \u09aa\u09cd\u09b0\u09cb\u0997\u09cd\u09b0\u09be\u09ae \u0989\u09a6\u09be\u09b9\u09b0\u09a8","link":"http:\/\/bangla.sitestree.com\/?cat=1417"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]}],"_links":{"self":[{"href":"http:\/\/bangla.sitestree.com\/index.php?rest_route=\/wp\/v2\/posts\/10192","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/bangla.sitestree.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/bangla.sitestree.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/bangla.sitestree.com\/index.php?rest_route=\/wp\/v2\/users\/130"}],"replies":[{"embeddable":true,"href":"http:\/\/bangla.sitestree.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=10192"}],"version-history":[{"count":1,"href":"http:\/\/bangla.sitestree.com\/index.php?rest_route=\/wp\/v2\/posts\/10192\/revisions"}],"predecessor-version":[{"id":10193,"href":"http:\/\/bangla.sitestree.com\/index.php?rest_route=\/wp\/v2\/posts\/10192\/revisions\/10193"}],"wp:attachment":[{"href":"http:\/\/bangla.sitestree.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=10192"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/bangla.sitestree.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=10192"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/bangla.sitestree.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=10192"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}