Course
URL Processing
Java Tutorial
This Java tutorial is tailored for newcomers, offering a journey from basic principles to complex Java programming techniques. Completing this tutorial equips you with a solid understanding of Java, preparing you for advanced learning. You'll emerge ready to tackle the challenges of becoming a top-tier software engineer, with the skills to innovate and excel in the vast world of software development.
URL Processing
URL stands for Uniform Resource Locator and represents a resource on the World Wide Web, such as a Web page or FTP directory.
This section shows you how to write Java programs that communicate with a URL. A URL can be broken down into parts, as follows
protocol://host:port/path?query#ref
The following is a URL to a web page whose protocol is HTTP
https://www.amrood.com/index.htm?language=en#j2se
Notice that this URL does not specify a port, in which case the default port for the protocol is used. With HTTP, the default port is 80.
Constructors
The java.net.URL class represents a URL and has a complete set of methods to manipulate URL in Java.
The URL class has several constructors for creating URLs, including the following
The URL class contains many methods for accessing the various parts of the URL being represented. Some of the methods in the URL class include the following
Example
The following URLDemo program demonstrates the various parts of a URL. A URL is entered on the command line, and the URLDemo program outputs each part of the given URL.
// File Name : URLDemo.javaimport java.io.IOException;import java.net.URL;
public class URLDemo {
public static void main(String [] args) { try { URL url = new URL("https://www.tutorialspoint.com/index.htm?language=en#j2se"); 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(); } }}
A sample run of the this program will produce the following result
Output
URL is https://www.tutorialspoint.com/index.htm?language=en#j2seprotocol is httpsauthority is www.tutorialspoint.comfile name is /index.htm?language=enhost is www.tutorialspoint.compath is /index.htmport is -1default port is 443query is language=enref is j2se
URLConnections Class Methods
The openConnection() method returns a java.net.URLConnection, an abstract class whose subclasses represent the various types of URL connections.
For example −
- If you connect to a URL whose protocol is HTTP, the openConnection() method returns an HttpURLConnection object.
- If you connect to a URL that represents a JAR file, the openConnection() method returns a JarURLConnection object, etc.
The URLConnection class has many methods for setting or determining information about the connection, including the following
Example
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 one line at a time.
package com.tutorialspoint;
import java.io.BufferedReader;import java.io.IOException;import java.io.InputStreamReader;import java.net.HttpURLConnection;import java.net.URL;import java.net.URLConnection;
public class URLConnDemo { public static void main(String [] args) { try { URL url = new URL("https://www.tutorialspoint.com"); URLConnection urlConnection = url.openConnection(); HttpURLConnection connection = null; if(urlConnection instanceof HttpURLConnection) { connection = (HttpURLConnection) urlConnection; }else { System.out.println("Please enter an HTTP URL."); return; } BufferedReader in = new BufferedReader( new InputStreamReader(connection.getInputStream())); String urlString = ""; String current; while((current = in.readLine()) != null) { urlString += current; } System.out.println(urlString); } catch (IOException e) { e.printStackTrace(); } }}
A sample run of this program will produce the following result
Output
$ java URLConnDemo
.....a complete HTML content of home page of tutorialspoint.com.....