java bufferreader io异常:流已关闭

c8ib6hqw  于 2021-07-03  发布在  Java
关注(0)|答案(0)|浏览(278)

这是我目前的代码:

//Create connection
            URL url = new URL(targetURL);
            connection = (HttpURLConnection) url.openConnection();
            connection.setRequestMethod("POST");
            connection.setRequestProperty("Content-Type",
                    "application/x-www-form-urlencoded");

            connection.setRequestProperty("Content-Length",
                    Integer.toString(urlParameters.getBytes().length));
            connection.setRequestProperty("Content-Language", "en-US");
            connection.setRequestProperty("User-Agent", "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.95 Safari/537.36");
            connection.setUseCaches(false);
            connection.setDoOutput(true);
            connection.setInstanceFollowRedirects(true);

            //Send request
            DataOutputStream wr = new DataOutputStream (
                    connection.getOutputStream());
            wr.writeBytes(urlParameters);
            wr.close();

            InputStream is = connection.getInputStream();
            BufferedReader rd = new BufferedReader(new InputStreamReader(is));
            StringBuilder response = new StringBuilder(); 
            String line;
            while ((line = rd.readLine()) != null) {
                response.append(line);
                response.append('\n');
            }
            rd.close();
            is.close();
            return response.toString();

它正在破门而入

while ((line = rd.readLine()) != null) {

这是在一个线程中,似乎其他线程正在同时关闭所有的bufferreader。我不确定是什么问题。

java.io.IOException: stream is closed
    at sun.net.www.http.ChunkedInputStream.ensureOpen(ChunkedInputStream.java:174)
    at sun.net.www.http.ChunkedInputStream.read(ChunkedInputStream.java:673)
    at java.io.FilterInputStream.read(FilterInputStream.java:133)
    at sun.net.www.protocol.http.HttpURLConnection$HttpInputStream.read(HttpURLConnection.java:3454)
    at sun.nio.cs.StreamDecoder.readBytes(StreamDecoder.java:284)
    at sun.nio.cs.StreamDecoder.implRead(StreamDecoder.java:326)
    at sun.nio.cs.StreamDecoder.read(StreamDecoder.java:178)
    at java.io.InputStreamReader.read(InputStreamReader.java:184)
    at java.io.BufferedReader.fill(BufferedReader.java:161)
    at java.io.BufferedReader.readLine(BufferedReader.java:324)
    at java.io.BufferedReader.readLine(BufferedReader.java:389)
    at Utilities.Url_Opener.executePost(Url_Opener.java:105)
    at Utilities.Url_Opener.gotoOutwarSessId(Url_Opener.java:43)
    at Utilities.Url_Opener.gotoOutwar(Url_Opener.java:22)
    at Utilities.Functions.castAllSkills(Functions.java:158)
    at RaidTool.Skilling.run(Skiller.java:59)
    at java.lang.Thread.run(Thread.java:748)

更新为包含整个(最小)函数。

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题