Understanding URLs in Java: Communicating with Web Resources

Learn how to work with URLs in Java, a crucial part of web communication. This guide covers URL structure, syntax, and examples for accessing online resources, with explanations on using Java classes for handling and parsing URLs effectively.



Understanding URLs in Java

A URL (Uniform Resource Locator) represents a resource on the World Wide Web, such as a web page or an FTP directory. This section explains how to write Java programs that communicate with a URL. A URL can be divided into parts:

URL Structure

protocol://host:port/path?query#ref

Examples of protocols include HTTP, HTTPS, FTP, and File. The path is often referred to as the filename, while the host is also known as the authority.

Example URL

Here is an example of a URL with the HTTP protocol:

https://www.example.com/index.htm?language=en#section1

Note that this URL does not specify a port. In such cases, the default port for the protocol is used. For HTTP, the default port is 80.

Constructors of the URL Class

The java.net.URL class represents a URL and provides various methods to manipulate URLs in Java. Below are some of its constructors:

Sr.No. Constructors & Description
1 public URL(String protocol, String host, int port, String file) throws MalformedURLException

Creates a URL by assembling the given parts.

2 public URL(String protocol, String host, String file) throws MalformedURLException

Similar to the previous constructor, but uses the default port for the given protocol.

3 public URL(String url) throws MalformedURLException

Creates a URL from the provided String.

4 public URL(URL context, String url) throws MalformedURLException

Creates a URL by parsing the URL and String arguments.

Methods of the URL Class

The URL class contains several methods that help in accessing different parts of a URL. Below are some of the key methods:

Sr.No. Method & Description
1 public equals(Object obj)
This method compares this URL for equality with another object.
2 public String getAuthority()
This method returns the authority of the URL.
3 public Object getContent()
This method returns the contents of this URL.
4 public Object getContent(Class[] classes)
This method returns the contents of this URL.
5 public int getDefaultPort()
This method returns the default port for the protocol of the URL.
6 public String getFile()
This method returns the filename of the URL.
7 public String getHost()
This method returns the host of the URL.
8 public String getPath()
This method returns the path of the URL.
9 public int getPort()
This method returns the port of the URL.
10 public String getProtocol()
This method returns the protocol of the URL.
11 public String getQuery()
This method returns the query part of the URL.
12 public String getRef()
This method returns the reference part of the URL.
13 public String getUserInfo()
This method returns the userInfo part of the URL.
14 public int hashCode()
This method creates and returns an integer suitable for hash table indexing.
15 public URLConnection openConnection()
This method returns a URLConnection instance that represents a connection to the remote object referred to by the URL.
16 public URLConnection openConnection(Proxy proxy)
This method acts as openConnection(), except that the connection will be made through the specified proxy.
17 public InputStream openStream()
This method opens a connection to this URL and returns an InputStream for reading from that connection.
18 public boolean sameFile(URL other)
This method compares two URLs, excluding the fragment component.
19 public static void setURLStreamHandlerFactory(URLStreamHandlerFactory fac)
This method sets an application's URLStreamHandlerFactory.
20 public String toExternalForm()
This method constructs and returns a string representation of this URL.
21 public String toString()
This method constructs and returns a string representation of this URL.
22 public String toURI()
This method returns a URI equivalent to this URL.

Example: URLDemo Program

The following URLDemo program demonstrates the various parts of a URL. The user inputs a URL from the command line, and the program outputs each part of the given URL.

Syntax

import java.io.IOException;
import java.net.URL;

public class URLDemo {
public static void main(String [] args) {
try {
    URL url = new URL("https://www.example.com/");
    
    System.out.println("URL is " + url.toString());
    System.out.println("protocol is " + url.getProtocol());
    System.out.println("authority is " + url.getAuthority());
    System.out.println("file name is " + url.getFile());
    System.out.println("host is " + url.getHost());
    System.out.println("path is " + url.getPath());
    System.out.println("port is " + url.getPort());
    System.out.println("default port is " + url.getDefaultPort());
    System.out.println("query is " + url.getQuery());
    System.out.println("ref is " + url.getRef());
} catch (IOException e) {
    e.printStackTrace();
}
}
}
Output

URL is https://www.example.com/
protocol is https
authority is www.example.com
file name is /index.htm
host is www.example.com
path is /index.htm
port is -1
default port is 443
query is null
ref is null

URLConnections Class Methods

The openConnection() method returns a java.net.URLConnection, an abstract class whose subclasses represent different types of URL connections. For example:

  • If the protocol is HTTP, openConnection() returns an HttpURLConnection object.
  • If the URL represents a JAR file, openConnection() returns a JarURLConnection object.

Methods in URLConnection

The URLConnection class has numerous methods that allow for the manipulation and retrieval of data related to the URL connection. Below is a list of essential methods:

Sr.No. Method & Description
1 void addRequestProperty(String key, String value)
Adds a general request property specified by a key-value pair.
2 boolean getAllowUserInteraction()
Returns the value of the allowUserInteraction field for this object.
3 int getConnectTimeout()
Returns setting for connect timeout.
4 Object getContent()
Retrieves the contents of this URL connection.
5 Object getContent(Class[] classes)
Retrieves the contents of this URL connection.
6 String getContentEncoding()
Returns the value of the content-encoding header field.
7 int getContentLength()
Returns the value of the content-length header field.
8 long getContentLengthLong()
Returns the value of the content-length header field as long.
9 String getContentType()
Returns the value of the content-type header field.
10 long getDate()
Returns the value of the date header field.
11 static boolean getDefaultAllowUserInteraction()
Returns the default value of the allowUserInteraction field.
12 boolean getDefaultUseCaches()
Returns the default value of a URLConnection's useCaches flag.
13 static boolean getDefaultUseCaches(String protocol)
Returns the default value of the useCaches flag for the given protocol.
14 boolean getDoInput()
Returns the value of this URLConnection's doInput flag.
15 boolean getDoOutput()
Returns the value of this URLConnection's doOutput flag.
16 long getExpiration()
Returns the value of the expires header field.
17 static FileNameMap getFileNameMap()
Loads filename map (a mimetable) from a data file.
18 String getHeaderField(int n)
Returns the value for the nth header field.
19 String getHeaderField(String name)
Returns the value of the named header field.
20 long getHeaderFieldDate(String name, long Default)
Returns the value of the named field parsed as date.
21 int getHeaderFieldInt(String name, int Default)
Returns the value of the named field parsed as a number.
22 String getHeaderFieldKey(int n)
Returns the key for the nth header field.
23 long getHeaderFieldLong(String name, long Default)
Returns the value of the named field parsed as a number.
24 Map> getHeaderFields()
Returns an unmodifiable Map of the header fields.
25 long getIfModifiedSince()
Returns the value of this object's ifModifiedSince field.
26 InputStream getInputStream()
Returns an input stream that reads from this open connection.
27 int getLastModified()
Returns the value of the last-modified header field.
28 OutputStream getOutputStream()
Returns an output stream that writes to this connection.
29 Permission getPermission()
Returns a permission object representing the permission necessary to make the connection represented by this object.
30 int getReadTimeout()
Returns setting for read timeout. A return value of 0 implies that the option is disabled (i.e., timeout of infinity).
31 Map> getRequestProperties()
Returns an unmodifiable Map of general request properties for this connection.
32 String getRequestProperty(String key)
Returns the value of the named general request property for this connection.
33 URL getURL()
Returns the value of this URLConnection's URL field.
34 boolean getUseCaches()
Returns the value of this URLConnection's useCaches field.
35 static String guessContentTypeFromName(String fname)
Tries to determine the content type of an object, based on the specified "file" component of a URL.
36 static String guessContentTypeFromStream(InputStream is)
Tries to determine the type of an input stream based on the characters at the beginning of the input stream.
37 void setAllowUserInteraction(boolean allowuserinteraction)
Sets the value of the allowUserInteraction field of this URLConnection.
38 void setConnectTimeout(int timeout)
Sets a specified timeout value, in milliseconds, to be used when opening a communications link to the resource referenced by this URLConnection.
39 static void setContentHandlerFactory(ContentHandlerFactory fac)
Sets the ContentHandlerFactory of an application.
40 static void setDefaultAllowUserInteraction(boolean defaultallowuserinteraction)
Sets the default value of the allowUserInteraction field for all future URLConnection objects to the specified value.
41 void setDefaultUseCaches(boolean defaultusecaches)
Sets the default value of the useCaches field to the specified value.
42 static void setDefaultUseCaches(String protocol, boolean defaultVal)
Sets the default value of the useCaches field for the named protocol to the given value.
43 void setDoInput(boolean doinput)
Sets the value of the doInput field for this URLConnection to the specified value.
44 void setDoOutput(boolean dooutput)
Sets the value of the doOutput field for this URLConnection to the specified value.
45 static void setFileNameMap(FileNameMap map)
Sets the FileNameMap.
46 void setIfModifiedSince(long ifmodifiedsince)
Sets the value of the ifModifiedSince field of this URLConnection to the specified value.
47 void setReadTimeout(int timeout)
Sets the read timeout to a specified timeout, in milliseconds.
48 void setRequestProperty(String key, String value)
Sets the general request property.
49 void setUseCaches(boolean usecaches)
Sets the value of the useCaches field of this URLConnection to the specified value.
50 String toString()
Returns a String representation of this URL connection.

Example: URLConnectionDemo Program

The following URLConnectionDemo program connects to a URL entered from the command line. If the URL represents an HTTP resource, the connection is cast to HttpURLConnection, and the data in the resource is read line by line.

Syntax

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;

public class URLConnectionDemo {
public static void main(String[] args) throws Exception {
String urlString = "https://www.example.com/";
URL url = new URL(urlString);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();

connection.setRequestMethod("GET");
BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
String inputLine;
StringBuffer response = new StringBuffer();

while ((inputLine = in.readLine()) != null) {
    response.append(inputLine);
}
in.close();

System.out.println("Response: " + response.toString());
}
}
Output

Response: <!doctype html>
<html>
...
</html>

Conclusion

In this tutorial, we covered the basics of using URLs in Java, including the structure of URLs, constructors and methods of the URL and URLConnection classes, and provided examples to illustrate their usage. Understanding how to work with URLs is essential for network programming in Java.