{"id":183,"date":"2018-07-26T11:49:25","date_gmt":"2018-07-26T11:49:25","guid":{"rendered":"http:\/\/csp12.epgpbooks.inflibnet.ac.in\/?post_type=chapter&#038;p=183"},"modified":"2018-08-02T06:59:38","modified_gmt":"2018-08-02T06:59:38","slug":"java-networking","status":"publish","type":"chapter","link":"https:\/\/ebooks.inflibnet.ac.in\/csp12\/chapter\/java-networking\/","title":{"rendered":"Java Networking"},"content":{"raw":"<div>\r\n\r\n<strong>URL Connections<\/strong>\r\n\r\n&nbsp;\r\n\r\n<strong>URL stands for Uniform Resource Locator<\/strong>\r\n\r\n&nbsp;\r\n\r\nIt is a scheme used for uniquely identifying all kinds of network resources.\r\n\r\nThe basic form of a URL looks like,\r\n\r\n&lt;pr otoc ol&gt;:&lt;site name &gt;&lt;pathname &gt;\r\n\r\nSome examples are,\r\n\r\nhttp:\/\/www.cs.ust.hk\/~liao\/comp201\/index.html\r\n\r\nftp:\/\/ftp.cs.ust.hk\/pub\/lzhang\/teach\/201\/codes\/HttpTest\/HttpTest.java file:\/MyDisk\/Letters\/Toxx2-11-15.The protocols include file, http, ftp, gopher, news, mailto, etc.\r\n\r\n<\/div>\r\n<div>\r\n<p style=\"text-align: justify\">Generally, connecting to a URL is a hard way which means we need to manually parse out the host, protocol, path from URL and create a socket to host. Then we need to use the protocol to send the right request and interpret response. But Java makes this much easier than that.<\/p>\r\n&nbsp;\r\n\r\n<strong>URL Connections\/Retrieve Information<\/strong>\r\n\r\n&nbsp;\r\n\r\n<strong>Open connection with java.net.URL<\/strong>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">To open a connection with a URL, first we need to create an URL object using the constructor of the URL class as discussed in the previous module.<\/p>\r\n<p style=\"text-align: justify\">URL url1 = new URL(\u201c<a href=\"http:\/\/www.cs.ust.hk\/~liao\/comp201\/index.html\">http:\/\/www.cs.ust.hk\/~liao\/comp201\/index.html<\/a>\u201d);<\/p>\r\n<p style=\"text-align: justify\">Then to fetch contents of the resource, use the openStream method of URL, which returns an InputStream.<\/p>\r\n<p style=\"text-align: justify\">InputStream in1 = url1.openStream();<\/p>\r\n<p style=\"text-align: justify\">Now we can nest the InputStreams with other Java streams to retrieve the contents from the URL.<\/p>\r\n&nbsp;\r\n\r\n<strong>Example of Read stream<\/strong>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">This is an example that reads a stream directly from the URL. Here we use the openStream() method of the URL class that returns an InputStream. This InputStream is wrapped with the BufferedReader to read lines of text.<\/p>\r\n&nbsp;\r\n\r\nimport java.net.*;\r\n\r\nimport java.io.*;\r\n\r\npublic class ReadStream\r\n\r\n{\r\n\r\npublic static void main(String args[]) throws Exception\r\n\r\n{\r\n\r\ntry{\r\n\r\nURL u = new URL(\"https:\/\/www.google.co.in \"); InputStream in = u.openStream();\r\n\r\nBufferedReader bin = new BufferedReader(new InputStreamReader(in)); String temp;\r\n\r\nwhile(( temp=bin.readLine()) != null)\r\n\r\n{\r\n\r\nSystem.out.println(temp);\r\n\r\n}\r\n\r\n}catch(IOExce ption e)\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">{<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">System.out.println(\"Socket error\");<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">}<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">}<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">}<\/span>\r\n\r\n<\/div>\r\nThe output of this code is shown in Figure 21.1.\r\n\r\n&nbsp;\r\n<p style=\"text-align: center\"><img class=\"size-full wp-image-184 aligncenter\" src=\"http:\/\/csp12.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-104.png\" alt=\"\" width=\"556\" height=\"325\" \/><\/p>\r\n\r\n<div>\r\n\r\n<strong>URLConnection<\/strong>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">URLConnection is a general-purpose class for accessing the attributes of a remote resource. This URLConnection class is used to inspect the properties of the remote object before actually transporting it locally. These attributes are exposed by the HTTP protocol specification and, as such, only make sense for URL objects that are using the HTTP protocol. The instance methods defined in the URLConnection class is listed in the Table 21.1.<\/p>\r\n\r\n<\/div>\r\n<p style=\"text-align: center\"><img class=\"size-full wp-image-185 aligncenter\" src=\"http:\/\/csp12.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-105.png\" alt=\"\" width=\"658\" height=\"430\" \/><\/p>\r\n&nbsp;\r\n\r\n<strong>HttpURLConnection<\/strong>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">Java provides a subclass of URLConnection that provides support for HTTP connections. This class is called HttpURLConnection. We can obtain an HttpURLConnection object in the sameway, by calling the openConnection( ) method on an URL object. The instance methods defined in the HttpURLConnection class is listed in the Table 21.2.<\/p>\r\n<p style=\"text-align: center\"><img class=\"size-full wp-image-186 aligncenter\" src=\"http:\/\/csp12.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-106.png\" alt=\"\" width=\"659\" height=\"636\" \/><\/p>\r\n&nbsp;\r\n<p style=\"text-align: justify\"><strong style=\"text-align: initial;font-size: 1em\">Communicating with Web Servers<\/strong><\/p>\r\n\r\n<div>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">Class java.net.URL represents a Uniform Resource Locator. Create an Java object that represents an URL using the constructor of the URL class.<\/p>\r\n&nbsp;\r\n\r\nURL url = new URL(\u201c<a href=\"http:\/\/www.cs.ust.hk\/~lzhang\/comp201\/index.html\">http:\/\/www.cs.ust.hk\/~lzhang\/comp201\/index.html<\/a>\u201d);\r\n\r\n&nbsp;\r\n\r\nThe instance methods of the URL class are etc.\r\n\r\n&nbsp;\r\n\r\nThis <em>java.net.URLConnection<\/em> represents a communication link between the application and a URL.\r\n\r\n&nbsp;\r\n\r\nWe can use the Constructor of this class to create an URLConnection object.\r\n\r\n&nbsp;\r\n\r\nURLConnection con = new URLConnection( url).\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\"><span style=\"text-align: initial;font-size: 1em\">The connection can also be obtainable from the URL class. But here we can create an URLConnection object by using the openConnection() method of the URL class to create an object.<\/span><\/p>\r\n&nbsp;\r\n<p style=\"text-align: justify\"><span style=\"text-align: initial;font-size: 1em\">URLConnection con = url.openConnection();<\/span><\/p>\r\n\r\n<\/div>\r\n<div>\r\n\r\n<strong>\u00a0 \u00a0 <\/strong>\r\n\r\n<strong>URLConnection<\/strong>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">We can directly open a stream for reading using the URL class. The signature of the openStream() method is given below,public final<\/p>\r\n&nbsp;\r\n<p style=\"text-align: justify\"><a href=\"https:\/\/course.cs.ust.hk\/comp201\/2003spring\/Password_Only\/jDocs\/1.4.1\/api\/java\/io\/InputStream.html\">InputStream <\/a>openStream() throws <a href=\"https:\/\/course.cs.ust.hk\/comp201\/2003spring\/Password_Only\/jDocs\/1.4.1\/api\/java\/io\/IOException.html\">IOException<\/a><\/p>\r\nInputStream in = u.openStream();\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">This opens a connection to this URL and returns an InputStream for reading from that connection. This method is a shorthand for,<\/p>\r\nopenConnection().getInputStream()\r\n\r\n&nbsp;\r\n\r\n<strong>Connecting to a URL using URLConnection<\/strong>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">We can connect to URL using the URL\u2019s <em>openConnection()<\/em> method that returns an URLConnection object. The signature is given below,URLConnection openConnection() throws IOExce ption ;<\/p>\r\n&nbsp;\r\n\r\n<strong>Example:<\/strong>\r\n\r\n&nbsp;\r\n\r\ntry {\r\n\r\nURL url = new URL(\"http:\/\/www.cs.biu.ac.il\/~freskom1\/\") ; URLConnection uc = url.openConnection() ;\r\n\r\n}\r\n\r\ncatch (MalformedURLException e)\r\n\r\n{ \u2026 }\r\n\r\ncatch (IOException e)\r\n\r\n{ \u2026 }\r\n\r\n<\/div>\r\n<strong>\u00a0 Reading from a URL connection<\/strong>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">To read from the URL connection, we first create an URL object using the constrcutor of the URL class. Then we can use the <em>openConnection()<\/em> method of the URL class that returns an URLConnection object. The method <em>getInputStream()<\/em> of this URLConnection object returns an InputStream that can be wrapped with other streams to read data from the URL.<\/p>\r\n&nbsp;\r\n\r\nimport java.net.* ;\r\n\r\nimport java.io.* ;\r\n\r\npublic class URLConnectionReader1\r\n\r\n{\r\n\r\npublic static void main(String[] args) throws Exception\r\n\r\n{\r\n\r\nURL url = new URL(\"http:\/\/www.google.com\") ; URLConnection uc = url.openConnection() ; BufferedReader br=new BufferedReader(new InputStreamReader(uc.getInputStream()));\r\n\r\nString line ;\r\n\r\nwhile ((line=br.readLine())!=null)\r\n\r\nSystem.out.println(line) ;\r\n\r\nbr.close() ;\r\n\r\n}\r\n\r\n}\r\n<p style=\"text-align: justify\">The output of reading from URL using URLConnection is given in the following Figure 21.2.<\/p>\r\n&nbsp;\r\n\r\n<strong>Output of Reading from a URL connection<\/strong>\r\n\r\n&nbsp;\r\n<p style=\"text-align: center\"><img class=\"size-full wp-image-187 aligncenter\" src=\"http:\/\/csp12.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-107.png\" alt=\"\" width=\"533\" height=\"357\" \/><\/p>\r\n\r\n<div>\r\n\r\n<strong>Instance Methods of URLConnection<\/strong>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">Some of the instance methods of the URLConnection class is demonstrated in this example code. The method <em>getDate()<\/em> prints the time and date of the response in milliseconds. The method <em>getContentT ype() <\/em>prints the type of content found in the resource. The method<em> getLastModified() <\/em>prints the time and date when the resource was last modified in milliseconds. The method <em>getExpiration() <\/em>prints the expiration time and date of the resource in milliseconds. The method<em> getContentLength() <\/em>prints the size in bytes of the content associated with the resource if it is available. Finally, if the content is available it opens an input stream to the resource and reads the content.<\/p>\r\n&nbsp;\r\n\r\nimport java.net.*;\r\n\r\nimport java.io.*;\r\n\r\nimport java.util.Date;\r\n\r\npublic class URLConnectionReade r\r\n\r\n{\r\n\r\npublic static void main(String[] args) throws Exception {\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">int c;<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">\/\/URL yahoo = new URL(\"http:\/\/java.sun.com\/docs\/books\/tutorial\/\"); URL yahoo = new URL(\"https:\/\/www.google.co.in\/\");<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">URLConnection yc = yahoo.openConnection();<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">long d = yc.getDate();<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">if(d==0)<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">System.out.println(\"no date info\");<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">else<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">System.out.println(\"Date:\" + new Date(d)); System.out.println(\"content type\" + yc.getContentType()); d = yc.getExpiration();<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">if(d==0)<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">System.out.println(\"no expiration info\");<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">else<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">System.out.println(\"Expires\" + new Date(d));<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">d = yc.getLastModified();<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">if(d==0)<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">System.out.println(\"no last modified info\");<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">else<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">System.out.println(\"last modified\"+ new Date(d))<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">int len = yc.getContentLength();<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">if(len == -1)<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">System.out.println(\"content length unavailable\"); else<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">s<\/span><span style=\"text-align: initial;font-size: 1em\">ystem.out.println(\"content length\" + len);<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">if(len!=0)<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">{<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">System.out.println(\"===content===\");<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">InputStream input = yc.getInputStream();<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">while((( c= input.read()) != -1))<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">{<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">System.out.print((char) c);<\/span>\r\n\r\n}\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">input.close();<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">}<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">else<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">{<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">System.out.println(\"no content available\");<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">}<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">}<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">}<\/span>\r\n\r\n<\/div>\r\n<p style=\"text-align: justify\">The output of demonstrating the instance methods of URLConnection is shown in Figure 21.3<\/p>\r\n&nbsp;\r\n\r\n<strong>Output of URL Connection<\/strong>\r\n\r\n&nbsp;\r\n<p style=\"text-align: center\"><img class=\"size-full wp-image-188 aligncenter\" src=\"http:\/\/csp12.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-108.png\" alt=\"\" width=\"509\" height=\"384\" \/><\/p>\r\n&nbsp;\r\n<div>\r\n\r\n<strong>\u00a0 \u00a0 \u00a0 \u00a0URL Connections\/Sending Information<\/strong>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">The browser allows the user to enter data in text fields and other GUI components. Like processing HTML forms, the browser writes the data to the URL. On the server a CGI-BIN script processes it and returns a response.<\/p>\r\n&nbsp;\r\n<p style=\"text-align: justify\"><span style=\"text-align: initial;font-size: 1em\">The Web servers receive information from clients using either GET or POST. The request method is specified in the form as below,<\/span><\/p>\r\n\r\n<\/div>\r\n<div>\r\n\r\n\u00a0 \u00a0 &lt;form method=\u201cpost\u201d action=\"\/cgi-bin\/ipc\/idbsprd\"&gt;\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">Appropriate CGI (common gateway interface) script is called to process information received and produce an HTML page to send back to client. CGI scripts are usually written in C, Perl, shellscript etc.<\/p>\r\n&nbsp;\r\n\r\n<strong>Send information to CGI script using GET<\/strong>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">The \"GET\" request attaches the parameters to the end of URL as shown below,<a href=\"http:\/\/host\/script?parameters\">http:\/\/host\/script?parameters.<\/a><\/p>\r\n<p style=\"text-align: justify\">The parameters are seperated using \u201c&amp;\u201d and parameters are encoded as follows to avoid misinterpretation which is called as URL encoding.<\/p>\r\n&nbsp;\r\n\r\nThe URL encoding is done as follows,\r\n<ul>\r\n \t<li>Replace space with \u201c+\u201d<\/li>\r\n \t<li style=\"text-align: justify\">Replace each non-alphanumeric character with \u201c%\u201d followed by the hexadecimal code of the character<\/li>\r\n<\/ul>\r\nFor example,\r\n\r\n\u201cMastering C++\u201d \u00e8 \u201cMastering+C%2b%2b\u201d\r\n<p style=\"text-align: justify\">The disadvantage of this request type is long parameter string, might exceed limits of browsers and hence could not be sent.<\/p>\r\n&nbsp;\r\n\r\n<strong>Writing Information<\/strong>\r\n\r\n&nbsp;\r\n\r\nTo write information to an URL resource, follow the steps as shown below,\r\n\r\n&nbsp;\r\n\r\n<strong>Steps for working with java.net.URLConnection<\/strong>\r\n\r\n&nbsp;\r\n\r\n<strong>1.<\/strong>\u00a0 <strong>Create URLConnection object from URL,<\/strong>\r\n\r\n<\/div>\r\n<div>\r\n<ul>\r\n \t<li>\u00a0URLConnection con = url.openConnection();<\/li>\r\n<\/ul>\r\n<strong>\u00a0 \u00a0 2.\u00a0<\/strong><strong>Set properties of connection:<\/strong>\r\n<ul>\r\n \t<li>setDoInPut(true) \/\/default<\/li>\r\n \t<li>setDoOutPut(true) for sending information to the server<\/li>\r\n<\/ul>\r\n<strong>\u00a0 \u00a03.\u00a0<\/strong><strong>Make connection:<\/strong>\r\n<ul>\r\n \t<li>con.connect();<\/li>\r\n<\/ul>\r\n<strong>\u00a0 \u00a04.\u00a0\u00a0<\/strong><strong>Query header information can be obtained by the following methods<\/strong>\r\n<ul>\r\n \t<li style=\"text-align: justify\"><strong>\u00a0<\/strong>getContentType, getContentLength, getContentEncoding, getDate, getExpiration, getLastModified<\/li>\r\n<\/ul>\r\n<strong>\u00a0 \u00a05.\u00a0<\/strong><strong>Use getInputStream() for reading and getOutputStream() for writing<\/strong>\r\n\r\n&nbsp;\r\n\r\n<strong>Writing to a URLConnection<\/strong>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">The example code demonstrates about how to write the reverse of a string to a URL connection. The input string received in the command line is reversed using URLEncoder. An URL object is created and an URLConnection object is created using the openConnection() method. The output mode of the URLConnection is set as true and the outputstream is obtained and the string is writtem to the resource. Then the contents are fetched again from the URL and displayed.<\/p>\r\n&nbsp;\r\n\r\nimport java.io.* ;\r\n\r\nimport java.net.* ;\r\n\r\npublic class Reverse {\r\n\r\npublic static void main(String[]args) throws Exception\r\n\r\n{\r\n\r\nif (args.length&lt;1)\r\n\r\n{\r\n\r\nSystem.err.println(\"Usage: java Reverse &lt;String&gt;\") ; System.exit(1) ;\r\n\r\n}\r\n\r\nString stringToReverse = URLEncoder.encode(args[0],\"UTF-8\") ;\r\n\r\n&nbsp;\r\n\r\nURL url = new URL(\"http:\/\/java.sun.com\/cgi -bin\/backwards\") ; URLConnection uc = url.openConnection() ; uc.setDoOutput(true) ;\r\n\r\nPrintWriter out = new PrintWriter(uc.getOutputStream()) ;\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">out.println(\"string=\"+stringToReverse) ;<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">out.close() ;<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">BufferedReader in = new BufferedReader(new\u00a0<\/span><span style=\"text-align: initial;font-size: 1em\">InputStreamReader(uc.getInputStream())) ;<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">String line ;<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">while ((line=in.readLine())!=null)<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">System.out.println(line) ;<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">}<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">}<\/span>\r\n\r\n<\/div>\r\n<strong>\u00a0 \u00a0 Summary<\/strong>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">This module explains about communicating with web servers using URL and URLConnection class in Java. This module also discusses about how to retrieve information and send information to servers with examples.<\/p>\r\n&nbsp;\r\n\r\n<strong>Web Links<\/strong>\r\n<ul>\r\n \t<li>Herbert Schildt, \" <em>Java: The Complete Reference\", 9th Edition,<\/em> Mcgraw-Hill, 2014.<\/li>\r\n<\/ul>","rendered":"<div>\n<p><strong>URL Connections<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p><strong>URL stands for Uniform Resource Locator<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p>It is a scheme used for uniquely identifying all kinds of network resources.<\/p>\n<p>The basic form of a URL looks like,<\/p>\n<p>&lt;pr otoc ol&gt;:&lt;site name &gt;&lt;pathname &gt;<\/p>\n<p>Some examples are,<\/p>\n<p>http:\/\/www.cs.ust.hk\/~liao\/comp201\/index.html<\/p>\n<p>ftp:\/\/ftp.cs.ust.hk\/pub\/lzhang\/teach\/201\/codes\/HttpTest\/HttpTest.java file:\/MyDisk\/Letters\/Toxx2-11-15.The protocols include file, http, ftp, gopher, news, mailto, etc.<\/p>\n<\/div>\n<div>\n<p style=\"text-align: justify\">Generally, connecting to a URL is a hard way which means we need to manually parse out the host, protocol, path from URL and create a socket to host. Then we need to use the protocol to send the right request and interpret response. But Java makes this much easier than that.<\/p>\n<p>&nbsp;<\/p>\n<p><strong>URL Connections\/Retrieve Information<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p><strong>Open connection with java.net.URL<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">To open a connection with a URL, first we need to create an URL object using the constructor of the URL class as discussed in the previous module.<\/p>\n<p style=\"text-align: justify\">URL url1 = new URL(\u201c<a href=\"http:\/\/www.cs.ust.hk\/~liao\/comp201\/index.html\">http:\/\/www.cs.ust.hk\/~liao\/comp201\/index.html<\/a>\u201d);<\/p>\n<p style=\"text-align: justify\">Then to fetch contents of the resource, use the openStream method of URL, which returns an InputStream.<\/p>\n<p style=\"text-align: justify\">InputStream in1 = url1.openStream();<\/p>\n<p style=\"text-align: justify\">Now we can nest the InputStreams with other Java streams to retrieve the contents from the URL.<\/p>\n<p>&nbsp;<\/p>\n<p><strong>Example of Read stream<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">This is an example that reads a stream directly from the URL. Here we use the openStream() method of the URL class that returns an InputStream. This InputStream is wrapped with the BufferedReader to read lines of text.<\/p>\n<p>&nbsp;<\/p>\n<p>import java.net.*;<\/p>\n<p>import java.io.*;<\/p>\n<p>public class ReadStream<\/p>\n<p>{<\/p>\n<p>public static void main(String args[]) throws Exception<\/p>\n<p>{<\/p>\n<p>try{<\/p>\n<p>URL u = new URL(&#8220;https:\/\/www.google.co.in &#8220;); InputStream in = u.openStream();<\/p>\n<p>BufferedReader bin = new BufferedReader(new InputStreamReader(in)); String temp;<\/p>\n<p>while(( temp=bin.readLine()) != null)<\/p>\n<p>{<\/p>\n<p>System.out.println(temp);<\/p>\n<p>}<\/p>\n<p>}catch(IOExce ption e)<\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">{<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">System.out.println(&#8220;Socket error&#8221;);<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">}<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">}<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">}<\/span><\/p>\n<\/div>\n<p>The output of this code is shown in Figure 21.1.<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: center\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-184 aligncenter\" src=\"http:\/\/csp12.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-104.png\" alt=\"\" width=\"556\" height=\"325\" srcset=\"https:\/\/ebooks.inflibnet.ac.in\/csp12\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-104.png 556w, https:\/\/ebooks.inflibnet.ac.in\/csp12\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-104-300x175.png 300w, https:\/\/ebooks.inflibnet.ac.in\/csp12\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-104-65x38.png 65w, https:\/\/ebooks.inflibnet.ac.in\/csp12\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-104-225x132.png 225w, https:\/\/ebooks.inflibnet.ac.in\/csp12\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-104-350x205.png 350w\" sizes=\"auto, (max-width: 556px) 100vw, 556px\" \/><\/p>\n<div>\n<p><strong>URLConnection<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">URLConnection is a general-purpose class for accessing the attributes of a remote resource. This URLConnection class is used to inspect the properties of the remote object before actually transporting it locally. These attributes are exposed by the HTTP protocol specification and, as such, only make sense for URL objects that are using the HTTP protocol. The instance methods defined in the URLConnection class is listed in the Table 21.1.<\/p>\n<\/div>\n<p style=\"text-align: center\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-185 aligncenter\" src=\"http:\/\/csp12.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-105.png\" alt=\"\" width=\"658\" height=\"430\" srcset=\"https:\/\/ebooks.inflibnet.ac.in\/csp12\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-105.png 658w, https:\/\/ebooks.inflibnet.ac.in\/csp12\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-105-300x196.png 300w, https:\/\/ebooks.inflibnet.ac.in\/csp12\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-105-65x42.png 65w, https:\/\/ebooks.inflibnet.ac.in\/csp12\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-105-225x147.png 225w, https:\/\/ebooks.inflibnet.ac.in\/csp12\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-105-350x229.png 350w\" sizes=\"auto, (max-width: 658px) 100vw, 658px\" \/><\/p>\n<p>&nbsp;<\/p>\n<p><strong>HttpURLConnection<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">Java provides a subclass of URLConnection that provides support for HTTP connections. This class is called HttpURLConnection. We can obtain an HttpURLConnection object in the sameway, by calling the openConnection( ) method on an URL object. The instance methods defined in the HttpURLConnection class is listed in the Table 21.2.<\/p>\n<p style=\"text-align: center\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-186 aligncenter\" src=\"http:\/\/csp12.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-106.png\" alt=\"\" width=\"659\" height=\"636\" srcset=\"https:\/\/ebooks.inflibnet.ac.in\/csp12\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-106.png 659w, https:\/\/ebooks.inflibnet.ac.in\/csp12\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-106-300x290.png 300w, https:\/\/ebooks.inflibnet.ac.in\/csp12\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-106-65x63.png 65w, https:\/\/ebooks.inflibnet.ac.in\/csp12\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-106-225x217.png 225w, https:\/\/ebooks.inflibnet.ac.in\/csp12\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-106-350x338.png 350w\" sizes=\"auto, (max-width: 659px) 100vw, 659px\" \/><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\"><strong style=\"text-align: initial;font-size: 1em\">Communicating with Web Servers<\/strong><\/p>\n<div>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">Class java.net.URL represents a Uniform Resource Locator. Create an Java object that represents an URL using the constructor of the URL class.<\/p>\n<p>&nbsp;<\/p>\n<p>URL url = new URL(\u201c<a href=\"http:\/\/www.cs.ust.hk\/~lzhang\/comp201\/index.html\">http:\/\/www.cs.ust.hk\/~lzhang\/comp201\/index.html<\/a>\u201d);<\/p>\n<p>&nbsp;<\/p>\n<p>The instance methods of the URL class are etc.<\/p>\n<p>&nbsp;<\/p>\n<p>This <em>java.net.URLConnection<\/em> represents a communication link between the application and a URL.<\/p>\n<p>&nbsp;<\/p>\n<p>We can use the Constructor of this class to create an URLConnection object.<\/p>\n<p>&nbsp;<\/p>\n<p>URLConnection con = new URLConnection( url).<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\"><span style=\"text-align: initial;font-size: 1em\">The connection can also be obtainable from the URL class. But here we can create an URLConnection object by using the openConnection() method of the URL class to create an object.<\/span><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\"><span style=\"text-align: initial;font-size: 1em\">URLConnection con = url.openConnection();<\/span><\/p>\n<\/div>\n<div>\n<p><strong>\u00a0 \u00a0 <\/strong><\/p>\n<p><strong>URLConnection<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">We can directly open a stream for reading using the URL class. The signature of the openStream() method is given below,public final<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\"><a href=\"https:\/\/course.cs.ust.hk\/comp201\/2003spring\/Password_Only\/jDocs\/1.4.1\/api\/java\/io\/InputStream.html\">InputStream <\/a>openStream() throws <a href=\"https:\/\/course.cs.ust.hk\/comp201\/2003spring\/Password_Only\/jDocs\/1.4.1\/api\/java\/io\/IOException.html\">IOException<\/a><\/p>\n<p>InputStream in = u.openStream();<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">This opens a connection to this URL and returns an InputStream for reading from that connection. This method is a shorthand for,<\/p>\n<p>openConnection().getInputStream()<\/p>\n<p>&nbsp;<\/p>\n<p><strong>Connecting to a URL using URLConnection<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">We can connect to URL using the URL\u2019s <em>openConnection()<\/em> method that returns an URLConnection object. The signature is given below,URLConnection openConnection() throws IOExce ption ;<\/p>\n<p>&nbsp;<\/p>\n<p><strong>Example:<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p>try {<\/p>\n<p>URL url = new URL(&#8220;http:\/\/www.cs.biu.ac.il\/~freskom1\/&#8221;) ; URLConnection uc = url.openConnection() ;<\/p>\n<p>}<\/p>\n<p>catch (MalformedURLException e)<\/p>\n<p>{ \u2026 }<\/p>\n<p>catch (IOException e)<\/p>\n<p>{ \u2026 }<\/p>\n<\/div>\n<p><strong>\u00a0 Reading from a URL connection<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">To read from the URL connection, we first create an URL object using the constrcutor of the URL class. Then we can use the <em>openConnection()<\/em> method of the URL class that returns an URLConnection object. The method <em>getInputStream()<\/em> of this URLConnection object returns an InputStream that can be wrapped with other streams to read data from the URL.<\/p>\n<p>&nbsp;<\/p>\n<p>import java.net.* ;<\/p>\n<p>import java.io.* ;<\/p>\n<p>public class URLConnectionReader1<\/p>\n<p>{<\/p>\n<p>public static void main(String[] args) throws Exception<\/p>\n<p>{<\/p>\n<p>URL url = new URL(&#8220;http:\/\/www.google.com&#8221;) ; URLConnection uc = url.openConnection() ; BufferedReader br=new BufferedReader(new InputStreamReader(uc.getInputStream()));<\/p>\n<p>String line ;<\/p>\n<p>while ((line=br.readLine())!=null)<\/p>\n<p>System.out.println(line) ;<\/p>\n<p>br.close() ;<\/p>\n<p>}<\/p>\n<p>}<\/p>\n<p style=\"text-align: justify\">The output of reading from URL using URLConnection is given in the following Figure 21.2.<\/p>\n<p>&nbsp;<\/p>\n<p><strong>Output of Reading from a URL connection<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: center\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-187 aligncenter\" src=\"http:\/\/csp12.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-107.png\" alt=\"\" width=\"533\" height=\"357\" srcset=\"https:\/\/ebooks.inflibnet.ac.in\/csp12\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-107.png 533w, https:\/\/ebooks.inflibnet.ac.in\/csp12\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-107-300x201.png 300w, https:\/\/ebooks.inflibnet.ac.in\/csp12\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-107-65x44.png 65w, https:\/\/ebooks.inflibnet.ac.in\/csp12\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-107-225x151.png 225w, https:\/\/ebooks.inflibnet.ac.in\/csp12\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-107-350x234.png 350w\" sizes=\"auto, (max-width: 533px) 100vw, 533px\" \/><\/p>\n<div>\n<p><strong>Instance Methods of URLConnection<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">Some of the instance methods of the URLConnection class is demonstrated in this example code. The method <em>getDate()<\/em> prints the time and date of the response in milliseconds. The method <em>getContentT ype() <\/em>prints the type of content found in the resource. The method<em> getLastModified() <\/em>prints the time and date when the resource was last modified in milliseconds. The method <em>getExpiration() <\/em>prints the expiration time and date of the resource in milliseconds. The method<em> getContentLength() <\/em>prints the size in bytes of the content associated with the resource if it is available. Finally, if the content is available it opens an input stream to the resource and reads the content.<\/p>\n<p>&nbsp;<\/p>\n<p>import java.net.*;<\/p>\n<p>import java.io.*;<\/p>\n<p>import java.util.Date;<\/p>\n<p>public class URLConnectionReade r<\/p>\n<p>{<\/p>\n<p>public static void main(String[] args) throws Exception {<\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">int c;<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">\/\/URL yahoo = new URL(&#8220;http:\/\/java.sun.com\/docs\/books\/tutorial\/&#8221;); URL yahoo = new URL(&#8220;https:\/\/www.google.co.in\/&#8221;);<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">URLConnection yc = yahoo.openConnection();<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">long d = yc.getDate();<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">if(d==0)<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">System.out.println(&#8220;no date info&#8221;);<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">else<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">System.out.println(&#8220;Date:&#8221; + new Date(d)); System.out.println(&#8220;content type&#8221; + yc.getContentType()); d = yc.getExpiration();<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">if(d==0)<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">System.out.println(&#8220;no expiration info&#8221;);<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">else<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">System.out.println(&#8220;Expires&#8221; + new Date(d));<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">d = yc.getLastModified();<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">if(d==0)<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">System.out.println(&#8220;no last modified info&#8221;);<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">else<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">System.out.println(&#8220;last modified&#8221;+ new Date(d))<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">int len = yc.getContentLength();<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">if(len == -1)<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">System.out.println(&#8220;content length unavailable&#8221;); else<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">s<\/span><span style=\"text-align: initial;font-size: 1em\">ystem.out.println(&#8220;content length&#8221; + len);<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">if(len!=0)<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">{<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">System.out.println(&#8220;===content===&#8221;);<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">InputStream input = yc.getInputStream();<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">while((( c= input.read()) != -1))<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">{<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">System.out.print((char) c);<\/span><\/p>\n<p>}<\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">input.close();<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">}<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">else<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">{<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">System.out.println(&#8220;no content available&#8221;);<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">}<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">}<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">}<\/span><\/p>\n<\/div>\n<p style=\"text-align: justify\">The output of demonstrating the instance methods of URLConnection is shown in Figure 21.3<\/p>\n<p>&nbsp;<\/p>\n<p><strong>Output of URL Connection<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: center\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-188 aligncenter\" src=\"http:\/\/csp12.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-108.png\" alt=\"\" width=\"509\" height=\"384\" srcset=\"https:\/\/ebooks.inflibnet.ac.in\/csp12\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-108.png 509w, https:\/\/ebooks.inflibnet.ac.in\/csp12\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-108-300x226.png 300w, https:\/\/ebooks.inflibnet.ac.in\/csp12\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-108-65x49.png 65w, https:\/\/ebooks.inflibnet.ac.in\/csp12\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-108-225x170.png 225w, https:\/\/ebooks.inflibnet.ac.in\/csp12\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-108-350x264.png 350w\" sizes=\"auto, (max-width: 509px) 100vw, 509px\" \/><\/p>\n<p>&nbsp;<\/p>\n<div>\n<p><strong>\u00a0 \u00a0 \u00a0 \u00a0URL Connections\/Sending Information<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">The browser allows the user to enter data in text fields and other GUI components. Like processing HTML forms, the browser writes the data to the URL. On the server a CGI-BIN script processes it and returns a response.<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\"><span style=\"text-align: initial;font-size: 1em\">The Web servers receive information from clients using either GET or POST. The request method is specified in the form as below,<\/span><\/p>\n<\/div>\n<div>\n<p>\u00a0 \u00a0 &lt;form method=\u201cpost\u201d action=&#8221;\/cgi-bin\/ipc\/idbsprd&#8221;&gt;<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">Appropriate CGI (common gateway interface) script is called to process information received and produce an HTML page to send back to client. CGI scripts are usually written in C, Perl, shellscript etc.<\/p>\n<p>&nbsp;<\/p>\n<p><strong>Send information to CGI script using GET<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">The &#8220;GET&#8221; request attaches the parameters to the end of URL as shown below,<a href=\"http:\/\/host\/script?parameters\">http:\/\/host\/script?parameters.<\/a><\/p>\n<p style=\"text-align: justify\">The parameters are seperated using \u201c&amp;\u201d and parameters are encoded as follows to avoid misinterpretation which is called as URL encoding.<\/p>\n<p>&nbsp;<\/p>\n<p>The URL encoding is done as follows,<\/p>\n<ul>\n<li>Replace space with \u201c+\u201d<\/li>\n<li style=\"text-align: justify\">Replace each non-alphanumeric character with \u201c%\u201d followed by the hexadecimal code of the character<\/li>\n<\/ul>\n<p>For example,<\/p>\n<p>\u201cMastering C++\u201d \u00e8 \u201cMastering+C%2b%2b\u201d<\/p>\n<p style=\"text-align: justify\">The disadvantage of this request type is long parameter string, might exceed limits of browsers and hence could not be sent.<\/p>\n<p>&nbsp;<\/p>\n<p><strong>Writing Information<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p>To write information to an URL resource, follow the steps as shown below,<\/p>\n<p>&nbsp;<\/p>\n<p><strong>Steps for working with java.net.URLConnection<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p><strong>1.<\/strong>\u00a0 <strong>Create URLConnection object from URL,<\/strong><\/p>\n<\/div>\n<div>\n<ul>\n<li>\u00a0URLConnection con = url.openConnection();<\/li>\n<\/ul>\n<p><strong>\u00a0 \u00a0 2.\u00a0<\/strong><strong>Set properties of connection:<\/strong><\/p>\n<ul>\n<li>setDoInPut(true) \/\/default<\/li>\n<li>setDoOutPut(true) for sending information to the server<\/li>\n<\/ul>\n<p><strong>\u00a0 \u00a03.\u00a0<\/strong><strong>Make connection:<\/strong><\/p>\n<ul>\n<li>con.connect();<\/li>\n<\/ul>\n<p><strong>\u00a0 \u00a04.\u00a0\u00a0<\/strong><strong>Query header information can be obtained by the following methods<\/strong><\/p>\n<ul>\n<li style=\"text-align: justify\"><strong>\u00a0<\/strong>getContentType, getContentLength, getContentEncoding, getDate, getExpiration, getLastModified<\/li>\n<\/ul>\n<p><strong>\u00a0 \u00a05.\u00a0<\/strong><strong>Use getInputStream() for reading and getOutputStream() for writing<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p><strong>Writing to a URLConnection<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">The example code demonstrates about how to write the reverse of a string to a URL connection. The input string received in the command line is reversed using URLEncoder. An URL object is created and an URLConnection object is created using the openConnection() method. The output mode of the URLConnection is set as true and the outputstream is obtained and the string is writtem to the resource. Then the contents are fetched again from the URL and displayed.<\/p>\n<p>&nbsp;<\/p>\n<p>import java.io.* ;<\/p>\n<p>import java.net.* ;<\/p>\n<p>public class Reverse {<\/p>\n<p>public static void main(String[]args) throws Exception<\/p>\n<p>{<\/p>\n<p>if (args.length&lt;1)<\/p>\n<p>{<\/p>\n<p>System.err.println(&#8220;Usage: java Reverse &lt;String&gt;&#8221;) ; System.exit(1) ;<\/p>\n<p>}<\/p>\n<p>String stringToReverse = URLEncoder.encode(args[0],&#8221;UTF-8&#8243;) ;<\/p>\n<p>&nbsp;<\/p>\n<p>URL url = new URL(&#8220;http:\/\/java.sun.com\/cgi -bin\/backwards&#8221;) ; URLConnection uc = url.openConnection() ; uc.setDoOutput(true) ;<\/p>\n<p>PrintWriter out = new PrintWriter(uc.getOutputStream()) ;<\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">out.println(&#8220;string=&#8221;+stringToReverse) ;<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">out.close() ;<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">BufferedReader in = new BufferedReader(new\u00a0<\/span><span style=\"text-align: initial;font-size: 1em\">InputStreamReader(uc.getInputStream())) ;<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">String line ;<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">while ((line=in.readLine())!=null)<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">System.out.println(line) ;<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">}<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">}<\/span><\/p>\n<\/div>\n<p><strong>\u00a0 \u00a0 Summary<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">This module explains about communicating with web servers using URL and URLConnection class in Java. This module also discusses about how to retrieve information and send information to servers with examples.<\/p>\n<p>&nbsp;<\/p>\n<p><strong>Web Links<\/strong><\/p>\n<ul>\n<li>Herbert Schildt, &#8221; <em>Java: The Complete Reference&#8221;, 9th Edition,<\/em> Mcgraw-Hill, 2014.<\/li>\n<\/ul>\n","protected":false},"author":4,"menu_order":19,"template":"","meta":{"_acf_changed":false,"pb_show_title":"on","pb_short_title":"","pb_subtitle":"","pb_authors":["dr-m-vijayalakshmi"],"pb_section_license":""},"chapter-type":[],"contributor":[58],"license":[],"class_list":["post-183","chapter","type-chapter","status-publish","hentry","contributor-dr-m-vijayalakshmi"],"part":3,"_links":{"self":[{"href":"https:\/\/ebooks.inflibnet.ac.in\/csp12\/wp-json\/pressbooks\/v2\/chapters\/183","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ebooks.inflibnet.ac.in\/csp12\/wp-json\/pressbooks\/v2\/chapters"}],"about":[{"href":"https:\/\/ebooks.inflibnet.ac.in\/csp12\/wp-json\/wp\/v2\/types\/chapter"}],"author":[{"embeddable":true,"href":"https:\/\/ebooks.inflibnet.ac.in\/csp12\/wp-json\/wp\/v2\/users\/4"}],"version-history":[{"count":5,"href":"https:\/\/ebooks.inflibnet.ac.in\/csp12\/wp-json\/pressbooks\/v2\/chapters\/183\/revisions"}],"predecessor-version":[{"id":556,"href":"https:\/\/ebooks.inflibnet.ac.in\/csp12\/wp-json\/pressbooks\/v2\/chapters\/183\/revisions\/556"}],"part":[{"href":"https:\/\/ebooks.inflibnet.ac.in\/csp12\/wp-json\/pressbooks\/v2\/parts\/3"}],"metadata":[{"href":"https:\/\/ebooks.inflibnet.ac.in\/csp12\/wp-json\/pressbooks\/v2\/chapters\/183\/metadata\/"}],"wp:attachment":[{"href":"https:\/\/ebooks.inflibnet.ac.in\/csp12\/wp-json\/wp\/v2\/media?parent=183"}],"wp:term":[{"taxonomy":"chapter-type","embeddable":true,"href":"https:\/\/ebooks.inflibnet.ac.in\/csp12\/wp-json\/pressbooks\/v2\/chapter-type?post=183"},{"taxonomy":"contributor","embeddable":true,"href":"https:\/\/ebooks.inflibnet.ac.in\/csp12\/wp-json\/wp\/v2\/contributor?post=183"},{"taxonomy":"license","embeddable":true,"href":"https:\/\/ebooks.inflibnet.ac.in\/csp12\/wp-json\/wp\/v2\/license?post=183"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}