By: Ivan Lim in Java Tutorials on 2022年09月15日 [フレーム]
URLConnectionis 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 URLobjects that are using the HTTP protocol. We'll examine the most useful elements of URLConnectionhere.
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.
This policy contains information about your privacy. By posting, you are declaring that you understand this policy:
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:
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.
Most Viewed Articles (in Java )
Step by Step guide to setup freetts for Java
Open a .docx file and show content in a TextArea using Java
concurrent.Flow instead of Observable class in Java
DateFormat sample program in Java
Simple Port Scanner application using Java
Using the AWS SDK for Java in Eclipse
Read a file having a list of telnet commands and execute them one by one using Java
Calculator application in Java
Latest Articles (in Java)
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)
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)
© 2023 Java-samples.com
Tutorial Archive: Data Science React Native Android AJAX ASP.net C C++ C# Cocoa Cloud Computing EJB Errors Java Certification Interview iPhone Javascript JSF JSP Java Beans J2ME JDBC Linux Mac OS X MySQL Perl PHP Python Ruby SAP VB.net EJB Struts Trends WebServices XML Office 365 Hibernate
Latest Tutorials on: Data Science React Native Android AJAX ASP.net C Cocoa C++ C# EJB Errors Java Certification Interview iPhone Javascript JSF JSP Java Beans J2ME JDBC Linux Mac OS X MySQL Perl PHP Python Ruby SAP VB.net EJB Struts Cloud Computing WebServices XML Office 365 Hibernate