Java URL 处理

说明

Java URL openConnection() 方法返回一个 URLConnection 实例,表示与 URL 引用的远程对象的连接。每次调用此 URL 的协议处理程序的 URLStreamHandler.openConnection(URL) 方法时,都会创建一个新的 URLConnection 实例。当 URLConnection.connect() 方法被调用时, URLConnection 实例建立实际的网络连接。对于 HTTP 协议,将返回 HttpURLConnection,对于 JAR,将返回 JarURLConnection。

声明

以下是 java.net.URL.openConnection( ) 方法

public URLConnection openConnection() 

参数

返回值

链接到 URL 的 URLConnection。

Exception

IOException - 如果发生 I/O 异常。

示例 1

以下示例显示用法Java URL openConnection() 方法用于使用 https 协议的有效 url。在此示例中,我们将创建 URL 类的实例。现在使用 openConnection() 方法,我们获取 URLConnection 对象。接下来,我们使用 URLConnection.getInputStream() 获取 url 指向的网站页面的内容并打印相应的内容 -

package com.yxjc123;

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 UrlDemo {
   public static void main(String [] args) {
      try {
         URL url = new URL("https://www.yxjc123.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();
      }
   }
} 

让我们编译并运行上面的程序,这将产生以下结果 -

输出

<!DOCTYPE html><html lang="en"><head><title>Online Tutorials, Courses,...</body></html> 

示例 2

以下示例显示了有效 url 的 Java URL openConnection() 方法的用法。在此示例中,我们使用 http 协议创建 URL 类的实例。现在使用 openConnection() 方法,我们获取 URLConnection 对象。接下来,我们使用 URLConnection.getInputStream() 获取 url 指向的网站页面的内容并打印相应的内容 -

package com.yxjc123;

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 UrlDemo {
   public static void main(String [] args) {
      try {
         URL url = new URL("http","www.yxjc123.com","/index.htm");
         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();
      }
   }
} 

让我们编译并运行上面的程序,这将产生以下结果 -

输出

<!DOCTYPE html><html lang="en"><head><title>Online Tutorials, Courses,...</body></html> 

示例 3

以下示例显示了有效 url 的 Java URL openConnection() 方法的用法与文件协议。在此示例中,我们将创建 URL 类的实例。现在使用 openConnection() 方法,我们获取 URLConnection 对象。接下来,我们使用 URLConnection.getInputStream() 来获取 url 指向的网站页面的内容。由于协议不是 http 或 https,程序将打印一条消息 请输入 HTTP URL。 并终止,如下所示 -

package com.yxjc123;

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 UrlDemo {
   public static void main(String [] args) {
      try {
         URL url = new URL("file","www.yxjc123.com","/index.htm");
         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();
      }
   }
} 

让我们编译并运行上面的程序,这将产生以下结果 -

输出

Please enter an HTTP URL.