java - 多线程 java 应用程序中 java.net.HttpURLConnection 的不同实例

标签 java multithreading httpurlconnection

我有一个多线程 java 客户端,它将数据发送到服务器并使用 HttpURLConnection 读取响应。客户端的每个实例都是线程,即使 HttpURLConnection 不共享,它们也不共享任何内容。 (每个线程都有自己的HttpURLConnection)并且它似乎仍然不是线程安全的。有人可以证实这种行为吗?您建议采取什么解决方案? 顺便说一句,我看到过类似的问题,但那是在不同线程之间共享 HttpURLConnection 。请注意,就我而言,它没有共享。

public class ESIHttpCaller{

private final String ESIHTTPURL = "http://localhost:7033/FBWS/eigBagHttpDispatcher"
private HttpURLConnection connection = null;
protected StringBuilder response = new StringBuilder();

public CBBag executePost(byte[] input) throws CBException {
    InputStream is = null;
    DataOutputStream wr =null;
    int STATE = 0;
    try{
        // Create connection
        URL url = new URL(getConnectionUrl());
        connection = (HttpURLConnection) url.openConnection();
        connection.setRequestMethod("POST");
        connection.setRequestProperty("Content-Type","application/x-www-form-urlencoded;charset="+encoding);
        connection.setRequestProperty("Content-Length", "" + Integer.toString(length));
        connection.setRequestProperty("encoding", encoding);
        connection.setRequestProperty("Accept-Encoding", encoding);
        connection.setConnectTimeout(getConnectTimeout());
        connection.setReadTimeout(getReadTimeout());
        connection.setUseCaches(false);
        connection.setDoInput(true);
        connection.setDoOutput(true);

        // Send request
        wr = new DataOutputStream(connection.getOutputStream());
        wr.write(input);
        wr.flush();
        //Get response  
        STATE = 1;
        is = connection.getInputStream();
        BufferedReader rd = new BufferedReader(new InputStreamReader(is,encoding));
        String line = rd.readLine() ;
        while (line != null ) {
            response.append(line);
            line = rd.readLine();
        }
        rd.close();
        return handleResponse(response);
    }   
    catch (Exception e) {
        closeQuietly(is,wr);
        throw handleException(STATE, e);
    }
    finally {
        if (connection != null) {
            connection.disconnect();
        }
    }
}

}

线程中的调用者代码如下:

        ESIHttpCaller caller = new ESIHttpCaller();
        caller.setTcid(tcid);
        return caller.executePost(outStr.getBytes());

最佳答案

因为您在 HttpURLConnection 上调用disconnect()而不是close()。

来自 HttpURLConnection 的 JavaDoc (强调我的):

Each HttpURLConnection instance is used to make a single request but the underlying network connection to the HTTP server may be transparently shared by other instances. Calling the close() methods on the InputStream or OutputStream of an HttpURLConnection after a request may free network resources associated with this instance but has no effect on any shared persistent connection. Calling the disconnect() method may close the underlying socket if a persistent connection is otherwise idle at that time.

关于java - 多线程 java 应用程序中 java.net.HttpURLConnection 的不同实例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32367729/

相关文章:

java - 如何在Android中使用twitter4j库将图片发布到Twitter

java - "stay logged in"登录页面

python - 具有已发布的 GIL 和复杂线程的多线程代码在 Python 中速度较慢

java - 使用 Java HttpURLConnection 查询 Open MapQuest API 没有给出结果

Java - 为什么这个二进制堆的实现比另一个更快?

java - 计算 Android/Google map 中行驶的距离

javascript - Node.js worker_threads 模块是真正的线程还是只是带有 IPC 的进程?

java - 检测当前线程是否为 ExecutorService 线程

java - 使用 UrlConnection 逐行读取是否考虑换行?

java - 使用 Java 的 HTTP OPTIONS 方法