Java HttpURLConnection 类

如果连接失败但服务器仍然发送了有用的数据,Java HttpURLConnection getErrorStream() 方法将返回错误流。典型的例子是当 HTTP 服务器响应 404 时,这将导致在连接中抛出 FileNotFoundException,但服务器发送了一个 HTML 帮助页面,其中包含有关如何操作的建议。

此方法将不会导致连接启动。如果连接未连接,或者服务器在连接时没有发生错误,或者服务器发生错误但没有发送错误数据,则此方法将返回 null。这是默认值。

声明

以下是 java.net.HttpURLConnection.getErrorStream() 方法的声明

public InputStream getErrorStream()
  • 1

参数

返回值

错误流(如果有),如果没有错误、连接未连接或服务器未发送任何有用信息,则为 null

异常

示例 1

以下示例显示了 Java HttpURLConnection getErrorStream() 方法的用法,该方法用于处理无效的错误使用 https 协议的 url。在此示例中,我们将创建 URL 类的实例。使用 url.openConnection() 方法,我们获取 HttpURLConnection 实例。使用 getErrorStream(),我们检查服务器是否返回错误响应并打印相同的内容 -

package com.yxjc123;

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.yxjc123.com/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();
      }
   }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33

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

输出

Connected.
Error Stream is null
  • 1
  • 2

示例 2

以下示例显示使用 Java HttpURLConnection getErrorStream() 方法来处理 http 协议的无效 url。在此示例中,我们将创建 URL 类的实例。使用 url.openConnection() 方法,我们获取 HttpURLConnection 实例。使用 getErrorStream(),我们检查服务器是否返回错误响应并打印相同的内容 -

package com.yxjc123;

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("http://www.yxjc123.com/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();
      }
   }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33

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

输出

Connected.
Error Stream is null
  • 1
  • 2

示例 3

以下示例显示使用 Java HttpURLConnection getErrorStream() 方法来处理 http 协议的无效 url。在此示例中,我们将创建 URL 类的实例。使用 url.openConnection() 方法,我们获取 HttpURLConnection 实例。使用 getErrorStream(),我们检查服务器是否返回错误响应并打印相同的内容 -

package com.yxjc123;

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.baidu.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();
      }
   }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33

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

输出

Connected.
Error Stream is null
  • 1
  • 2