为什么HttpURLConnection.getInputStream停止在Java中工作 [英] Why HttpURLConnection.getInputStream stop working in Java

查看:258
本文介绍了为什么HttpURLConnection.getInputStream停止在Java中工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的节目停在了以下一行:

My program below stopped at the line:

InputStream is = conn.getInputStream();

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

Neither error message popped up nor any output displayed on the console. I am running Eclipse Oxygen 4.7.0 on Red Hat Enterprise Linux Server release 7.4.

 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();停止工作
但这是针对Android而没有给出解决方案。

I noticed that the similar question is asked InputStream is = httpURLConnection.getInputStream(); stop working but it was for Android and no solution was given there.

有没有人对此有所了解?不要犹豫,在我的代码中指出其他任何错误。

Does anyone have some idea on that? Do not hesitate to point out anything else wrong in my code.

非常感谢!

推荐答案

您的服务器没有响应。

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

It would be wise to set a read timeout, with conn.setReadTimeout(5000) (say). Adjust the timeout as necessary.

这篇关于为什么HttpURLConnection.getInputStream停止在Java中工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆