Java 教程

Java 控制語句

面向物件程式設計

Java 內建類

Java 檔案處理

Java 錯誤和異常

Java 多執行緒

Java 同步

Java 網路

Java 集合

Java 介面

Java 資料結構

Java 集合演算法

高階 Java

Java 雜項

Java API 和框架

Java 類參考

Java 有用資源

Java - HttpURLConnection getErrorStream()



Java HttpURLConnection getErrorStream() 方法在連線失敗但伺服器仍然傳送了有用的資料時返回錯誤流。典型的例子是當 HTTP 伺服器響應 404 時,這將導致在連線中丟擲 FileNotFoundException,但伺服器傳送了一個 HTML 幫助頁面,其中包含有關如何操作的建議。

此方法不會導致連線啟動。如果連線未連線,或者伺服器在連線過程中沒有錯誤,或者伺服器有錯誤但沒有傳送錯誤資料,則此方法將返回 null。這是預設值。

宣告

以下是 java.net.HttpURLConnection.getErrorStream() 方法的宣告

public InputStream getErrorStream()

引數

返回值

如果有錯誤流,則返回錯誤流;如果沒有錯誤、連線未連線或伺服器未傳送任何有用的資料,則返回 null。

異常

示例 1

以下示例演示瞭如何使用 Java HttpURLConnection getErrorStream() 方法處理使用 https 協議的無效 url。在此示例中,我們正在建立 URL 類的例項。使用 url.openConnection() 方法,我們獲取 HttpURLConnection 例項。使用 getErrorStream(),我們檢查伺服器是否返回錯誤響應並打印出來。

package com.tutorialspoint;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;

public class HttpUrlConnectionDemo {
   public static void main(String [] args) {
      try {
         URL url = new URL("https://tutorialspoint.tw/index1.htm?language=en#j2se");
         HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
         urlConnection.setConnectTimeout(1000);  
         urlConnection.connect();  
         System.out.println("Connected.");  
         if(urlConnection.getErrorStream() != null) {
            BufferedReader in = new BufferedReader(
               new InputStreamReader(urlConnection.getErrorStream()));
            String content = "";
            String current;
            while((current = in.readLine()) != null) {
               content += current;
            }
            System.out.println(content);
         }else {
            System.out.println("Error Stream is null");
         }
      } catch (IOException e) {
         e.printStackTrace();
      }
   }
}

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

輸出

Connected.
Error Stream is null

示例 2

以下示例演示瞭如何使用 Java HttpURLConnection getErrorStream() 方法處理使用 http 協議的無效 url。在此示例中,我們正在建立 URL 類的例項。使用 url.openConnection() 方法,我們獲取 HttpURLConnection 例項。使用 getErrorStream(),我們檢查伺服器是否返回錯誤響應並打印出來。

package com.tutorialspoint;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;

public class HttpUrlConnectionDemo {
   public static void main(String [] args) {
      try {
         URL url = new URL("https://tutorialspoint.tw/index1.htm?language=en#j2se");
         HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
         urlConnection.setConnectTimeout(1000);  
         urlConnection.connect();  
         System.out.println("Connected.");  
         if(urlConnection.getErrorStream() != null) {
            BufferedReader in = new BufferedReader(
               new InputStreamReader(urlConnection.getErrorStream()));
            String content = "";
            String current;
            while((current = in.readLine()) != null) {
               content += current;
            }
            System.out.println(content);
         }else {
            System.out.println("Error Stream is null");
         }
      } catch (IOException e) {
         e.printStackTrace();
      }
   }
}

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

輸出

Connected.
Error Stream is null

示例 3

以下示例演示瞭如何使用 Java HttpURLConnection getErrorStream() 方法處理使用 http 協議的無效 url。在此示例中,我們正在建立 URL 類的例項。使用 url.openConnection() 方法,我們獲取 HttpURLConnection 例項。使用 getErrorStream(),我們檢查伺服器是否返回錯誤響應並打印出來。

package com.tutorialspoint;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;

public class HttpUrlConnectionDemo {
   public static void main(String [] args) {
      try {
         URL url = new URL("https://www.google.com/index1.htm");
         HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
         urlConnection.setConnectTimeout(1000);  
         urlConnection.connect();  
         System.out.println("Connected.");  
         if(urlConnection.getErrorStream() != null) {
            BufferedReader in = new BufferedReader(
               new InputStreamReader(urlConnection.getErrorStream()));
            String content = "";
            String current;
            while((current = in.readLine()) != null) {
               content += current;
            }
            System.out.println(content);
         }else {
            System.out.println("Error Stream is null");
         }
      } catch (IOException e) {
         e.printStackTrace();
      }
   }
}

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

輸出

Connected.
Error Stream is null
java_httpurlconnection.htm
廣告

© . All rights reserved.