List of usage examples for org.apache.commons.httpclient DefaultHttpMethodRetryHandler DefaultHttpMethodRetryHandler
public DefaultHttpMethodRetryHandler()
From source file:org.apdplat.qa.datasource.GoogleDataSource.java
private List<Evidence> search(String query) { List<Evidence> evidences = new ArrayList<>(); try {/*from ww w . j a v a2 s . c o m*/ HttpClient httpClient = new HttpClient(); GetMethod getMethod = new GetMethod(query); httpClient.executeMethod(getMethod); getMethod.getParams().setParameter(HttpMethodParams.RETRY_HANDLER, new DefaultHttpMethodRetryHandler()); int statusCode = httpClient.executeMethod(getMethod); if (statusCode != HttpStatus.SC_OK) { LOG.error("Method failed: " + getMethod.getStatusLine()); } byte[] responseBody = getMethod.getResponseBody(); String response = new String(responseBody, "UTF-8"); LOG.debug("??" + response); JSONObject json = new JSONObject(response); String totalResult = json.getJSONObject("responseData").getJSONObject("cursor") .getString("estimatedResultCount"); int totalResultCount = Integer.parseInt(totalResult); LOG.info("? " + totalResultCount); JSONArray results = json.getJSONObject("responseData").getJSONArray("results"); LOG.debug(" Results:"); for (int i = 0; i < results.length(); i++) { Evidence evidence = new Evidence(); JSONObject result = results.getJSONObject(i); String title = result.getString("titleNoFormatting"); LOG.debug(title); evidence.setTitle(title); if (SUMMARY) { String content = result.get("content").toString(); content = content.replaceAll("<b>", ""); content = content.replaceAll("</b>", ""); content = content.replaceAll("\\.\\.\\.", ""); LOG.debug(content); evidence.setSnippet(content); } else { //URL??? String url = result.get("url").toString(); String content = Tools.getHTMLContent(url); if (content == null) { content = result.get("content").toString(); content = content.replaceAll("<b>", ""); content = content.replaceAll("</b>", ""); content = content.replaceAll("\\.\\.\\.", ""); } evidence.setSnippet(content); LOG.debug(content); } evidences.add(evidence); } } catch (Exception e) { LOG.error("?", e); } return evidences; }
From source file:org.apdplat.qa.util.Tools.java
public static String getRewindEvidenceText(String question, String answer) { //1??// w w w . j a va2 s . c o m String rewindEvidenceText = MySQLUtils.getRewindEvidenceText(question, answer); if (rewindEvidenceText != null) { //? LOG.info("?" + question + " " + answer); return rewindEvidenceText; } //2???google StringBuilder text = new StringBuilder(); String query = question + answer; try { query = URLEncoder.encode(query, "UTF-8"); } catch (UnsupportedEncodingException e) { LOG.error("url", e); return null; } query = "http://ajax.googleapis.com/ajax/services/search/web?start=0&rsz=large&v=1.0&q=" + query; try { HostConfiguration hcf = new HostConfiguration(); hcf.setProxy("127.0.0.1", 8087); HttpClient httpClient = new HttpClient(); GetMethod getMethod = new GetMethod(query); //httpClient.executeMethod(hcf, getMethod); httpClient.executeMethod(getMethod); getMethod.getParams().setParameter(HttpMethodParams.RETRY_HANDLER, new DefaultHttpMethodRetryHandler()); int statusCode = httpClient.executeMethod(getMethod); if (statusCode != HttpStatus.SC_OK) { LOG.error("Method failed: " + getMethod.getStatusLine()); } byte[] responseBody = getMethod.getResponseBody(); String response = new String(responseBody, "UTF-8"); LOG.debug("??" + response); JSONObject json = new JSONObject(response); String totalResult = json.getJSONObject("responseData").getJSONObject("cursor") .getString("estimatedResultCount"); int totalResultCount = Integer.parseInt(totalResult); LOG.info("? " + totalResultCount); JSONArray results = json.getJSONObject("responseData").getJSONArray("results"); LOG.debug(" Results:"); for (int i = 0; i < results.length(); i++) { JSONObject result = results.getJSONObject(i); String title = result.getString("titleNoFormatting"); LOG.debug(title); //URL??? String url = result.get("url").toString(); String content = null;//Tools.getHTMLContent(url); if (content == null) { //???? content = result.get("content").toString(); content = content.replaceAll("<b>", ""); content = content.replaceAll("</b>", ""); content = content.replaceAll("\\.\\.\\.", ""); } LOG.debug(content); text.append(title).append(content); } LOG.info("" + question + " " + answer + " MySQL?"); MySQLUtils.saveRewindEvidenceText(question, answer, text.toString()); return text.toString(); } catch (Exception e) { LOG.debug("?", e); } return null; }
From source file:org.apdplat.search.GoogleAjaxSearcher.java
@Override public SearchResult search(String keyword, int page) { int pageSize = 8; //???8start?/* w w w.j a va 2 s .c o m*/ //?start=0start=10start=20?(page-1)*pageSize String url = "http://ajax.googleapis.com/ajax/services/search/web?start=" + (page - 1) * pageSize + "&rsz=large&v=1.0&q=" + keyword; SearchResult searchResult = new SearchResult(); searchResult.setPage(page); List<Webpage> webpages = new ArrayList<>(); try { HttpClient httpClient = new HttpClient(); GetMethod getMethod = new GetMethod(url); httpClient.executeMethod(getMethod); getMethod.getParams().setParameter(HttpMethodParams.RETRY_HANDLER, new DefaultHttpMethodRetryHandler()); int statusCode = httpClient.executeMethod(getMethod); if (statusCode != HttpStatus.SC_OK) { LOG.error("?: " + getMethod.getStatusLine()); return null; } InputStream in = getMethod.getResponseBodyAsStream(); byte[] responseBody = Tools.readAll(in); String response = new String(responseBody, "UTF-8"); LOG.debug("??" + response); JSONObject json = new JSONObject(response); String totalResult = json.getJSONObject("responseData").getJSONObject("cursor") .getString("estimatedResultCount"); int totalResultCount = Integer.parseInt(totalResult); LOG.info("? " + totalResultCount); searchResult.setTotal(totalResultCount); JSONArray results = json.getJSONObject("responseData").getJSONArray("results"); LOG.debug("?:"); for (int i = 0; i < results.length(); i++) { Webpage webpage = new Webpage(); JSONObject result = results.getJSONObject(i); //??? String title = result.getString("titleNoFormatting"); LOG.debug("" + title); webpage.setTitle(title); //???? String summary = result.get("content").toString(); summary = summary.replaceAll("<b>", ""); summary = summary.replaceAll("</b>", ""); summary = summary.replaceAll("\\.\\.\\.", ""); LOG.debug("?" + summary); webpage.setSummary(summary); //URL??? String _url = result.get("url").toString(); webpage.setUrl(_url); String content = Tools.getHTMLContent(_url); LOG.debug("" + content); webpage.setContent(content); webpages.add(webpage); } } catch (IOException | JSONException | NumberFormatException e) { LOG.error("?", e); } searchResult.setWebpages(webpages); return searchResult; }
From source file:org.mvnsearch.snippet.HttpClientManager.java
/** * create http client instance/*from w ww.ja v a2 s.c o m*/ * * @return HttpClient object */ private static HttpClient createHttpClient() { HttpClient client = new HttpClient(); //HttpClient create HttpClientParams clientParams = client.getParams(); clientParams.setParameter("http.socket.timeout", 10000); //10 seconds for socket waiting clientParams.setParameter("http.connection.timeout", 10000); //10 seconds http connection creation clientParams.setParameter("http.connection-manager.timeout", 3000L); //3 seconds waiting to get connection from http connection manager clientParams.setParameter("http.method.retry-handler", new DefaultHttpMethodRetryHandler()); // if failed, try 3 return client; }
From source file:org.openo.nfvo.monitor.dac.util.APIHttpClient.java
public static String doGet(String url, String queryString, String charset, String token) { HttpClient httpClient = new HttpClient(); httpClient.getHttpConnectionManager().getParams().setConnectionTimeout(5000); GetMethod getMethod = new GetMethod(url); if (!Global.isEmpty(queryString)) { getMethod.setQueryString(queryString); }// ww w .j a va 2 s.co m getMethod.getParams().setParameter(HttpMethodParams.SO_TIMEOUT, 10000); if (!Global.isEmpty(token)) { getMethod.addRequestHeader("X-Auth-Token", token); } getMethod.getParams().setParameter(HttpMethodParams.RETRY_HANDLER, new DefaultHttpMethodRetryHandler()); String response = ""; try { int statusCode = httpClient.executeMethod(getMethod); if (statusCode != HttpStatus.SC_OK) { logger.error("request error: " + getMethod.getStatusLine()); } byte[] responseBody = getMethod.getResponseBody(); response = new String(responseBody, charset); logger.debug("----------response:" + response); } catch (HttpException e) { logger.error("Exception", e); } catch (IOException e) { logger.error("Exception", e); } finally { getMethod.releaseConnection(); } return response; }
From source file:org.openo.nfvo.monitor.umc.util.APIHttpClient.java
public static String doGet(String url, String queryString, String charset, String token) { HttpClient httpClient = new HttpClient(); httpClient.getHttpConnectionManager().getParams().setConnectionTimeout(5000); GetMethod getMethod = new GetMethod(url); if (!Global.isEmpty(queryString)) { getMethod.setQueryString(queryString); }/*from ww w .j av a 2 s . c o m*/ getMethod.getParams().setParameter(HttpMethodParams.SO_TIMEOUT, 10000); if (!Global.isEmpty(token)) { getMethod.addRequestHeader("X-Auth-Token", token); } getMethod.getParams().setParameter(HttpMethodParams.RETRY_HANDLER, new DefaultHttpMethodRetryHandler()); String response = ""; try { int statusCode = httpClient.executeMethod(getMethod); if (statusCode != HttpStatus.SC_OK) { logger.error("request error: " + getMethod.getStatusLine()); } else { byte[] responseBody = getMethod.getResponseBody(); response = new String(responseBody, charset); logger.debug("----------response:" + response); } } catch (HttpException e) { logger.error("Exception", e); } catch (IOException e) { logger.error("Exception", e); } finally { getMethod.releaseConnection(); } return response; }
From source file:org.openremote.beehive.utils.LIRCrawler.java
/** * Gets the html body./* ww w . j a v a 2 s . co m*/ * * @param url * the url * * @return the html body */ private static String getHtmlBody(String url) { String responseBody = ""; GetMethod getMethod = new GetMethod(url); getMethod.getParams().setParameter(HttpMethodParams.SO_TIMEOUT, 30000); getMethod.getParams().setParameter(HttpMethodParams.RETRY_HANDLER, new DefaultHttpMethodRetryHandler()); try { int statusCode = httpClient.executeMethod(getMethod); if (statusCode != HttpStatus.SC_OK) { LOGGER.error("Method failed: " + getMethod.getStatusLine()); } BufferedReader bufferIn = new BufferedReader( new InputStreamReader(getMethod.getResponseBodyAsStream())); StringBuffer sb = new StringBuffer(); char[] buf = new char[1024 * 1024]; int len; while ((len = bufferIn.read(buf)) > 0) { sb.append(buf, 0, len); } responseBody = sb.toString(); } catch (HttpException e) { LOGGER.error("Please check your provided http address " + url, e); return null; } catch (IOException e) { LOGGER.error("Occur the network exception, maybe the url [" + url + "] is unreachable.", e); return null; } finally { getMethod.releaseConnection(); } return responseBody; }
From source file:zz.pseas.ghost.utils.HttpClinetUtil.java
/** * @author GS/*from w w w . ja v a 2s . c o m*/ * @param url * @param para * get?? * @return * @throws IOException */ public static String get(String url, Map<String, String> para) throws IOException { String responseBody = null; GetMethod getMethod = new GetMethod(url); getMethod.getParams().setParameter(HttpMethodParams.RETRY_HANDLER, new DefaultHttpMethodRetryHandler()); // ???? NameValuePair[] data = new NameValuePair[para.size()]; int index = 0; for (String s : para.keySet()) { data[index++] = new NameValuePair(s, para.get(s)); // ?? } getMethod.setQueryString(data); // ? try { int statusCode = hc.executeMethod(getMethod); if (statusCode != HttpStatus.SC_OK) { LOG.error("Method failed: " + getMethod.getStatusLine()); } responseBody = getMethod.getResponseBodyAsString(); } catch (SocketTimeoutException e) { e.printStackTrace(); LOG.error("??" + e.getMessage()); } catch (UnknownHostException e) { e.printStackTrace(); LOG.error("?,UnknownHostException,?" + e.getMessage()); } catch (HttpException e) { // ? e.printStackTrace(); LOG.error("?" + e.getMessage()); } catch (IOException e) { // ? e.printStackTrace(); LOG.error("?" + e.getMessage()); } finally { getMethod.releaseConnection(); // } return responseBody; }
From source file:zz.pseas.ghost.utils.HttpClinetUtil.java
/** * @author GS//from w ww . j a v a2s .c o m * @param url * @return * @throws IOException */ public static String get(String url) throws IOException { String responseBody = null; GetMethod getMethod = new GetMethod(url); getMethod.getParams().setParameter(HttpMethodParams.RETRY_HANDLER, new DefaultHttpMethodRetryHandler()); // ???? try { int statusCode = hc.executeMethod(getMethod); if (statusCode != HttpStatus.SC_OK) { LOG.error("Method failed: " + getMethod.getStatusLine()); } responseBody = getMethod.getResponseBodyAsString(); } catch (SocketTimeoutException e) { e.printStackTrace(); LOG.error("??" + e.getMessage()); } catch (UnknownHostException e) { e.printStackTrace(); LOG.error("??" + e.getMessage()); } catch (HttpException e) { // ? e.printStackTrace(); LOG.error("?" + e.getMessage()); } catch (IOException e) { // ? e.printStackTrace(); LOG.error("?" + e.getMessage()); } finally { getMethod.releaseConnection(); // } return responseBody; }
From source file:zz.pseas.ghost.utils.HttpClinetUtil.java
/** * @author GS/* w w w . jav a 2 s. c o m*/ * @param url * @param para * @param cookie * @return * @throws IOException */ public static String get(String url, Map<String, String> para, String cookie) throws IOException { String responseBody = null; GetMethod getMethod = new GetMethod(url); getMethod.getParams().setParameter(HttpMethodParams.RETRY_HANDLER, new DefaultHttpMethodRetryHandler()); // ???? NameValuePair[] data = new NameValuePair[para.size()]; int index = 0; for (String s : para.keySet()) { data[index++] = new NameValuePair(s, para.get(s)); // ?? } getMethod.setQueryString(data); // ? if (!cookie.equals("")) { getMethod.setRequestHeader("cookie", cookie); } try { int statusCode = hc.executeMethod(getMethod); if (statusCode != HttpStatus.SC_OK) { LOG.error("Method failed: " + getMethod.getStatusLine()); } responseBody = getMethod.getResponseBodyAsString(); } catch (UnknownHostException e) { e.printStackTrace(); LOG.error("??" + e.getMessage()); } catch (HttpException e) { // ? e.printStackTrace(); LOG.error("?" + e.getMessage()); } catch (IOException e) { // ? e.printStackTrace(); LOG.error("?" + e.getMessage()); } finally { getMethod.releaseConnection(); // } return responseBody; }