Java 教程

Java 控制語句

面向物件程式設計

Java 內建類

Java 檔案處理

Java 錯誤和異常

Java 多執行緒

Java 同步

Java 網路程式設計

Java 集合

Java 介面

Java 資料結構

Java 集合演算法

高階 Java

Java 雜項

Java APIs 和框架

Java 類參考

Java 有用資源

Java - URLConnection getContent() 方法及示例



描述

Java URLConnection getContent() 方法檢索此 URL 連線的內容。此方法首先透過呼叫 getContentType 方法確定物件的 內容型別。如果這是應用程式第一次看到該特定內容型別,則會建立該內容型別的 內容處理程式。

宣告

以下是 java.net.URLConnection.getContent() 方法的宣告

public Object getContent() throws IOException

引數

返回值

獲取到的物件。應使用 instanceof 運算子來確定返回物件的具體型別。

異常

IOException − 獲取內容時發生 I/O 錯誤。

UnknownServiceException − 協議不支援內容型別。

示例 1

以下示例演示瞭如何使用具有 https 協議的有效 url 的 Java URLConnection getContent() 方法。在此示例中,我們正在建立 URL 類的例項。使用 url.openConnection() 方法,我們正在獲取 URLConnection 例項。使用 getContent(),我們正在獲取網站主頁的內容並列印它。

package com.tutorialspoint;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;

public class UrlConnectionDemo {
   public static void main(String [] args) {
      try {
         URL url = new URL("https://tutorialspoint.tw/index.htm?language=en#j2se");
         URLConnection urlConnection = url.openConnection();

         BufferedReader in = new BufferedReader(
            new InputStreamReader((InputStream) urlConnection.getContent()));
         String content = "";
         String current;
         while((current = in.readLine()) != null) {
            content += current;
         }
         System.out.println(content);
      } catch (IOException e) {
         e.printStackTrace();
      }
   }
}

讓我們編譯並執行上述程式,這將產生以下結果:

輸出

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

示例 2

以下示例演示瞭如何使用具有 http 協議的有效 url 的 Java URLConnection getContent() 方法。在此示例中,我們正在建立 URL 類的例項。使用 url.openConnection() 方法,我們正在獲取 URLConnection 例項。使用 getContent(),我們正在獲取網站主頁的內容並列印它。

package com.tutorialspoint;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;

public class UrlConnectionDemo {
   public static void main(String [] args) {
      try {
         URL url = new URL("https://tutorialspoint.tw/index.htm?language=en#j2se");
         URLConnection urlConnection = url.openConnection();

         BufferedReader in = new BufferedReader(
            new InputStreamReader((InputStream) urlConnection.getContent()));
         String content = "";
         String current;
         while((current = in.readLine()) != null) {
            content += current;
         }
         System.out.println(content);
      } catch (IOException e) {
         e.printStackTrace();
      }
   }
}

讓我們編譯並執行上述程式,這將產生以下結果:

輸出

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

示例 3

以下示例演示瞭如何使用具有 http 協議的 google 的有效 url 的 Java URLConnection getContent() 方法。在此示例中,我們正在建立 URL 類的例項。使用 url.openConnection() 方法,我們正在獲取 URLConnection 例項。使用 getContent(),我們正在獲取網站主頁的內容並列印它。

package com.tutorialspoint;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;

public class UrlConnectionDemo {
   public static void main(String [] args) {
      try {
         URL url = new URL("http://www.google.com");
         URLConnection urlConnection = url.openConnection();

         BufferedReader in = new BufferedReader(
            new InputStreamReader((InputStream) urlConnection.getContent()));
         String content = "";
         String current;
         while((current = in.readLine()) != null) {
            content += current;
         }
         System.out.println(content);
      } catch (IOException e) {
         e.printStackTrace();
      }
   }
}

讓我們編譯並執行上述程式,這將產生以下結果:

輸出

<!doctype html><html itemscope="" itemtype="http://schema.org/WebPage" ....</body></html>
java_urlconnection.htm
廣告
© . All rights reserved.