【原创】Java HttpClient Error: Connection refused

Java HttpClient Raise Error: Connection http://www.xxx.com refused

在 Android 上运行 jar,目的是从服务器一直获取消息, 使用 HTTP 服务.

但是每次启动进程, 当请求次数达到1010 时候, 总是会报错: Connection http://www.xxx.com refused
很规律, 每次当请求1000次左右的时候就全挂了.

杀掉进程重新启动, 一切正常,1000次后又是连接被重置.

检查 HTTP 服务, 用 PC 访问API 正常, 在 Android 上用浏览器访问, API 也正常. 可以确认服务是正常的, 并不是服务器拒绝连接.


adb shell  logcat -c #  清除手机的 log

adb shell logcat

通过手机查看 log, 发现了错误提示:… -EMFILE (Too many open files) …

原因是: socket连接太多, Android本地拒绝连接服务.

有 httpGet/HttpPostclose()/releaseCollection()

有httpClient.getConnectionManager()closeExpiredConnections()/shutdown()

HttpGetclose()/releaseCollection 只是虚函数, 没有实例函数, 并不能用.

正确的方式是

httpClient.getConnectionManager().closeExpiredConnections()
httpClient.getConnectionManager().shutdown()

代码如下

     public static String get(String URL, Map params) {

        String result = "";

        String URI = setParams(params);

        // System.out.println(URL + "?" + URI);

        httpGet = new HttpGet(URL + "?" + URI);

        HttpClient httpClient = new DefaultHttpClient();

        httpClient.getParams().setIntParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, CONNECTION_TIMEOUT);

        httpClient.getParams().setIntParameter(CoreConnectionPNames.SO_TIMEOUT, SOCKET_TIMEOUT);

        httpClient.getParams().setLongParameter(ConnManagerPNames.TIMEOUT, MCC_TIMEOUT);

        httpClient.getParams().setLongParameter(ConnManagerPNames.MAX_TOTAL_CONNECTIONS, 256);



        HttpResponse response = null;

        try {

            response = httpClient.execute(httpGet);

        } catch (Exception e) {

            Logger.getLogger(Configure.loggerName).severe(Configure.logTag

                    + String.format("[COUNT: %s] Download failed, url: %s, uri:%s error: %s", COUNT, URL, URI, e.getMessage()));

            httpClient.getConnectionManager().closeExpiredConnections();

            httpClient.getConnectionManager().shutdown();

            //httpGet.close();

            //httpGet.releaseConnection();

            //e.printStackTrace();

            return result;

        }



        try {

            HttpEntity httpEntity = response.getEntity();

            InputStream inputStream = httpEntity.getContent();

            BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));

            String line = "";

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

                result += (line + "\n");

            }

            // System.out.println(result);

            inputStream.close();

            reader.close();

            httpClient.getConnectionManager().closeExpiredConnections();

            httpClient.getConnectionManager().shutdown();

            // httpGet.close();

            // httpGet.releaseConnection();

        } catch (Exception e) {

            Logger.getLogger(Configure.loggerName).severe(Configure.logTag

                    + String.format("Download read response failed, url: %s error: %s", URL, e.getMessage()));

            // e.printStackTrace();

        }



        return result;

    }

参考

java.net.SocketException: Too many open files

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值