List of usage examples for org.apache.commons.httpclient HttpMethod getResponseBodyAsString
public abstract String getResponseBodyAsString() throws IOException;
From source file:edu.berkeley.ground.plugins.hive.util.PluginUtil.java
public static String execute(HttpMethod method) throws IOException, HttpException, JsonParseException, JsonMappingException { if (client.executeMethod(method) == HttpURLConnection.HTTP_OK) { // getting the nodeId of the node created return method.getResponseBodyAsString(); }/*from ww w .j ava 2 s. co m*/ return null; }
From source file:atg.taglib.json.Helper.java
/** * Get a response from the server for a test * * @param pTestUrl The test url, relative to the tests context root * @return the <code>ResponseData</code> object for this test * @throws IOException //from ww w . j a va 2 s . c o m * @throws HttpException * @throws JSONException */ public static ResponseData getData(String pType, int pTestNum) throws HttpException, IOException, JSONException { // Setup HTTP client HttpClient client = new HttpClient(); HttpMethod method = new GetMethod(getTestUrl(pType, pTestNum)); // Execute the GET request, capturing response status int statusCode = client.executeMethod(method); String responseBody = method.getResponseBodyAsString(); method.releaseConnection(); // Create response data object ResponseData data = new ResponseData(); data.body = responseBody.trim(); data.statusCode = statusCode; if (statusCode == HttpStatus.SC_OK) { // Parse the JSON response data.json = parseJsonTextToObject(responseBody); } // Get the expected result method = new GetMethod(getStatus200ResultUrl(pType, pTestNum)); data.expectedStatusCode = HttpStatus.SC_OK; // Execute the GET request, capturing response status statusCode = client.executeMethod(method); if (statusCode == HttpStatus.SC_NOT_FOUND) { // Test result wasn't found - we must be expecting an error for this test method.releaseConnection(); method = new GetMethod(getStatus500ResultUrl(pType, pTestNum)); statusCode = client.executeMethod(method); data.expectedStatusCode = HttpStatus.SC_INTERNAL_SERVER_ERROR; } responseBody = method.getResponseBodyAsString().trim(); method.releaseConnection(); // Parse the expected data if (data.expectedStatusCode == HttpStatus.SC_OK) { // Parse body into JSON object data.expectedJson = parseJsonTextToObject(responseBody); } else { // Exception is expected, set the expected key data.expectedMsgKey = responseBody.trim(); } return data; }
From source file:edu.berkeley.ground.plugins.hive.util.PluginUtil.java
public static List<Long> getVersionList(HttpMethod method) throws JsonParseException, JsonMappingException, HttpException, IOException { if (client.executeMethod(method) == HttpURLConnection.HTTP_OK) { // getting the nodeId of the node created String response = method.getResponseBodyAsString(); ObjectMapper objectMapper = new ObjectMapper(); JsonNode jsonNode = objectMapper.readValue(response, JsonNode.class); List<Long> list = objectMapper.readValue(objectMapper.treeAsTokens(jsonNode), new TypeReference<List<Long>>() { });/* w w w . j ava2s .c o m*/ return list; } return null; }
From source file:jshm.sh.Api.java
public static void submitGhScore(final GhScore score) throws Exception { Client.getAuthCookies();//from w w w . jav a 2 s. c om new HttpForm((Object) URLs.gh.getInsertScoreUrl(score), "song", String.valueOf(score.getSong().getScoreHeroId()), "score", String.valueOf(score.getScore()), "stars", score.getRating() != 0 ? String.valueOf(score.getRating()) : "", "percent", score.getHitPercent() != 0.0f ? String.valueOf((int) (score.getHitPercent() * 100)) : "", "streak", score.getStreak() != 0 ? String.valueOf(score.getStreak()) : "", "comment", score.getComment(), "link", score.getImageUrl(), "videolink", score.getVideoUrl()) { @Override public void afterSubmit(final int response, final HttpClient client, final HttpMethod method) throws Exception { // TODO more vigorous error handing? String body = method.getResponseBodyAsString(); // LOG.finest("submitGhScore() result body:"); // LOG.finest("\n" + body); method.releaseConnection(); Matcher m = ERROR_PATTERN.matcher(body); if (m.matches()) { Exception e = new ClientException(m.group(1)); LOG.throwing("Api", "submitGhScore", e); throw e; } // can't be completely sure about this if (body.contains("window.close()")) { score.setStatus(Score.Status.SUBMITTED); } else { score.setStatus(Score.Status.UNKNOWN); LOG.warning("Score may not have been accepted, response body follows:"); LOG.warning(body); } score.setSubmissionDate(new java.util.Date()); Session sess = null; Transaction tx = null; try { sess = HibernateUtil.getCurrentSession(); tx = sess.beginTransaction(); sess.update(score); sess.getTransaction().commit(); } catch (Exception e) { if (null != tx) tx.rollback(); LOG.throwing("Api", "submitGhScore", e); throw e; } finally { if (null != sess && sess.isOpen()) sess.close(); } } }.submit(); }
From source file:jshm.sh.Api.java
public static void submitRbScore(final RbScore score) throws Exception { Client.getAuthCookies();//from w ww . jav a 2 s. c o m String[] staticData = { "song", String.valueOf(score.getSong().getScoreHeroId()), "game", String.valueOf(score.getGame().scoreHeroId), "platform", String.valueOf(RbPlatform.getId(score.getGame().platform)), "group", String.valueOf(score.getGroup().rockbandId), "score", String.valueOf(score.getScore()), "rating", score.getRating() != 0 ? String.valueOf(score.getRating()) : "", "comment", score.getComment(), "link", score.getImageUrl(), "videolink", score.getVideoUrl() }; List<String> data = new ArrayList<String>(Arrays.asList(staticData)); // TODO check on xxxUser being required or not for (Part p : score.getParts()) { final String istr = p.getInstrument().toShortString(); data.add(istr + "Diff"); data.add(String.valueOf(p.getDifficulty().scoreHeroId)); data.add(istr + "Name"); data.add(p.getPerformer()); data.add(istr + "Percent"); data.add(p.getHitPercent() != 0.0f ? String.valueOf((int) (p.getHitPercent() * 100)) : ""); data.add(istr + "Streak"); data.add(p.getStreak() != 0 ? String.valueOf(p.getStreak()) : ""); } new HttpForm((Object) URLs.rb.getInsertScoreUrl(score), data) { @Override public void afterSubmit(final int response, final HttpClient client, final HttpMethod method) throws Exception { // TODO more vigorous error handing? String body = method.getResponseBodyAsString(); // LOG.finest("submitRbScore() result body:"); // LOG.finest("\n" + body); method.releaseConnection(); Matcher m = ERROR_PATTERN.matcher(body); if (m.matches()) { Exception e = new ClientException(m.group(1)); LOG.throwing("Api", "submitRbScore", e); throw e; } // can't be completely sure about this if (body.contains("window.close()")) { score.setStatus(Score.Status.SUBMITTED); } else { score.setStatus(Score.Status.UNKNOWN); LOG.warning("Score may not have been accepted, response body follows:"); LOG.warning(body); } score.setSubmissionDate(new java.util.Date()); Session sess = null; Transaction tx = null; try { sess = HibernateUtil.getCurrentSession(); tx = sess.beginTransaction(); sess.update(score); tx.commit(); } catch (Exception e) { if (null != tx) tx.rollback(); LOG.throwing("Api", "submitRbScore", e); throw e; } finally { if (null != sess && sess.isOpen()) sess.close(); } } }.submit(); }
From source file:jshm.sh.Api.java
public static void submitWtScore(final WtScore score) throws Exception { Client.getAuthCookies();/*from w w w. j a v a 2s. c o m*/ String[] staticData = { "song", String.valueOf(score.getSong().getScoreHeroId()), "game", String.valueOf(score.getGame().scoreHeroId), "platform", String.valueOf(RbPlatform.getId(score.getGame().platform)), "group", String.valueOf(((WtGameTitle) score.getGameTitle()).scoreHeroGroupId), "inst", String.valueOf(score.getGroup().worldTourId), "score", String.valueOf(score.getScore()), "rating", score.getRating() != 0 ? String.valueOf(score.getRating()) : "", "comment", score.getComment(), "link", score.getImageUrl(), "videolink", score.getVideoUrl() }; List<String> data = new ArrayList<String>(Arrays.asList(staticData)); // TODO check on xxxUser being required or not for (Part p : score.getParts()) { final String istr = p.getInstrument().toShortString(); data.add(istr + "Diff"); data.add(String.valueOf(p.getDifficulty().scoreHeroId)); data.add(istr + "Name"); data.add(p.getPerformer()); data.add(istr + "Percent"); data.add(p.getHitPercent() != 0.0f ? String.valueOf((int) (p.getHitPercent() * 100)) : ""); data.add(istr + "Streak"); data.add(p.getStreak() != 0 ? String.valueOf(p.getStreak()) : ""); } new HttpForm((Object) URLs.wt.getInsertScoreUrl(score), data) { @Override public void afterSubmit(final int response, final HttpClient client, final HttpMethod method) throws Exception { // TODO more vigorous error handing? String body = method.getResponseBodyAsString(); // LOG.finest("submitRbScore() result body:"); // LOG.finest("\n" + body); method.releaseConnection(); Matcher m = ERROR_PATTERN.matcher(body); if (m.matches()) { Exception e = new ClientException(m.group(1)); LOG.throwing("Api", "submitRbScore", e); throw e; } // can't be completely sure about this if (body.contains("window.close()")) { score.setStatus(Score.Status.SUBMITTED); } else { score.setStatus(Score.Status.UNKNOWN); LOG.warning("Score may not have been accepted, response body follows:"); LOG.warning(body); } score.setSubmissionDate(new java.util.Date()); Session sess = null; Transaction tx = null; try { sess = HibernateUtil.getCurrentSession(); tx = sess.beginTransaction(); sess.update(score); tx.commit(); } catch (Exception e) { if (null != tx) tx.rollback(); LOG.throwing("Api", "submitWtScore", e); throw e; } finally { if (null != sess && sess.isOpen()) sess.close(); } } }.submit(); }
From source file:it.haefelinger.flaka.util.HttpUpload.java
protected static String getResponseFrom(HttpMethod meth) { String r = null;/*from ww w .java2 s. c om*/ try { r = meth.getResponseBodyAsString(); } catch (Exception e) { syslog("error reading HTTP response .." + e.getMessage()); } if (r == null) { syslog("* empty response seen .."); r = ""; } return r; }
From source file:cn.newtouch.util.HttpClientUtil.java
/** * ?url??post/* w ww .j a v a 2s . c o m*/ * * @param url * @param paramsStr * ? * @param method * @return String ? */ public static String post(String url, String paramsStr, String method) { HttpClient client = new HttpClient(); HttpMethod httpMethod = new PostMethod(url); httpMethod.setQueryString(paramsStr); try { int returnCode = client.executeMethod(httpMethod); if (returnCode == HttpStatus.SC_OK) { return httpMethod.getResponseBodyAsString(); } else if (returnCode == HttpStatus.SC_NOT_IMPLEMENTED) { System.err.println("The Post method is not implemented by this URI"); } } catch (Exception e) { System.err.println(e); } finally { httpMethod.releaseConnection(); } return null; }
From source file:demo.jaxrs.client.Client.java
private static void handleHttpMethod(HttpMethod httpMethod) throws Exception { HttpClient client = new HttpClient(); try {/* ww w .jav a2 s . c o m*/ int statusCode = client.executeMethod(httpMethod); System.out.println("Response status : " + statusCode); Response.Status status = Response.Status.fromStatusCode(statusCode); if (status == Response.Status.OK) { System.out.println(httpMethod.getResponseBodyAsString()); } else if (status == Response.Status.FORBIDDEN) { System.out.println("Authorization failure"); } else if (status == Response.Status.UNAUTHORIZED) { System.out.println("Authentication failure"); } System.out.println(); } finally { // release any connection resources used by the method httpMethod.releaseConnection(); } }
From source file:edu.pitt.dbmi.facebase.hd.HumanDataController.java
/** http web client class used for communicating with Hub * @param action String is one of status, update, event * @param signal String is a timeInSeconds or QueueID or eventCode * for action=status, signal is "0", number-of-seconds-until-back, * for action=update, signal is qid.hash * for action=event, signal "1" means mysql server is unavailable. * uses getOTP() method for URL construction */// ww w.j av a 2s .c om public static void httpGetter(String action, String signal) { String oneTimePassword = (new Long(getOTP())).toString(); String url = hubURL + responseTrigger + oneTimePassword + "/"; if (action == "update") { url += action + "/" + signal; } else if (action == "status") { long time = System.currentTimeMillis() / 1000L; time += Long.parseLong(signal); Long timeLong; timeLong = new Long(time); url += action + "/" + timeLong.toString(); } else if (action == "event") { url += action + "/" + signal; } else { } HttpClient webClient = new HttpClient(); HttpMethod httpMethod = new GetMethod(url); try { webClient.executeMethod(httpMethod); String response = httpMethod.getResponseBodyAsString(); } catch (HttpException httpe) { } catch (IOException ioe) { } finally { httpMethod.releaseConnection(); httpMethod.recycle(); } }