【问题标题】:HttpsUrlConnection getting EOF exception while GET requestGET请求时HttpsUrlConnection获取EOF异常
【发布时间】:2016-08-23 07:22:19
【问题描述】:

我正在使用 https 从服务器获取一些数据。问题是当我尝试运行此代码时,它只会写入空字符串(response.toString() 返回空)。但是当我在BufferedReader 行之前添加Thread.sleep 方法时,它可以完美运行(从服务器返回完整数据)。这里的主要问题是什么?

   private static void httpGet(String urlStr, String[] paramName, String[] paramVal) throws Exception {
    URL url = new URL(urlStr);
    HttpsURLConnection conn
            = (HttpsURLConnection) url.openConnection();
    conn.setReadTimeout(10000);
    conn.setConnectTimeout(15000);
    conn.setRequestMethod("GET");
    conn.setDoOutput(true);
    conn.setDoInput(true);
    conn.setUseCaches(false);
    conn.setAllowUserInteraction(false);
    conn.setRequestProperty(COOKIE_HEADER, param);

    if (conn != null) {
    //Thread.sleep(1000);
    BufferedReader in = new BufferedReader(
                    new InputStreamReader(conn.getInputStream()));
            String inputLine;
            StringBuffer response = new StringBuffer();

            while ((inputLine = in.readLine()) != null) {
                response.append(inputLine);
            }
     System.out.println(response.toString());
    }
   }

【问题讨论】:

  • 你的服务器代码是什么?此客户端代码看起来非常好,所以请发布您的服务器代码,也许问题就在那里。也请将此代码更新为完整的方法,因为这只是一个 sn-p,并且可能包含在此代码的信封中。
  • @KrzysztofCichocki 我没有编写服务器代码。我从 Nessus 应用程序中获取一些 xml 数据。 (discussions.tenable.com/docs/DOC-1172)。当我发布我的登录参数时,Nessus 会返回一些会话令牌。这部分工作没有任何问题,但是当我尝试从 Nessus 获取一些 xml 信息时,如果没有 Thread.sleep,它就无法工作。
  • 尝试删除“conn.setDoOutput(true);”行,如果没有效果,则尝试使用“conn.getOutputStream().flush();”而不是 "Thread.sleep() " ,也许这会对你有所帮助。

标签: java https stream


【解决方案1】:

好吧,您的代码对我有用 ;-) 即使没有 Thread.sleep - 见下文 - 我正在从“Oracle”中提取数据。另外请不要忘记关闭输入流。

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

import javax.net.ssl.HttpsURLConnection;

public class URLConnectionReader {
public static void main(String[] args) throws Exception {
    URL url = new URL("https://www.oracle.com");
    HttpsURLConnection conn = (HttpsURLConnection) url.openConnection();
    conn.setReadTimeout(10000);
    conn.setConnectTimeout(15000);
    conn.setRequestMethod("GET");
    conn.setDoOutput(true);
    conn.setDoInput(true);
    conn.setUseCaches(false);
    conn.setAllowUserInteraction(false);
    //conn.setRequestProperty("COOKIE_HEADER", param);

    if (conn != null) {
        // Thread.sleep(1000);
        BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream()));
        String inputLine;
        StringBuffer response = new StringBuffer();

        while ((inputLine = in.readLine()) != null) {
            response.append(inputLine);
        }
        System.out.println(response.toString());
    }
}

}

【讨论】:

    【解决方案2】:

    HttpsUrlConnection GET 请求时出现 EOF 异常

    您没有执行 GET 请求。您正在执行 POST,但未提供任何 POST 数据。

    这里还有很多其他问题:

    conn.setRequestMethod("GET");
    

    这是默认设置。不要编写无意义的代码。

    conn.setDoOutput(true);
    

    这会将请求方法更改为 POST。如果您不想进行 POST,请删除此行。

    conn.setDoInput(true);
    

    这是默认设置。不要编写无意义的代码。

    if (conn != null) {
    

    conn 此时不可能为空。不要编写无意义的代码。

    //Thread.sleep(1000);
    

    在网络代码中休眠实际上是在浪费时间。不要使用它们,也不要认为它们可以解决网络问题。他们没有。他们可能会掩盖它们,但根本问题仍然存在。

    BufferedReader in = new BufferedReader(
                    new InputStreamReader(conn.getInputStream()));
            String inputLine;
            StringBuffer response = new StringBuffer();
    
            while ((inputLine = in.readLine()) != null) {
                response.append(inputLine);
            }
    

    在这里,您尝试读取来自 POST 的响应,而无需写入任何 POST 数据。服务器可能会对此表示反对,可能会过早终止连接,这可能会导致EOFException,但不会出现在上面的readLine() 代码中。由于您没有提供堆栈跟踪,因此无法进一步评论。但是,除非存在服务器端问题,否则您的代码在修复上述错误后应该可以工作。

    【讨论】:

    • 此代码不会引发任何异常,但 response.toString 方法返回空字符串。(当我尝试使用 conn.getInputstream() 时出现文件结束错误)当我添加 thread.sleep 方法时response.toString 方法从服务器返回数据。我不明白为什么我需要添加 Thread.sleep? Thread.sleep 在这里做什么?
    • 您无需添加Thread.sleep()。我已经说过了。您需要修复我在上面列举的几个错误。您仍未提供所请求的堆栈跟踪。
    • 如果代码没有抛出任何异常,为什么你的问题会提到'EOF异常'?
    猜你喜欢
    • 2021-09-03
    • 1970-01-01
    • 2021-11-01
    • 1970-01-01
    • 2021-10-12
    • 2011-07-19
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多