private HttpClient getHttpClient() { MultiThreadedHttpConnectionManager connectionManager = new MultiThreadedHttpConnectionManager(); // 多线程 // 多线程 HttpClient h = new HttpClient(connectionManager); // 设置HTTP请求头文件 HttpConnectionManagerParams managerParams = h.getHttpConnectionManager().getParams(); // 设置连接超时时间(单位毫秒) managerParams.setConnectionTimeout(10000); // 设置读数据超时时间(单位毫秒) managerParams.setSoTimeout(5000); return h; } /** * 功能:检测当前URL是否可连接或是否有效, * 描述:最多连接网络 3 次, 3次脚本调用失败则跳过这个URL * @param link 指定URL网络地址 * @return Map<String,Integer> */ /* private int getHttpReq(String link) { int StatusCode = 0; HttpClient h = getHttpClient(); try { // 获取http客户端连接 String httpUrl =link; GetMethod g = new GetMethod(httpUrl); StatusCode = h.executeMethod(g); } catch (Exception e) { e.printStackTrace(); LOGGER.info(">>>>>>>>>>>>>>>>>>>>获取URL状态码出错<<<<<<<<<<<<<<<<<<<<"); } return StatusCode;}*/
private synchronized Map<String,Object> getHttpReq(String link) { //new map Map<String,Object> map = new HashMap<String, Object>(); //获取HttpClient HttpClient h = getHttpClient(); //变量状态码 int state = -1; //变量链接次数 int counts = 1; //判断链接是否有效 if (link == null || link.length() <= 0) { return map; } //最多连接网络 3 次 while (counts < 4) { try { // 获取http客户端连接 String httpUrl =link; //获取get请求的url GetMethod g = new GetMethod(httpUrl); //执行url state = h.executeMethod(g); LOGGER.info(">>>>>>>>>>>>>>>>>>>>"+counts+"= "+state+"<<<<<<<<<<<<<<<<<<<<"); if (state == 200) { LOGGER.info(">>>>>>>>>>>>>>>>>>>>URL可用<<<<<<<<<<<<<<<<<<<<"); } break; } catch (Exception ex) { LOGGER.info(">>>>>>>>>>>>>>>>>>>>URL不可用,连接第"+counts+"次<<<<<<<<<<<<<<<<<<<<"); map.put("exception",ex); counts++; continue; } } map.put("counts",counts); map.put("state",state); return map; }