Java – 使用 URLConnection 类从 URL 中读取
URLConnection是一个抽象类,它的子类构成了用户应用程序和网络上任何资源之间的链接。我们可以使用它来读取/写入 URL 对象引用的任何资源。主要有两个子类扩展了 URLConnection 类
- HttpURLConnection:如果我们连接到任何使用“http”作为其协议的 URL,则使用 HttpURLConnection 类。
- JarURLConnection:但是,如果我们尝试建立与 Web 上的 jar 文件的连接,则使用 JarURLConnection。
一旦建立连接并且我们有一个 URLConnection 对象,我们可以使用它来读取或写入或获取有关页面/文件上次修改时间、内容长度等的更多信息。
但勉强获得状态信息并不是真实世界应用程序的真正动机。检索信息,对其进行处理,并将结果发送回服务器,或者只是显示从服务器检索到的所需信息,这是我们的目标。
插图:
小型应用程序,它要求用户提供电影名称,然后返回电影的“IMDb”评级或返回与该电影相关的所有链接。所有这些都可以使用 URLConnection 类来实现。
URLConnection 类的方法
Method | Action Performed |
---|---|
getContent() | Retrieves the content of the URLConnection |
getContentEncoding() | Returns the value of the content-encoding header field. |
getContentLength() | Returns the length of the content header field |
getDate() | Returns the value of date in the header field |
getHeaderFields() | Returns the map containing the values of various header fields in the HTTP header |
getHeaderField(int i) | Returns the value of the ith index of the header |
getHeaderField(String field) | Returns the value of the field named “field” in the header |
getInputStream() | Returns the input stream to this open connection been inside of OutputStream class |
getOutputStream() | Returns the output stream to this connection of OutputStream class |
openConnection() | Opens the connection to the specified URL. |
setAllowUserInteraction() | Setting this true means a user can interact with the page. The default value is true. |
setDefaultUseCaches() | Sets the default value of useCache field as the given value. |
setDoInput() | Sets if the user is allowed to take input or not |
setDoOutput() | Sets if the user is allowed to write on the page. The default value is false since most of the URLs don’t allow to write |
现在了解了上述过程中涉及的方法步骤
- URL 创建:使用给定的任何构造函数创建一个 URL 对象
- 创建对象:调用 openConnection() 调用来创建 URLConnection 的对象。
- 显示内容:要么使用上面创建的对象显示有关资源的信息,要么使用 getInputStream() 方法使用打开连接的 bufferedReader 和 InputStream 读取/写入文件的内容到控制台。
- 关闭流:完成后关闭 InputStream。
实现:让我们看一个示例程序,它使用上述方法显示标题字段,并将整个页面的源代码打印到控制台窗口。
Java
// Java Program to Illustrate Reading and Writing
// in URLConnection Class
// Importing required classes
import java.io.*;
import java.net.*;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
// Class
// URLConnectionclass
class GFG {
// main driver method
public static void main(String[] args)
{
// Try block to check for exceptions
try {
// Fetching URL by creating
URL url = new URL(
"https://www.geeksforgeeks.org/java");
// Opening the connection to the above URL
URLConnection urlcon = url.openConnection();
// Executing the below line would print the
// value of
// Allow User Interaction field.
// System.out.println(urlcon.getAllowUserInteraction());
// Executing the below line would print
// the value of Content Type field.
// System.out.println(urlcon.getContentType());
// Executing the below line would print the
// value
// of URL of the given connection.
// System.out.println(urlcon.getURL());
// Executing the below line would
// print the value of Do Input field.
// System.out.println(urlcon.getDoInput());
// Executing the below line would
// print the value of Do Output field.
// System.out.println(urlcon.getDoOutput());
// Executing the below line would
// print the value of Last Modified field.
// System.out.println(new
// Date(urlcon.getLastModified()));
// Executing the below line would
// print the value of Content Encoding field.
// System.out.println(urlcon.getContentEncoding());
// To get a map of all the fields of http header
Map > header
= urlcon.getHeaderFields();
// Printing all the fields along with their
// value
for (Map.Entry > mp :
header.entrySet()) {
System.out.print(mp.getKey() + " : ");
System.out.println(
mp.getValue().toString());
}
System.out.println();
System.out.println(
"Complete source code of the URL is-");
System.out.println(
"---------------------------------");
// Getting the inputstream of the open
// connection
BufferedReader br
= new BufferedReader(new InputStreamReader(
urlcon.getInputStream()));
String i;
// Printing the source code line by line
while ((i = br.readLine()) != null) {
System.out.println(i);
}
}
// Catch block to handle exceptions
catch (Exception e) {
// Displaying exceptions
System.out.println(e);
}
}
}
输出:
Keep-Alive : [timeout=5, max=100]
null : [HTTP/1.1 200 OK]
Server : [Apache/2.4.18 (Ubuntu)]
Connection : [Keep-Alive]
Last-Modified : [Wed, 16 Nov 2016 06:49:55 GMT]
Date : [Wed, 16 Nov 2016 10:58:34 GMT]
Accept-Ranges : [bytes]
Cache-Control : [max-age=3]
ETag : ["10866-541657b07e4d7"]
Vary : [Accept-Encoding]
Expires : [Wed, 16 Nov 2016 10:58:37 GMT]
Content-Length : [67686]
Content-Type :
Complete source code of the URL is-
--------------------------------------------------
...source code of the page...