List of usage examples for org.apache.commons.httpclient HttpClient HttpClient
public HttpClient()
From source file:com.taobao.ad.easyschedule.commons.utils.SmsUtil.java
/** * ??//from w w w . j a va 2s .c o m * * @param list * ? * @param subject * @param content */ public static void sendSMS(String list, String subject, String content) { if ("false".equals(Constants.SENDSMS) || StringUtils.isEmpty(list) || StringUtils.isEmpty(subject)) { return; } try { String command = Constants.SMS_SEND_COMMAND; String strSubject = subject; if (subject.getBytes().length > 50) { strSubject = StringUtil.bSubstring(subject, 49); } String strContent = subject; if (subject.getBytes().length > 150) { // ????content?subjectcontent?? strContent = StringUtil.bSubstring(subject + ":" + content, 149); } command = command.replaceAll("#list#", URLEncoder.encode(list, "GBK")) .replaceAll("#subject#", URLEncoder.encode(strSubject, "GBK")) .replaceAll("#content#", URLEncoder.encode(strContent, "GBK")); HttpClient client = new HttpClient(); client.getHttpConnectionManager().getParams().setConnectionTimeout(Constants.NOTIFY_API_CONN_TIMEOUT); GetMethod getMethod = new GetMethod(command); getMethod.getParams().setParameter(HttpMethodParams.RETRY_HANDLER, new DefaultHttpMethodRetryHandler()); for (int i = 1; i <= 3; i++) { try { int statusCode = client.executeMethod(getMethod); if (statusCode != HttpStatus.SC_OK) { logger.error("SmsUtil.sendWangWang statusCode:" + statusCode); sendSMSByShell(list, "es:" + subject); Thread.sleep(Constants.JOB_RETRY_WAITTIME); continue; } String ret = getMethod.getResponseBodyAsString(); if (!"OK".equals(ret)) { logger.error("Send message failed[" + i + "];list:" + list + ";subject:" + subject + ";content:" + content); sendSMSByShell(list, "es:" + subject); Thread.sleep(Constants.JOB_RETRY_WAITTIME); continue; } break; } catch (Exception e) { logger.error("Send message failed[" + i + "]:" + e.getMessage()); Thread.sleep(Constants.JOB_RETRY_WAITTIME); continue; } } } catch (Exception e) { logger.error("SmsUtil.sendSMS", e); } }
From source file:egpi.tes.ahv.servicio.AutenticacionREST.java
public void init() throws ServletException { try {//w ww . j av a2 s .c om cargarPropiedades(); client = new HttpClient(); status = 0; obj = new JSONObject(); } catch (IOException ex) { Logger.getLogger(AutenticacionREST.class.getName()).log(Level.SEVERE, null, ex); } catch (ReporteJasperException ex) { Logger.getLogger(AutenticacionREST.class.getName()).log(Level.SEVERE, null, ex); } }
From source file:es.carebear.rightmanagement.client.group.GetGroupListOfUser.java
public GetGroupListOfUser(String baseUrl) { this.baseUri = baseUrl; this.httpClient = new HttpClient(); }
From source file:com.taobao.ad.easyschedule.commons.utils.WangWangUtil.java
/** * ??/*from w w w. j a v a 2s.co m*/ * * @param list * ? * @param subject * @param content */ public static void sendWangWang(String list, String subject, String content) { if ("false".equals(Constants.SENDWANGWANG) || StringUtils.isEmpty(list) || StringUtils.isEmpty(subject) || StringUtils.isEmpty(content)) { return; } try { String command = Constants.WANGWANG_SEND_COMMAND; String strSubject = subject; if (subject.getBytes().length > 50) { strSubject = StringUtil.bSubstring(subject, 49); } String strContent = content; if (content.getBytes().length > 900) { strContent = StringUtil.bSubstring(content, 899); } command = command.replaceAll("#list#", URLEncoder.encode(list, "GBK")) .replaceAll("#subject#", URLEncoder.encode(strSubject, "GBK")).replaceAll("#content#", URLEncoder.encode(StringUtils.isEmpty(strContent) ? "null" : strContent, "GBK")); HttpClient client = new HttpClient(); client.getHttpConnectionManager().getParams().setConnectionTimeout(Constants.NOTIFY_API_CONN_TIMEOUT); GetMethod getMethod = new GetMethod(command); getMethod.getParams().setParameter(HttpMethodParams.RETRY_HANDLER, new DefaultHttpMethodRetryHandler()); for (int i = 1; i <= 3; i++) { try { int statusCode = client.executeMethod(getMethod); if (statusCode != HttpStatus.SC_OK) { logger.error("WangWangUtil.sendWangWang statusCode:" + statusCode); Thread.sleep(Constants.JOB_RETRY_WAITTIME); continue; } String ret = getMethod.getResponseBodyAsString(); if (!"OK".equals(ret)) { logger.error("Send message failed[" + i + "];list:" + list + ";subject:" + subject + ";content:" + content); Thread.sleep(Constants.JOB_RETRY_WAITTIME); continue; } break; } catch (Exception e) { logger.error("Send message failed[" + i + "]:" + e.getMessage()); Thread.sleep(Constants.JOB_RETRY_WAITTIME); continue; } } } catch (Exception e) { logger.error("WangWangUtil.sendWangWang", e); } }
From source file:cz.muni.fi.pa165.creatures.rest.client.services.utils.CRUDServiceHelper.java
/** * Helper methods which executes an HTTP {@code method} and writes the * output to the standard output (e.g. console). Return codes of the HTTP * responses are present so we can verify what happened at the server side. * * @param method method to send to the server side *//*ww w . ja v a 2s . c om*/ public static void send(HttpMethodBase method) { HttpClient httpClient = new HttpClient(); try { int result = httpClient.executeMethod(method); System.out.println(RESPONSE_STATUS_CODE + result); ResponseCode response = ResponseCode.fromInt(result); if (response != ResponseCode.NOT_FOUND && response != ResponseCode.SERVER_ERROR && response != ResponseCode.FORBIDDEN) { System.out.println(RESPONSE_HEADER); Header[] headers = method.getResponseHeaders(); for (Header h : headers) { System.out.println(h.toString()); } InputStreamReader isr = new InputStreamReader(method.getResponseBodyAsStream()); BufferedReader br = new BufferedReader(isr); String line; while ((line = br.readLine()) != null) { System.out.println(line); } } } catch (ConnectException ex) { logger.log(Level.WARNING, CONNECTION_EXCEPTION_MSG); } catch (IOException ex) { logger.log(Level.INFO, ex.getMessage()); } finally { method.releaseConnection(); } }
From source file:com.dianping.lion.service.impl.HttpMailServiceImpl.java
public void init() { httpClient = new HttpClient(); httpClient.getHostConfiguration().setHost(host, port, protocal); }
From source file:edu.uci.ics.external.connector.asterixdb.ConnectorUtils.java
public static DatasetInfo retrieveDatasetInfo(StorageParameter storageParameter) throws Exception { HttpClient client = new HttpClient(); String requestStr = storageParameter.getServiceURL() + "/connector" + "?dataverseName=" + storageParameter.getDataverseName() + "&datasetName=" + storageParameter.getDatasetName(); // Create a method instance. GetMethod method = new GetMethod(requestStr); // Provide custom retry handler is necessary method.getParams().setParameter(HttpMethodParams.RETRY_HANDLER, new DefaultHttpMethodRetryHandler(3, false)); // Deals with the response. // Executes the method. try {/* w ww. j av a 2s . co m*/ int statusCode = client.executeMethod(method); if (statusCode != HttpStatus.SC_OK) { System.err.println("Method failed: " + method.getStatusLine()); } // Deals with the response. JSONTokener tokener = new JSONTokener(new InputStreamReader(method.getResponseBodyAsStream())); JSONObject response = new JSONObject(tokener); // Checks if there are errors. String error = "error"; if (response.has(error)) { throw new IllegalStateException("AsterixDB returned errors: " + response.getString(error)); } // Extracts record type and file partitions. boolean temp = extractTempInfo(response); ARecordType recordType = extractRecordType(response); List<FilePartition> filePartitions = extractFilePartitions(response); IFileSplitProvider fileSplitProvider = createFileSplitProvider(storageParameter, filePartitions); String[] locations = getScanLocationConstraints(filePartitions, storageParameter.getIpToNcNames()); String[] primaryKeys = response.getString("keys").split(","); DatasetInfo datasetInfo = new DatasetInfo(locations, fileSplitProvider, recordType, primaryKeys, temp); return datasetInfo; } catch (Exception e) { throw e; } finally { method.releaseConnection(); } }
From source file:es.carebear.rightmanagement.client.group.GetUserlistGroup.java
public GetUserlistGroup(String baseUri) { this.baseUri = baseUri; this.httpClient = new HttpClient(); }
From source file:com.nfl.dm.clubsites.cms.articles.categorization.OpenCalaisRESTPost.java
public Map<String, ArrayList<String>> execute(Node node) { OpenCalaisRESTPost openCalaisRESTPost = new OpenCalaisRESTPost(); try {/* www .j a v a 2s . c o m*/ openCalaisRESTPost.input = node.getProperty("body").getString(); } catch (RepositoryException e) { e.printStackTrace(); } openCalaisRESTPost.client = new HttpClient(); openCalaisRESTPost.client.getParams().setParameter("http.useragent", "Calais Rest Client"); return openCalaisRESTPost.run(); }
From source file:edu.du.penrose.systems.util.HttpClientUtils_2.java
static public WebPage academicSearch(URL initialLink) { WebPage wp = new WebPage(); HttpClient client = new HttpClient(); GetMethod getMethod = new GetMethod(initialLink.toString()); getMethod.setFollowRedirects(true);//from www . j a v a 2 s.c o m try { // System.setProperty("org.apache.commons.logging.Log", "org.apache.commons.logging.impl.SimpleLog"); // System.setProperty("org.apache.commons.logging.simplelog.showdatetime", "true"); // System.setProperty("org.apache.commons.logging.simplelog.log.httpclient.wire.header", "debug"); // System.setProperty("org.apache.commons.logging.simplelog.log.org.apache.commons.httpclient", "debug"); // getMethod.setRequestHeader("User-Agent", "Mozilla/5.001 (windows; U; NT4.0; en-US; rv:1.0) Gecko/25250101"); // in case they check int statusCode = client.executeMethod(getMethod); String contents = getMethod.getResponseBodyAsString(); String formPage = getMethod.getURI().toString(); // page we were sent to. wp.setStatus(statusCode); wp.setWebPage(contents); // System.out.println();System.out.println();System.out.println();System.out.println();System.out.println(contents); String sid = getSID(contents); String postURL = "http://web.ebscohost.com/ehost/search?vid=1&hid=9&sid=" + sid; PostMethod postMethod = new PostMethod( "http://web.ebscohost.com/ehost/search?vid=1&hid=9&sid=c3220986-66e4-4fd1-ba6a-30504f3694ad%40sessionmgr13"); postMethod.setRequestHeader("User-Agent", "Mozilla/5.001 (windows; U; NT4.0; en-US; rv:1.0) Gecko/25250101"); // in case they check // postMethod.setFollowRedirects( true ); causes error postMethod.setParameter(SESSION_ID, sid); setResolution(postMethod, contents); setNonchangingFields(postMethod); statusCode = client.executeMethod(postMethod); if (statusCode > 300) { String resultsPage = getMethod.getURI().toString(); // page we were sent to. getMethod = new GetMethod(resultsPage); getMethod.setRequestHeader("User-Agent", "Mozilla/5.001 (windows; U; NT4.0; en-US; rv:1.0) Gecko/25250101"); // in case they check statusCode = client.executeMethod(getMethod); contents = getMethod.getResponseBodyAsString(); wp.setStatus(statusCode); wp.setWebPage(contents); } contents = postMethod.getResponseBodyAsString(); wp.setStatus(statusCode); wp.setWebPage(contents); System.out.println(); System.out.println(); System.out.println(); System.out.println(); System.out.println(contents); } catch (Exception e) { System.out.println(e); } finally { getMethod.releaseConnection(); } return wp; }