【原创】Java HttpClient&nbs…
来源:互联网 发布:js requestanimframe 编辑:程序博客网 时间:2024/06/05 06:03
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 # 清除手机的 logadb 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
- 【原创】Java HttpClient&nbs…
- 【原创】python requests&nbs…
- 【原创】windows mongodb&nbs…
- 【原创】python Reuqests&nbs…
- 【原创】python requests&nbs…
- 【原创】 linux net&nbs…
- 【原创】Mac os pip&nbs…
- java for(int i:d)&nbs…
- (转载)Java Collection&nbs…
- zoj2008 || poj1511&nbs…
- kdevelop + konsole&nbs…
- android videoview&nbs…
- poll, select &&nbs…
- Start/stop Oracle&nbs…
- unsupported major.minor&nbs…
- iOS UIDeviceOrientation&nbs…
- Remove WifiStateTracker&nbs…
- Remove WifiStateTracker&nbs…
- 【原创】反编译Android apk,提取*.…
- 【原创】Macbook 修改 ls 时间格式
- 【原创】python requests&nbs…
- banner使用
- 【原创】Java JNI 参数
- 【原创】Java HttpClient&nbs…
- PHP中include引用jpgraph时出现问题
- 【原创】windows xp squ…
- 协同过滤算法
- 【原创】MAC-OX install&nbsp…
- 您的设计模式——适配器模式【Adapter Pattern】
- 【原创】Python Mongo 批量操作
- Redis哨兵—实现Redis高可用
- C#读取Excel五种方式的体会