简体   繁体   中英

Socket timeout not working, InputStream read blocks

I have problem with socket inputstream reading. Before reading I set timeout for HttpClient

connectionManager.getParams().setSoTimeout(1000*10);
connectionManager.getParams().setConnectionTimeout(1000*10);
HttpClient client = new HttpClient(connectionManager);

Try to read response stream and instead SocketTimeoutException I getting block on read() method.

    client.executeMethod(method);
    InputStream stream = method.getResponseBodyAsStream();

    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    try {
        int read;
        byte[] bytes = new byte[1024];
        while ((read = stream.read(bytes)) != -1) {
            baos.write(bytes, 0, read);
        }
    } catch (Throwable e) {
        e.printStackTrace();
    } finally {
        try {
            baos.close();
            stream.close();
        } catch (Throwable e) {
            e.printStackTrace();
        }
    }

How can I read input stream without blocks and why soTimeout not working ?

Thanks a lot.

Was resolved. I added thread monitor that invoke stop method of thread reading response.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM