List of usage examples for org.apache.http.client.methods CloseableHttpResponse getEntity
HttpEntity getEntity();
From source file:com.abc.turkey.service.unfreeze.UnfreezeProxy.java
private int checkState2(String randstr) throws Exception { // ?/* w w w. j a v a2s . co m*/ String lockUrl = "https://aq.qq.com/cn2/login_limit/checkstate?from=2&verifycode=" + randstr; HttpGet httpGet = new HttpGet(lockUrl); CloseableHttpResponse response = httpclient.execute(httpGet); String res = EntityUtils.toString(response.getEntity(), "utf8"); logger.debug(res); response.close(); return JSON.parseObject(res).getIntValue("if_lock"); }
From source file:org.jenkinsci.plugins.kubernetesworkflowsteps.KubeStepExecution.java
protected Object parse(CloseableHttpResponse resp) throws IOException { try {/*from w w w . j a va 2s . c o m*/ return (new JsonSlurper()).parse((new InputStreamReader(resp.getEntity().getContent()))); } finally { resp.close(); } }
From source file:guru.nidi.ramltester.ServletTest.java
@Test public void testServletOk() throws IOException { final HttpGet get = new HttpGet(url("data")); final CloseableHttpResponse response = client.execute(get); assertEquals("\"json string\"", EntityUtils.toString(response.getEntity())); assertTrue(testFilter.report.isEmpty()); }
From source file:com.pingidentity.adapters.idp.mobileid.restservice.MssRequestHandlerRest.java
public MssSignatureResponseJson sendSignatureRequest(MssSignatureRequestJson signatureRequest) { try {//from ww w.ja v a 2 s . com HttpPost httpRequest = new HttpPost(mssServiceUrl); httpRequest.addHeader("Content-Type", "application/json;charset=UTF-8"); httpRequest.addHeader("Accept", "application/json"); httpRequest.setEntity(new StringEntity(signatureRequest.getRequestMessage())); CloseableHttpResponse httpResponse = httpClient.execute(httpRequest); String responseMessage = EntityUtils.toString(httpResponse.getEntity()); return new MssSignatureResponseJson.Builder().responseMessage(responseMessage).build(); } catch (IOException e) { throw new RuntimeException("Failed to send the request or to receive the response"); } }
From source file:ar.edu.ubp.das.src.chat.actions.ActualizarMensajesAction.java
@Override public ForwardConfig execute(ActionMapping mapping, DynaActionForm form, HttpServletRequest request, HttpServletResponse response) throws SQLException, RuntimeException { try (CloseableHttpClient httpClient = HttpClientBuilder.create().build()) { //prepare http get SalaBean sala = (SalaBean) request.getSession().getAttribute("sala"); String ultimo_mensaje = String.valueOf(request.getSession().getAttribute("ultimo_mensaje")); String authToken = String.valueOf(request.getSession().getAttribute("token")); if (ultimo_mensaje.equals("null") || ultimo_mensaje.isEmpty()) { ultimo_mensaje = "-1"; }// w ww . j a v a 2 s . c o m URIBuilder builder = new URIBuilder(); builder.setScheme("http").setHost("25.136.78.82").setPort(8080) .setPath("/mensajes/sala/" + sala.getId()); builder.setParameter("ultimo_mensaje", ultimo_mensaje); HttpGet getRequest = new HttpGet(); getRequest.setURI(builder.build()); getRequest.addHeader("Authorization", "BEARER " + authToken); getRequest.addHeader("accept", "application/json; charset=ISO-8859-1"); CloseableHttpResponse getResponse = httpClient.execute(getRequest); HttpEntity responseEntity = getResponse.getEntity(); StatusLine responseStatus = getResponse.getStatusLine(); String restResp = EntityUtils.toString(responseEntity); if (responseStatus.getStatusCode() != 200) { throw new RuntimeException(restResp); } //parse message data from response Gson gson = new Gson(); Type listType = new TypeToken<LinkedList<MensajeBean>>() { }.getType(); List<MensajeBean> mensajes = gson.fromJson(restResp, listType); if (!mensajes.isEmpty()) { MensajeBean ultimo = mensajes.get(mensajes.size() - 1); request.getSession().removeAttribute("ultimo_mensaje"); request.getSession().setAttribute("ultimo_mensaje", ultimo.getId_mensaje()); } if (!ultimo_mensaje.equals("-1")) { request.setAttribute("mensajes", mensajes); } return mapping.getForwardByName("success"); } catch (IOException | URISyntaxException | RuntimeException e) { String id_sala = (String) request.getSession().getAttribute("id_sala"); request.setAttribute("message", "Error al intentar actualizar mensajes de Sala " + id_sala + ": " + e.getMessage()); response.setStatus(400); return mapping.getForwardByName("failure"); } }
From source file:eu.fusepool.p3.transformer.web.client.server.RootResource.java
private ClosableEntity getEntity(String resourceUri, Set<MimeType> supportedInputFormats) throws IOException { final CloseableHttpClient httpclient = HttpClients.createDefault(); final HttpGet request = new HttpGet(resourceUri); request.setHeader("Accept", toAcceptHeaderValue(supportedInputFormats.iterator())); final CloseableHttpResponse response = httpclient.execute(request); final HttpEntity httpEntity = response.getEntity(); httpEntity.getContent();/*from ww w .jav a 2 s . co m*/ return new ClosableInputStreamEntity() { @Override public MimeType getType() { try { return new MimeType(httpEntity.getContentType().getValue()); } catch (MimeTypeParseException ex) { throw new RuntimeException(ex); } } @Override public InputStream getData() throws IOException { return httpEntity.getContent(); } @Override public void close() throws IOException { response.close(); } }; }
From source file:org.fedoraproject.jenkins.plugins.copr.CoprClient.java
private String doPost(String username, String coprname, List<NameValuePair> params, String url) throws IOException { CloseableHttpClient httpclient = HttpClients.createDefault(); HttpPost httppost = new HttpPost(new URL(apiurl, url).toString()); try {/*from w w w . j a v a 2 s . c o m*/ httppost.setHeader("Authorization", "Basic " + Base64 .encodeBase64String(String.format("%s:%s", this.apilogin, this.apitoken).getBytes("UTF-8"))); } catch (UnsupportedEncodingException e) { // here goes trouble throw new AssertionError(e); } if (params != null) { UrlEncodedFormEntity entity = new UrlEncodedFormEntity(params, Consts.UTF_8); httppost.setEntity(entity); } String result; CloseableHttpResponse response = httpclient.execute(httppost); result = EntityUtils.toString(response.getEntity()); response.close(); httpclient.close(); return result; }
From source file:TTrestclient.TeachTimeRESTclient.java
private void execute_and_dump(HttpRequestBase request) { try {// w ww . j a v a2 s . c o m System.out.println("Metodo: " + request.getMethod()); System.out.println("URL: " + request.getURI()); if (request.getFirstHeader("Accept") != null) { System.out.println(request.getFirstHeader("Accept")); } if (request.getMethod().equals("POST")) { HttpEntity e = ((HttpPost) request).getEntity(); System.out.print("Payload: "); e.writeTo(System.out); System.out.println(); System.out.println("Tipo payload: " + e.getContentType()); } //eseguiamo la richiesta CloseableHttpResponse response = client.execute(request); try { //preleviamo il contenuto della risposta HttpEntity entity = response.getEntity(); if (entity != null) { entity.writeTo(System.out); System.out.println(); } //controlliamo lo status //if (response.getStatusLine().getStatusCode() != 200) { System.out.println("Return status: " + response.getStatusLine().getReasonPhrase() + " (" + response.getStatusLine().getStatusCode() + ")"); //} } finally { //chiudiamo la risposta response.close(); } } catch (IOException ex) { Logger.getLogger(TeachTimeRESTclient.class.getName()).log(Level.SEVERE, null, ex); } }
From source file:guru.nidi.ramltester.ServletTest.java
@Test public void testServletNok() throws IOException { final HttpGet get = new HttpGet(url("data?param=bu")); final CloseableHttpResponse response = client.execute(get); assertEquals("illegal json", EntityUtils.toString(response.getEntity())); assertEquals(violations("Query parameter 'param' on action(GET /data) is not defined"), testFilter.report.getRequestViolations()); assertEquals(violations(/*from ww w .j a v a 2s .c o m*/ "Body does not match schema for action(GET /data) response(200) mime-type('abc/xyz+json')\n" + "Content: illegal json\n" + "Message: Schema invalid: Unrecognized token 'illegal': was expecting ('true', 'false' or 'null')\n" + " at [Source: Body; line: 1, column: 8]"), testFilter.report.getResponseViolations()); }
From source file:org.fedoraproject.jenkins.plugins.copr.CoprClient.java
String doGet(String username, String url) throws CoprException { CloseableHttpClient httpclient = HttpClients.createDefault(); HttpGet httpget = new HttpGet(this.apiurl + url); try {//from ww w . j a va 2 s .c o m httpget.setHeader("Authorization", "Basic " + Base64.encodeBase64String(String.format("%s:%s", apilogin, apitoken).getBytes("UTF-8"))); } catch (UnsupportedEncodingException e) { // here goes trouble throw new AssertionError(e); } String result; try { CloseableHttpResponse response = httpclient.execute(httpget); result = EntityUtils.toString(response.getEntity()); response.close(); httpclient.close(); } catch (IOException e) { throw new CoprException("Error while processing HTTP request", e); } return result; }