Java getInputStream冻结并且永远不会结束

Java getInputStream冻结并且永远不会结束,java,Java,我正在使用HttpURLConnection从我的网站读取一个简单的文本文件,当它调用getInputStream时,它会冻结并且永远不会结束,getResponseCode返回200。一开始它一直在工作,后来它停止了工作 我在互联网上到处寻找解决方案,但都没有找到。我认为这可能是网络服务器上的ddos保护,或者是网络服务器上的某些东西阻止了它 public static String getLatestInfo(String urlString){ URL url = null; HttpUR

我正在使用HttpURLConnection从我的网站读取一个简单的文本文件,当它调用getInputStream时,它会冻结并且永远不会结束,getResponseCode返回200。一开始它一直在工作,后来它停止了工作

我在互联网上到处寻找解决方案,但都没有找到。我认为这可能是网络服务器上的ddos保护,或者是网络服务器上的某些东西阻止了它

public static String getLatestInfo(String urlString){
URL url = null;
HttpURLConnection connection = null;
/*InputStream inputStream = null;
InputStreamReader isr = null;
BufferedReader in = null;*/
try {
    url = new URL(urlString);
    connection = (HttpURLConnection) url.openConnection();
    //connection.setRequestMethod("GET");
    //connection.setDoOutput(true);
    connection.setConnectTimeout(5000); // 5 seconds connectTimeout
    connection.setReadTimeout(5000); // 5 seconds socketTimeout
    connection.setRequestProperty("Connection", "close");
    connection.setRequestProperty("Content-Type", "text/xml");
    connection.setRequestProperty("Accept", "text/xml");
    connection.setRequestProperty("User-Agent", "Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10.4; en-US; rv:1.9.2.2) Gecko/20100316 Firefox/3.6.2");

    connection.connect(); 

    if (connection.getResponseCode() == 200) {
            try(InputStream inputStream = connection.getInputStream())
            {
                try(InputStreamReader isr = new InputStreamReader(inputStream, "UTF-8")) {
                    try(BufferedReader in = new BufferedReader(isr))
                    {
                        String line;
                        String config = "";
                        while ((line = in.readLine()) != null) {
                            config = config + line + "\n";
                        }
                        return config;
                    }
                }
            }
        /*inputStream = connection.getInputStream();
        isr = new InputStreamReader(inputStream, "UTF-8");
        in = new BufferedReader(isr);
        String line;
        String config = "";
        while ((line = in.readLine()) != null) {
            config = config + line + "\n";
        }
        return config;*/
    }

}
catch (SocketTimeoutException e)
{
    System.out.println("Timeout: " + e.getMessage());
}
catch (MalformedURLException e) {
    System.out.println("Malformed URL: " + e.getMessage());
}
catch (IOException e) {
    System.out.println("I/O Error: " + e.getMessage());
    try {
        System.out.println(connection.getResponseCode());
    } catch (IOException e1) {
        e1.printStackTrace();
    }
}
finally {
    if (connection != null) connection.disconnect();
    /*try {
        if (inputStream != null) inputStream.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
    try {
        if (in != null) in.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
    try {
        if (isr != null) isr.close();
    } catch (IOException e) {
        e.printStackTrace();
    }*/
}
return null;}

我从简单开始,添加了所有本应解决此问题的建议。。它获取响应代码200,然后在连接时冻结。getInputStream()

我的代码中有多处错误:

  • 用于文件读取的某些流未关闭
  • 我使用的是
    FileInputStream/FileOutputStream
    ,它使它冻结。解释它。虽然它显示了一个工作环境,但它仍然会使它冻结
  • 我使用的主要问题是
    SwingUtilities.invokeLater(newrunnable()…
    ),它是冻结点和随机点

  • 修复了以上所有因素,解决了我的问题。

    你多久运行一次这个程序?我想网站可能已经确定你在太短的时间内连接了太多次。无关:不要在GET请求中指定
    内容类型。
    。GET请求没有内容,因此没有任何内容可以指定type of.Unrelated:记得关闭流。强烈建议使用try with resources!!!删除
    setDoOutput(true)
    ,因为你实际上没有做任何输出(因为你不应该做GET请求)。我已经使用它大约一个月了,我尝试切换主机并得到同样的结果。