List of usage examples for org.apache.commons.httpclient HttpMethod getResponseBodyAsString
public abstract String getResponseBodyAsString() throws IOException;
From source file:edu.uci.ics.asterix.test.aql.TestsUtils.java
private static int executeHttpMethod(HttpMethod method) throws Exception { HttpClient client = new HttpClient(); int statusCode; try {//from ww w . j a v a 2s. co m statusCode = client.executeMethod(method); } catch (Exception e) { GlobalConfig.ASTERIX_LOGGER.log(Level.SEVERE, e.getMessage(), e); e.printStackTrace(); throw e; } if (statusCode != HttpStatus.SC_OK) { // QQQ For now, we are indeed assuming we get back JSON errors. // In future this may be changed depending on the requested // output format sent to the servlet. String errorBody = method.getResponseBodyAsString(); JSONObject result = new JSONObject(errorBody); String[] errors = { result.getJSONArray("error-code").getString(0), result.getString("summary"), result.getString("stacktrace") }; GlobalConfig.ASTERIX_LOGGER.log(Level.SEVERE, errors[2]); throw new Exception("HTTP operation failed: " + errors[0] + "\nSTATUS LINE: " + method.getStatusLine() + "\nSUMMARY: " + errors[1] + "\nSTACKTRACE: " + errors[2]); } return statusCode; }
From source file:com.ibm.watson.apis.conversation_enhanced.filters.LookUp.java
private String sendGetRequest(String url) throws HttpException, IOException { HttpMethod httpMethod = getHttpMethod(url); try {//from ww w . j a v a 2s. c o m getHttpClient().executeMethod(httpMethod); return httpMethod.getResponseBodyAsString(); } finally { httpMethod.releaseConnection(); } }
From source file:it.intecs.pisa.toolbox.util.URLReader.java
public static Object getURLContent(String host, String url, int port) throws Exception { HttpMethod method; try {//from w ww.j a v a 2 s . com method = new GetMethod(url); HttpConnection conn = new HttpConnection(host, port); String proxyHost; String proxyPort; if ((proxyHost = System.getProperty("http.proxyHost")) != null && (proxyPort = System.getProperty("http.proxyPort")) != null) { conn.setProxyHost(proxyHost); conn.setProxyPort(Integer.parseInt(proxyPort)); } conn.open(); method.execute(new HttpState(), conn); String inpTmp = method.getResponseBodyAsString(); Reader in = new InputStreamReader(method.getResponseBodyAsStream()); StringBuffer out = new StringBuffer(); char[] buffer = new char[1024]; for (int count = in.read(buffer); count >= 0; count = in.read(buffer)) { out.append(buffer, 0, count); } return out.toString(); } catch (Exception e) { e.printStackTrace(System.out); return null; } }
From source file:com.feilong.tools.net.httpclient3.HttpClientUtil.java
/** * HttpMethod.//w w w . j a v a 2 s. c om * * @param httpMethod * httpMethod * @param httpClientConfig * the http client config * @return the http method response body as string * @throws HttpClientException * ?HttpClientUtilException? */ private static String getHttpMethodResponseBodyAsString(HttpMethod httpMethod, HttpClientConfig httpClientConfig) throws HttpClientException { try { httpMethod = executeMethod(httpMethod, httpClientConfig); //httpMethod.getParams().setContentCharset(charSet); // ?getResponseBodybyte? // ?getResponseBodyAsStringString?String????String??"?"?? // ?getResponseBodyAsStream???? // ? String responseBodyAsString = httpMethod.getResponseBodyAsString(); if (log.isDebugEnabled()) { Map<String, Object> map = getHttpMethodResponseAttributeMapForLog(httpMethod, httpClientConfig); log.debug("getHttpMethodResponseAttributeMapForLog:{}", JsonUtil.format(map)); } return responseBodyAsString; } catch (Exception e) { log.error(e.getClass().getName(), e); throw new HttpClientException(e); } finally { // httpMethod.releaseConnection(); } }
From source file:gov.nih.nci.cagwas.web.action.RemoteContentHelper.java
/** * getContent will connect to the passed in address and read in the contents and * return them as a String./*from ww w.j a v a2 s . co m*/ * <P> * @param addr The URL address to read the contents from * @return String the contents or null if unable to connect */ private String getContent(String addr) { String responseBody = null; HttpClient client = new HttpClient(); client.getHttpConnectionManager().getParams().setConnectionTimeout(5000); HttpMethod method = new GetMethod(addr); method.setFollowRedirects(true); try { client.executeMethod(method); responseBody = method.getResponseBodyAsString(); } catch (HttpException e) { logger.error("Error connecting to remote site", e); } catch (IOException e) { logger.error("Error connecting to remote site", e); } return responseBody; }
From source file:com.intellij.plugins.firstspirit.languagesupport.FirstSpiritClassPathHack.java
public void addFirstSpiritClientJar(UrlClassLoader classLoader, HttpScheme schema, String serverName, int port, String firstSpiritUserName, String firstSpiritUserPassword) throws Exception { System.out.println("starting to download fs-client.jar"); String resolvalbleAddress = null; // asking DNS for IP Address, if some error occur choose the given value from user try {/* w w w .j a v a 2 s . c o m*/ InetAddress address = InetAddress.getByName(serverName); resolvalbleAddress = address.getHostAddress(); System.out.println("Resolved address: " + resolvalbleAddress); } catch (Exception e) { System.err.println("DNS cannot resolve address, using your given value: " + serverName); resolvalbleAddress = serverName; } String uri = schema + "://" + resolvalbleAddress + ":" + port + "/clientjar/fs-client.jar"; String versionUri = schema + "://" + resolvalbleAddress + ":" + port + "/version.txt"; String tempDirectory = System.getProperty("java.io.tmpdir"); System.out.println(uri); System.out.println(versionUri); HttpClient client = new HttpClient(); HttpMethod getVersion = new GetMethod(versionUri); client.executeMethod(getVersion); String currentServerVersionString = getVersion.getResponseBodyAsString(); System.out .println("FirstSpirit server you want to connect to is at version: " + currentServerVersionString); File fsClientJar = new File(tempDirectory, "fs-client-" + currentServerVersionString + ".jar"); if (!fsClientJar.exists()) { // get an authentication cookie from FirstSpirit HttpMethod post = new PostMethod(uri); post.setQueryString("login.user=" + URLEncoder.encode(firstSpiritUserName, "UTF-8") + "&login.password=" + URLEncoder.encode(firstSpiritUserPassword, "UTF-8") + "&login=webnonsso"); client.executeMethod(post); String setCookieJsession = post.getResponseHeader("Set-Cookie").getValue(); // download the fs-client.jar by using the authentication cookie HttpMethod get = new GetMethod(uri); get.getParams().setCookiePolicy(CookiePolicy.IGNORE_COOKIES); get.setRequestHeader("Cookie", setCookieJsession); client.executeMethod(get); InputStream inputStream = get.getResponseBodyAsStream(); FileOutputStream outputStream = new FileOutputStream(fsClientJar); outputStream.write(IOUtils.readFully(inputStream, -1, false)); outputStream.close(); System.out.println("tempfile of fs-client.jar created within path: " + fsClientJar); } addFile(classLoader, fsClientJar); }
From source file:edu.berkeley.ground.plugins.hive.GroundReadWrite.java
String checkStatus(HttpMethod method) throws GroundException { try {//w w w . j ava 2 s.c om if (PluginUtil.client.executeMethod(method) == HttpURLConnection.HTTP_OK) { ObjectMapper objectMapper = new ObjectMapper(); String text = method.getResponseBodyAsString(); JsonNode jsonNode = objectMapper.readValue(text, JsonNode.class); JsonNode nodeId = jsonNode.get("id"); return nodeId.asText(); } } catch (IOException e) { throw new GroundException(e); } return null; }
From source file:net.sourceforge.jcctray.model.HTTPCruise.java
private boolean isInvokeSuccessful(HttpMethod method, DashBoardProject project) throws InvocationException { try {//from w ww. java2 s.c om String message = getSuccessMessage(project); boolean invokeSuccessful = (method.getResponseBodyAsString().indexOf(message) > -1); if (!invokeSuccessful) getLog().error( "Could not find the string '" + message + "' in the page located at " + method.getURI()); return invokeSuccessful; } catch (IOException e) { getLog().error("Attempted to force the build, but the force was not successful", e); throw new InvocationException("Attempted to force the build, but the force was not successful", e); } }
From source file:com.twitter.tokyo.kucho.SeatingList.java
String loadJSON(String url) throws IOException { HttpClient client = new HttpClient(); HttpMethod getMethod = new GetMethod(url); int statusCode = client.executeMethod(getMethod); if (statusCode != HttpStatus.SC_OK) { throw new IOException("Server returned " + statusCode); }//from www .j av a 2s . com return getMethod.getResponseBodyAsString(); }
From source file:net.sf.sail.webapp.domain.webservice.http.AbstractHttpRequest.java
/** * Logs the HttpMethod response information * /*from w ww. ja va2 s . com*/ * @param method the HttpMethod response. * @param actualStatusCode The status code retrieved from the response. * @throws IOException If the response body cannot be retrieved. */ protected void logMethodInfo(HttpMethod method, int actualStatusCode) throws IOException { if (logger.isWarnEnabled()) { logger.warn(actualStatusCode + ": " + method.getStatusText()); logger.warn("body: " + method.getResponseBodyAsString()); } }