List of usage examples for org.apache.http.util EntityUtils toString
public static String toString(HttpEntity httpEntity) throws IOException, ParseException
From source file:com.ny.apps.executor.httpclient.HttpRequestSender.java
public static void main(String[] args) throws ClientProtocolException, IOException { /*/*from w ww . j a v a 2s.co m*/ CredentialsProvider credsProvider = new BasicCredentialsProvider(); credsProvider.setCredentials(new AuthScope("localhost", 80), new UsernamePasswordCredentials("root", "123")); CloseableHttpClient client = HttpClients.custom().setDefaultCredentialsProvider(credsProvider).build(); */ CloseableHttpClient client = HttpClients.createDefault(); HttpGet httpGet = new HttpGet("http://www.baidu.com/"); httpGet.setHeader("Authorization", ""); logger.info("executing request " + httpGet.getURI()); ResponseHandler<String> responseHandler = new ResponseHandler<String>() { @Override public String handleResponse(HttpResponse response) throws ClientProtocolException, IOException { // TODO ? int statusCode = response.getStatusLine().getStatusCode(); if (statusCode >= 200 && statusCode < 300) { HttpEntity entity = response.getEntity(); return entity == null ? null : EntityUtils.toString(entity); } else { throw new ClientProtocolException("Unexpected response status: " + statusCode); } } }; try { String responseBody = client.execute(httpGet, responseHandler); System.out.println("----------------------------------------"); System.out.println(responseBody); System.out.println("----------------------------------------"); } finally { client.close(); } }
From source file:com.javaquery.apache.httpclient.HttpPostExample.java
public static void main(String[] args) throws UnsupportedEncodingException { /* Create object of CloseableHttpClient */ CloseableHttpClient httpClient = HttpClients.createDefault(); /* Prepare POST request */ HttpPost httpPost = new HttpPost("http://www.example.com/api/customer"); /* Add headers to POST request */ httpPost.addHeader("Authorization", "value"); httpPost.addHeader("Content-Type", "application/json"); /* Prepare StringEntity from JSON */ StringEntity jsonData = new StringEntity("{\"id\":\"123\", \"name\":\"Vicky Thakor\"}", "UTF-8"); /* Body of request */ httpPost.setEntity(jsonData);/*from ww w . ja va 2s .c om*/ /* Response handler for after request execution */ ResponseHandler<String> responseHandler = new ResponseHandler<String>() { @Override public String handleResponse(HttpResponse httpResponse) throws ClientProtocolException, IOException { /* Get status code */ int httpResponseCode = httpResponse.getStatusLine().getStatusCode(); System.out.println("Response code: " + httpResponseCode); if (httpResponseCode >= 200 && httpResponseCode < 300) { /* Convert response to String */ HttpEntity entity = httpResponse.getEntity(); return entity != null ? EntityUtils.toString(entity) : null; } else { return null; /* throw new ClientProtocolException("Unexpected response status: " + httpResponseCode); */ } } }; try { /* Execute URL and attach after execution response handler */ String strResponse = httpClient.execute(httpPost, responseHandler); /* Print the response */ System.out.println("Response: " + strResponse); } catch (IOException ex) { ex.printStackTrace(); } }
From source file:com.mycompany.horus.Teste.java
public static void main(String[] args) throws Exception { ServiceListener list = new ServiceListener(); list.getHorusServicesList();/*from www.j a va 2 s. c o m*/ String soapBody = "<soap:Envelope xmlns:soap=\"http://www.w3.org/2003/05/soap-envelope\" xmlns:est=\"http://servicos.saude.gov.br/horus/v1r0/EstoqueService\">\n" + " <soap:Header>\n" + " <wsse:Security xmlns:wsse=\"http://docs.oasis-open.org/wss/2004/01/oasis-200401-wsswssecurity-secext-1.0.xsd\">\n" + " <wsse:UsernameToken wsu:Id=\"Id-0001334008436683-000000002c4a1908-1\" xmlns:wsu=\"http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd\">\n" + " <wsse:Username>HORUS</wsse:Username>\n" + " <wsse:Password Type=\"http://docs.oasis-open.org/wss/2004/01/oasis-200401-wssusername-token-profile-1.0#PasswordText\">SENHA</wsse:Password>\n" + " </wsse:UsernameToken>\n" + " </wsse:Security>\n" + " </soap:Header>\n" + " <soap:Body><est:requestConsultarPosicaoEstoquePorCNES>\n" + " <est:cnes>7604041</est:cnes>\n" + " </est:requestConsultarPosicaoEstoquePorCNES>\n" + " </soap:Body>\n" + "</soap:Envelope>\n"; try { // Get target URL CloseableHttpClient httpclient = HttpClientBuilder.create().build(); StringEntity strEntity = new StringEntity(soapBody, "UTF-8"); strEntity.setContentType("text/xml"); HttpPost post = new HttpPost("https://servicos.saude.gov.br/horus/v1r0/EstoqueService"); post.setEntity(strEntity); // Execute request HttpResponse response = httpclient.execute(post); HttpEntity respEntity = response.getEntity(); String resp = EntityUtils.toString(respEntity); if (respEntity != null) { System.out.println("Response:"); System.out.println(resp); //Changing response to Xml file stringToDom(resp); } else { System.out.println("No Response"); } } catch (Exception e) { System.out.println("Other exception = " + e.toString()); } }
From source file:com.orange.ClientExecuteProxy.java
public static void main(String[] args) throws Exception { CloseableHttpClient httpclient = HttpClients.createDefault(); try {// w w w . java 2 s .c o m HttpHost target = new HttpHost("httpbin.org", 443, "https"); HttpHost proxy = new HttpHost("127.0.0.1", 8080, "http"); RequestConfig config = RequestConfig.custom().setProxy(proxy).build(); HttpGet request = new HttpGet("/"); request.setConfig(config); System.out.println("Executing request " + request.getRequestLine() + " to " + target + " via " + proxy); CloseableHttpResponse response = httpclient.execute(target, request); try { System.out.println("----------------------------------------"); System.out.println(response.getStatusLine()); System.out.println(EntityUtils.toString(response.getEntity())); } finally { response.close(); } } finally { httpclient.close(); } }
From source file:com.dlmu.heipacker.crawler.client.ClientExecuteDirect.java
public static void main(String[] args) throws Exception { DefaultHttpClient httpclient = new DefaultHttpClient(); try {//w w w . j ava2s . co m HttpHost target = new HttpHost("www.apache.org", 80, "http"); HttpGet req = new HttpGet("/"); System.out.println("executing request to " + target); HttpResponse rsp = httpclient.execute(target, req); HttpEntity entity = rsp.getEntity(); System.out.println("----------------------------------------"); System.out.println(rsp.getStatusLine()); Header[] headers = rsp.getAllHeaders(); for (int i = 0; i < headers.length; i++) { System.out.println(headers[i]); } System.out.println("----------------------------------------"); if (entity != null) { System.out.println(EntityUtils.toString(entity)); } } finally { // When HttpClient instance is no longer needed, // shut down the connection manager to ensure // immediate deallocation of all system resources httpclient.getConnectionManager().shutdown(); } }
From source file:com.mtea.macrotea_httpclient_study.ClientExecuteDirect.java
public static void main(String[] args) throws Exception { DefaultHttpClient httpclient = new DefaultHttpClient(); try {/*ww w . j av a 2 s. co m*/ //HttpHost HttpHost target = new HttpHost("www.baidu.com", 80, "http"); HttpGet req = new HttpGet("/"); System.out.println("executing request to " + target); HttpResponse rsp = httpclient.execute(target, req); HttpEntity entity = rsp.getEntity(); System.out.println("----------------------------------------"); System.out.println(rsp.getStatusLine()); //? Header[] headers = rsp.getAllHeaders(); for (int i = 0; i < headers.length; i++) { System.out.println(headers[i]); } System.out.println("----------------------------------------"); //?? if (entity != null) { System.out.println(EntityUtils.toString(entity)); } } finally { //??httpclient??? httpclient.getConnectionManager().shutdown(); } }
From source file:de.zazaz.iot.bosch.indego.App.java
public static void main(String[] args) throws ClientProtocolException, IOException, InterruptedException { CloseableHttpClient httpClient = HttpClients.createDefault(); HttpPost httpPost = new HttpPost(BASE_URL_PUSHWOOSH + "registerDevice"); String jsonPost = ""// + "{" // + " \"request\":{" // + " \"application\":\"8FF60-0666B\"," // + " \"push_token\":\"124692134091\"," // + " \"hwid\":\"00-0C-29-E8-B1-8D\"," // + " \"timezone\":3600," // + " \"device_type\":3" // + " }" // + "}"; httpPost.setEntity(new StringEntity(jsonPost, ContentType.APPLICATION_JSON)); CloseableHttpResponse response = httpClient.execute(httpPost); System.out.println(response.getStatusLine()); Header[] headers = response.getAllHeaders(); for (int i = 0; i < headers.length; i++) { System.out.println(headers[i].getName() + ": " + headers[i].getValue()); }/*from ww w .ja v a 2 s . c o m*/ HttpEntity entity = response.getEntity(); String contents = EntityUtils.toString(entity); System.out.println(contents); Thread.sleep(5000); HttpPost httpGet = new HttpPost(BASE_URL_PUSHWOOSH + "checkMessage"); String jsonGet = ""// + "{" // + " \"request\":{" // + " \"application\":\"8FF60-0666B\"," // + " \"hwid\":\"00-0C-29-E8-B1-8D\"" // + " }" // + "}"; httpGet.setEntity(new StringEntity(jsonGet, ContentType.APPLICATION_JSON)); httpClient.execute(httpGet); response.close(); }
From source file:com.lxf.spider.client.ClientWithResponseHandler.java
public final static void main(String[] args) throws Exception { CloseableHttpClient httpclient = HttpClients.createDefault(); try {//from w w w. ja v a 2 s. c o m HttpGet httpget = new HttpGet("http://localhost/"); System.out.println("Executing request " + httpget.getRequestLine()); // Create a custom response handler ResponseHandler<String> responseHandler = new ResponseHandler<String>() { public String handleResponse(final HttpResponse response) throws ClientProtocolException, IOException { int status = response.getStatusLine().getStatusCode(); if (status >= 200 && status < 300) { HttpEntity entity = response.getEntity(); return entity != null ? EntityUtils.toString(entity) : null; } else { throw new ClientProtocolException("Unexpected response status: " + status); } } }; String responseBody = httpclient.execute(httpget, responseHandler); System.out.println("----------------------------------------"); System.out.println(responseBody); } finally { httpclient.close(); } }
From source file:nl.knaw.dans.cmd2rdf.conversion.util.ClientWithResponseHandler.java
public final static void main(String[] args) throws Exception { CloseableHttpClient httpclient = HttpClients.createDefault(); try {//from w ww. j a v a 2 s. co m HttpGet httpget = new HttpGet("http://localhost/"); //System.out.println("Executing request " + httpget.getRequestLine()); // Create a custom response handler ResponseHandler<String> responseHandler = new ResponseHandler<String>() { public String handleResponse(final HttpResponse response) throws ClientProtocolException, IOException { int status = response.getStatusLine().getStatusCode(); if (status >= 200 && status < 300) { HttpEntity entity = response.getEntity(); return entity != null ? EntityUtils.toString(entity) : null; } else { throw new ClientProtocolException("Unexpected response status: " + status); } } }; String responseBody = httpclient.execute(httpget, responseHandler); //System.out.println("----------------------------------------"); //System.out.println(responseBody); } finally { httpclient.close(); } }
From source file:cn.anthony.util.ClientAuthentication.java
public static void main(String[] args) throws Exception { CredentialsProvider credsProvider = new BasicCredentialsProvider(); credsProvider.setCredentials(new AuthScope("httpbin.org", 80), new UsernamePasswordCredentials("user", "passwd")); CloseableHttpClient httpclient = HttpClients.custom().setDefaultCredentialsProvider(credsProvider).build(); try {//w w w. j ava 2 s. co m HttpGet httpget = new HttpGet("http://httpbin.org/basic-auth/user/passwd"); System.out.println("Executing request " + httpget.getRequestLine()); CloseableHttpResponse response = httpclient.execute(httpget); try { System.out.println("----------------------------------------"); System.out.println(response.getStatusLine()); System.out.println(EntityUtils.toString(response.getEntity())); } finally { response.close(); } } finally { httpclient.close(); } }