URLConnection sample program in Java
By: Ivan Lim in Java Tutorials on 2022-09-15
URLConnection is a general-purpose class for accessing the attributes of a remote resource. Once you make a connection to a remote server, you can use URLConnection 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. We'll examine the most useful elements of URLConnection here.
Here's an example program that demonstrates the use of URLConnection
in Java:
import java.net.*; import java.io.*; public class URLConnectionDemo { public static void main(String[] args) { try { URL url = new URL("https://example.com/"); URLConnection urlConn = url.openConnection(); // Set request properties urlConn.setRequestProperty("User-Agent", "Mozilla/5.0"); // Get content type String contentType = urlConn.getContentType(); System.out.println("Content Type: " + contentType); // Get content length int contentLength = urlConn.getContentLength(); System.out.println("Content Length: " + contentLength); // Get last modified date long lastModified = urlConn.getLastModified(); System.out.println("Last Modified: " + lastModified); // Get input stream InputStream input = urlConn.getInputStream(); // Read the content of the URL int c; while ((c = input.read()) != -1) { System.out.print((char) c); } input.close(); } catch (IOException e) { System.err.println(e); } } }
In this example, we create a URL
object for the URL we want to access, then create a URLConnection
object by calling the openConnection()
method of the URL
object.
We set some request properties on the URLConnection
object, such as the user agent string, then retrieve some information about the content we're accessing, such as the content type, content length, and last modified date.
Finally, we get an input stream from the URLConnection
object and read the content of the URL using a loop that reads each character from the input stream and prints it to the console.
Note that we wrap the entire code in a try-catch block to handle any IOException
that may be thrown during the process of accessing the URL.
Add Comment
This policy contains information about your privacy. By posting, you are declaring that you understand this policy:
- Your name, rating, website address, town, country, state and comment will be publicly displayed if entered.
- Aside from the data entered into these form fields, other stored data about your comment will include:
- Your IP address (not displayed)
- The time/date of your submission (displayed)
- Your email address will not be shared. It is collected for only two reasons:
- Administrative purposes, should a need to contact you arise.
- To inform you of new comments, should you subscribe to receive notifications.
- A cookie may be set on your computer. This is used to remember your inputs. It will expire by itself.
This policy is subject to change at any time and without notice.
These terms and conditions contain rules about posting comments. By submitting a comment, you are declaring that you agree with these rules:
- Although the administrator will attempt to moderate comments, it is impossible for every comment to have been moderated at any given time.
- You acknowledge that all comments express the views and opinions of the original author and not those of the administrator.
- You agree not to post any material which is knowingly false, obscene, hateful, threatening, harassing or invasive of a person's privacy.
- The administrator has the right to edit, move or remove any comment for any reason and without notice.
Failure to comply with these rules may result in being banned from submitting further comments.
These terms and conditions are subject to change at any time and without notice.
- Data Science
- Android
- React Native
- AJAX
- ASP.net
- C
- C++
- C#
- Cocoa
- Cloud Computing
- HTML5
- Java
- Javascript
- JSF
- JSP
- J2ME
- Java Beans
- EJB
- JDBC
- Linux
- Mac OS X
- iPhone
- MySQL
- Office 365
- Perl
- PHP
- Python
- Ruby
- VB.net
- Hibernate
- Struts
- SAP
- Trends
- Tech Reviews
- WebServices
- XML
- Certification
- Interview
categories
Related Tutorials
Read a file having a list of telnet commands and execute them one by one using Java
Open a .docx file and show content in a TextArea using Java
Step by Step guide to setup freetts for Java
Of Object, equals (), == and hashCode ()
Using the AWS SDK for Java in Eclipse
DateFormat sample program in Java
concurrent.Flow instead of Observable class in Java
Calculator application in Java
Sending Email from Java application (using gmail)
Comments