java - 释放 HttpClient 4.1.x 的连接以使用一个 HttpClient 实例顺序执行

标签 java httpclient

很抱歉有点重复,here , here ,但他们说,如果您读取响应正文的最后一个字节并关闭输入流。它已准备好接受另一个请求。但是看看我在这里得到的结果,在执行新请求时我总是收到此错误。

Invalid use of SingleClientConnManager: connection still allocated

public String detectGooglePost(String text) throws SystemException {

        List<NameValuePair> qParams = new ArrayList<NameValuePair>();
        qParams.add(new BasicNameValuePair("key", key));

        List<NameValuePair> bodyParams = new ArrayList<NameValuePair>();
        bodyParams.add(new BasicNameValuePair("q", text));

        HttpPost httpPost = new HttpPost(createURI(qParams));
        httpPost.addHeader("X-HTTP-Method-Override", "GET");
        try {
            httpPost.setEntity(new UrlEncodedFormEntity(bodyParams));
        } catch (UnsupportedEncodingException e) {
            throw new SystemException("Problem when buidling Google request entity", e);
        }

        String language = getLanguage(httpPost);
        return language;
    }

    private String getLanguage(HttpUriRequest request) throws SystemException {

        HttpResponse response;
        try {
            response = httpclient.execute(request);
        } catch (Exception e) {
            throw new SystemException("Problem when executing Google request", e);
        }

        int sc = response.getStatusLine().getStatusCode();
        if (sc != HttpStatus.SC_OK)
            throw new SystemException("google status code : " + sc);

        StringBuilder builder = getBuilder(response);
        String language = processJson(builder.toString());

        return language;
    }

    private StringBuilder getBuilder(HttpResponse response) throws SystemException {
        HttpEntity entity = response.getEntity();
        if (entity == null)
            throw new SystemException("response entity null");

        StringBuilder builder = new StringBuilder();
        BufferedReader in = null;
        String str;
        try {
            in = new BufferedReader(new InputStreamReader(entity.getContent()));
            while ((str = in.readLine()) != null)
                builder.append(str);
        } catch (IOException e) {
            throw new SystemException("Reading input stream of http google response entity problem", e);
        } finally {
                    IOUtils.closeQuietly(in);
        }
        if (builder.length() == 0)
            throw new SystemException("content stream of response entity empty has zero length");
        return builder;
    }

如何释放连接?该流被读取并关闭。但是当再次调用 detectorGooglePost(text) 方法(单个 HttpClient 实例)时,它仍然会抛出该错误。

最佳答案

伙计,它一定有效,试试这个:

    HttpResponse response;
    String responseBody;
    try {
        response = httpclient.execute(request);
        int sc = response.getStatusLine().getStatusCode();
        if (sc != HttpStatus.SC_OK)
            throw new SystemException("google status code : " + sc);

        HttpEntity entity = response.getEntity();
        if (entity == null)
            throw new SystemException("response entity null");
        responseBody = EntityUtils.toString(entity);
    } catch (Exception e) {
        request.abort();
        throw new SystemException("Problem when executing Google request", e);
    }

关于java - 释放 HttpClient 4.1.x 的连接以使用一个 HttpClient 实例顺序执行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7120558/

相关文章:

java - 从 SQLiteDB 获取选择信息?

java - 如何有效地在 Canvas 上绘制多个像素?

java - 访问运行时设置的变量值

java - Apache HTTPClient 没有这样的方法 ContentType.create

java - JPA将非id字段映射为外键

Java 杀死或终止一个线程

c# - 将 JSON HttpContent 发布到 ASP.NET Web API

java - 使用用户名和密码登录 LinkedIn 失败

java - 如何在 httpclient 4.3+ 中更新 HttpClient 的设置?

java - 具有自签名证书的双向 SSL 客户端