【问题标题】:Why HttpURLConnection.getInputStream stop working in Java为什么 HttpURLConnection.getInputStream 在 Java 中停止工作
【发布时间】:2018-03-22 02:10:25
【问题描述】:

我下面的程序停在了这一行:

InputStream is = conn.getInputStream();

既没有弹出错误消息,也没有在控制台上显示任何输出。 我在 Red Hat Enterprise Linux Server 7.4 版上运行 Eclipse Oxygen 4.7.0。

 public static void solveInstance(String instanceName){
            // solve a problem instance
            try{
                String query_url = "http://localhost:8807/scheduler";

                // read Request to a JSON Object
                JSONParser parser = new JSONParser();
                JSONObject request = (JSONObject) parser.parse(new FileReader(instanceName));

                // open connection
                URL url = new URL(query_url);
                HttpURLConnection conn = (HttpURLConnection) url.openConnection();
                conn.setConnectTimeout(5000);
                conn.setRequestProperty("Content-Type", "application/json; charset=UTF-8");
                conn.setDoOutput(true);
                conn.setRequestMethod("POST");

                // POST Request
                OutputStream os = conn.getOutputStream();
                OutputStreamWriter osw =  new OutputStreamWriter(os, "UTF-8");
                osw.write(request.toString());
                osw.close();

                // Get Response
                InputStream is = conn.getInputStream();
                InputStreamReader isr = new InputStreamReader(is);
                BufferedReader in = new BufferedReader(isr);

                String inputLine;
                StringBuffer response = new StringBuffer();
                while(( inputLine = in.readLine()) != null )
                {
                    response.append(inputLine);
                }
                in.close();

                // Write response to a file
                JSONObject jsonObj = (JSONObject) parser.parse(response.toString());
                String responseFile = /path_to_result/result.json;

                FileWriter fileWriter = new FileWriter(responseFile);
                fileWriter.write(jsonObj.toString());
                fileWriter.flush();
                fileWriter.close();

                System.out.println("Solved" + instanceName);
            }
            catch(Exception e) {
                System.out.println(e);
            }
        }

我注意到有人问过类似的问题InputStream is = httpURLConnection.getInputStream(); stop working 但它是针对 Android 的,没有给出解决方案。

有人对此有什么想法吗?不要犹豫,指出我的代码中的任何其他错误。

非常感谢!

【问题讨论】:

  • 当您尝试从网络浏览器访问query_url 时会发生什么?在我看来,您的网络应用程序可能挂起。
  • “我下面的程序停在了行” -- 你说的“停”是什么意思?除非您有一个空的 catch 块并抛出异常,否则您的程序不能在没有消息的情况下停止执行。一定是某处记录了一些东西。
  • @JimGarrison 如果没有发送响应并且服务器保持连接打开并且客户端没有读取超时,它可能会永远阻止读取响应。
  • @TimBiegeleisen 该网络应用程序似乎运行良好——我使用 Postman 发布了我的请求并得到了预期的响应。当我使用上述函数时会出现问题,执行将在指定行停止(或永远挂起?不确定)。我必须使用上述功能,因为 Postman 无法处理大型请求(大型 JSON 数据)。我仅针对您的问题尝试了使用少量数据的 Postman。
  • @JorgeCampos 它没有丢失。 Content-Length 是自动设置的,如果手动设置将被覆盖。

标签: java http


【解决方案1】:

您的服务器没有响应。

使用conn.setReadTimeout(5000)(比如说)设置读取超时是明智的。根据需要调整超时。

【讨论】:

    【解决方案2】:

    检查响应代码

    int response = connection.getResponseCode();

    如果您收到 301,则表示您的资源已被重定向。尝试将 URI 从 http 更改为 https。

    这对我有帮助。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-03-01
      • 1970-01-01
      • 2011-11-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-04-23
      相关资源
      最近更新 更多