19 Java Networking
Dr M. Vijayalakshmi
URL Connections
URL stands for Uniform Resource Locator
It is a scheme used for uniquely identifying all kinds of network resources.
The basic form of a URL looks like,
<pr otoc ol>:<site name ><pathname >
Some examples are,
http://www.cs.ust.hk/~liao/comp201/index.html
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.
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.
URL Connections/Retrieve Information
Open connection with java.net.URL
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.
URL url1 = new URL(“http://www.cs.ust.hk/~liao/comp201/index.html”);
Then to fetch contents of the resource, use the openStream method of URL, which returns an InputStream.
InputStream in1 = url1.openStream();
Now we can nest the InputStreams with other Java streams to retrieve the contents from the URL.
Example of Read stream
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.
import java.net.*;
import java.io.*;
public class ReadStream
{
public static void main(String args[]) throws Exception
{
try{
URL u = new URL(“https://www.google.co.in “); InputStream in = u.openStream();
BufferedReader bin = new BufferedReader(new InputStreamReader(in)); String temp;
while(( temp=bin.readLine()) != null)
{
System.out.println(temp);
}
}catch(IOExce ption e)
{
System.out.println(“Socket error”);
}
}
}
The output of this code is shown in Figure 21.1.
URLConnection
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.
HttpURLConnection
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.
Communicating with Web Servers
Class java.net.URL represents a Uniform Resource Locator. Create an Java object that represents an URL using the constructor of the URL class.
URL url = new URL(“http://www.cs.ust.hk/~lzhang/comp201/index.html”);
The instance methods of the URL class are etc.
This java.net.URLConnection represents a communication link between the application and a URL.
We can use the Constructor of this class to create an URLConnection object.
URLConnection con = new URLConnection( url).
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.
URLConnection con = url.openConnection();
URLConnection
We can directly open a stream for reading using the URL class. The signature of the openStream() method is given below,public final
InputStream openStream() throws IOException
InputStream in = u.openStream();
This opens a connection to this URL and returns an InputStream for reading from that connection. This method is a shorthand for,
openConnection().getInputStream()
Connecting to a URL using URLConnection
We can connect to URL using the URL’s openConnection() method that returns an URLConnection object. The signature is given below,URLConnection openConnection() throws IOExce ption ;
Example:
try {
URL url = new URL(“http://www.cs.biu.ac.il/~freskom1/”) ; URLConnection uc = url.openConnection() ;
}
catch (MalformedURLException e)
{ … }
catch (IOException e)
{ … }
Reading from a URL connection
To read from the URL connection, we first create an URL object using the constrcutor of the URL class. Then we can use the openConnection() method of the URL class that returns an URLConnection object. The method getInputStream() of this URLConnection object returns an InputStream that can be wrapped with other streams to read data from the URL.
import java.net.* ;
import java.io.* ;
public class URLConnectionReader1
{
public static void main(String[] args) throws Exception
{
URL url = new URL(“http://www.google.com”) ; URLConnection uc = url.openConnection() ; BufferedReader br=new BufferedReader(new InputStreamReader(uc.getInputStream()));
String line ;
while ((line=br.readLine())!=null)
System.out.println(line) ;
br.close() ;
}
}
The output of reading from URL using URLConnection is given in the following Figure 21.2.
Output of Reading from a URL connection
Instance Methods of URLConnection
Some of the instance methods of the URLConnection class is demonstrated in this example code. The method getDate() prints the time and date of the response in milliseconds. The method getContentT ype() prints the type of content found in the resource. The method getLastModified() prints the time and date when the resource was last modified in milliseconds. The method getExpiration() prints the expiration time and date of the resource in milliseconds. The method getContentLength() 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.
import java.net.*;
import java.io.*;
import java.util.Date;
public class URLConnectionReade r
{
public static void main(String[] args) throws Exception {
int c;
//URL yahoo = new URL(“http://java.sun.com/docs/books/tutorial/”); URL yahoo = new URL(“https://www.google.co.in/”);
URLConnection yc = yahoo.openConnection();
long d = yc.getDate();
if(d==0)
System.out.println(“no date info”);
else
System.out.println(“Date:” + new Date(d)); System.out.println(“content type” + yc.getContentType()); d = yc.getExpiration();
if(d==0)
System.out.println(“no expiration info”);
else
System.out.println(“Expires” + new Date(d));
d = yc.getLastModified();
if(d==0)
System.out.println(“no last modified info”);
else
System.out.println(“last modified”+ new Date(d))
int len = yc.getContentLength();
if(len == -1)
System.out.println(“content length unavailable”); else
system.out.println(“content length” + len);
if(len!=0)
{
System.out.println(“===content===”);
InputStream input = yc.getInputStream();
while((( c= input.read()) != -1))
{
System.out.print((char) c);
}
input.close();
}
else
{
System.out.println(“no content available”);
}
}
}
The output of demonstrating the instance methods of URLConnection is shown in Figure 21.3
Output of URL Connection
URL Connections/Sending Information
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.
The Web servers receive information from clients using either GET or POST. The request method is specified in the form as below,
<form method=“post” action=”/cgi-bin/ipc/idbsprd”>
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.
Send information to CGI script using GET
The “GET” request attaches the parameters to the end of URL as shown below,http://host/script?parameters.
The parameters are seperated using “&” and parameters are encoded as follows to avoid misinterpretation which is called as URL encoding.
The URL encoding is done as follows,
- Replace space with “+”
- Replace each non-alphanumeric character with “%” followed by the hexadecimal code of the character
For example,
“Mastering C++” è “Mastering+C%2b%2b”
The disadvantage of this request type is long parameter string, might exceed limits of browsers and hence could not be sent.
Writing Information
To write information to an URL resource, follow the steps as shown below,
Steps for working with java.net.URLConnection
1. Create URLConnection object from URL,
- URLConnection con = url.openConnection();
2. Set properties of connection:
- setDoInPut(true) //default
- setDoOutPut(true) for sending information to the server
3. Make connection:
- con.connect();
4. Query header information can be obtained by the following methods
- getContentType, getContentLength, getContentEncoding, getDate, getExpiration, getLastModified
5. Use getInputStream() for reading and getOutputStream() for writing
Writing to a URLConnection
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.
import java.io.* ;
import java.net.* ;
public class Reverse {
public static void main(String[]args) throws Exception
{
if (args.length<1)
{
System.err.println(“Usage: java Reverse <String>”) ; System.exit(1) ;
}
String stringToReverse = URLEncoder.encode(args[0],”UTF-8″) ;
URL url = new URL(“http://java.sun.com/cgi -bin/backwards”) ; URLConnection uc = url.openConnection() ; uc.setDoOutput(true) ;
PrintWriter out = new PrintWriter(uc.getOutputStream()) ;
out.println(“string=”+stringToReverse) ;
out.close() ;
BufferedReader in = new BufferedReader(new InputStreamReader(uc.getInputStream())) ;
String line ;
while ((line=in.readLine())!=null)
System.out.println(line) ;
}
}
Summary
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.
Web Links
- Herbert Schildt, ” Java: The Complete Reference”, 9th Edition, Mcgraw-Hill, 2014.