简体   繁体   English

套接字超时不起作用,InputStream读取了块

[英]Socket timeout not working, InputStream read blocks

I have problem with socket inputstream reading. 套接字输入流读取出现问题。 Before reading I set timeout for HttpClient 在阅读之前,我为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. 尝试读取响应流,但改为SocketTimeoutException我在read()方法上遇到了阻塞。

    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 ? 我如何读取没有块的输入流,为什么soTimeout无法正常工作?

Thanks a lot. 非常感谢。

Was resolved. 解决了。 I added thread monitor that invoke stop method of thread reading response. 我添加了调用线程读取响应的stop方法的线程监视器。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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