Java URL
In Java, the URL class is part of the java.net package and is used to represent a Uniform Resource Locator (URL). A URL is a reference or address used to locate resources on the internet or within a local network. URLs typically consist of a protocol, hostname (or IP address), port, path, and query parameters.
Here’s how to use the URL class in Java to work with URLs:
Import the
URLclass:First, make sure to import the
java.net.URLclass at the beginning of your Java source file:javaimport java.net.URL; import java.net.MalformedURLException;Create a
URLobject:You can create a
URLobject by passing a URL string to its constructor. Handle theMalformedURLExceptionin case the URL string is malformed:javatry { String urlString = "https://www.example.com"; URL url = new URL(urlString); } catch (MalformedURLException e) { e.printStackTrace(); }Replace
"https://www.example.com"with the URL you want to work with.Access URL Components:
You can access various components of the URL using methods provided by the
URLclass. Here are some common methods:getProtocol(): Returns the protocol (e.g., “http”, “https”).getHost(): Returns the host or hostname (e.g., “www.example.com“).getPort(): Returns the port number (-1 if not specified).getPath(): Returns the path portion of the URL.getQuery(): Returns the query string (if present).getFile(): Returns the file portion of the URL, including the path and query.
Example:
javaString protocol = url.getProtocol(); String host = url.getHost(); int port = url.getPort(); String path = url.getPath(); String query = url.getQuery();Open a Connection:
You can use the
openConnection()method of theURLobject to open a connection to the resource specified by the URL. This is often used for reading from or writing to a URL. Here’s an example usingHttpURLConnection:javaimport java.io.BufferedReader; import java.io.InputStreamReader; import java.net.HttpURLConnection; import java.net.URL; try { URL url = new URL("https://www.example.com"); HttpURLConnection connection = (HttpURLConnection) url.openConnection(); connection.setRequestMethod("GET"); int responseCode = connection.getResponseCode(); if (responseCode == HttpURLConnection.HTTP_OK) { BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream())); String line; while ((line = reader.readLine()) != null) { System.out.println(line); } reader.close(); } else { System.out.println("HTTP Error: " + responseCode); } connection.disconnect(); } catch (Exception e) { e.printStackTrace(); }In this example, we open an HTTP connection to the specified URL and read its content.
The URL class in Java is a powerful tool for working with URLs, allowing you to parse, construct, and connect to resources on the internet or local network. Depending on your use case, you may need to handle different protocols and perform various operations with the URL.
Demo Day 1 Video:
Conclusion:
Unogeeks is the No.1 Training Institute for Java Training. Anyone Disagree? Please drop in a comment
You can check out our other latest blogs on Java Training here – Java Blogs
You can check out our Best in Class Java Training details here – Java Training
Follow & Connect with us:
———————————-
For Training inquiries:
Call/Whatsapp: +91 73960 33555
Mail us at: info@unogeeks.com
Our Website ➜ https://unogeeks.com
Follow us:
Instagram: https://www.instagram.com/unogeeks
Facebook: https://www.facebook.com/UnogeeksSoftwareTrainingInstitute
Twitter: https://twitter.com/unogeeks