List of usage examples for org.apache.http.entity StringEntity getContentLength
public long getContentLength()
From source file:com.kingmed.dp.aperio.DsClient.java
public static String logon() throws Exception { String token = null;//from w w w . j ava 2 s. co m String url = "http://192.168.180.132:86/Aperio.Security/Security2.asmx"; String body = "<SOAP-ENV:Envelope xmlns:SOAP-ENV=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns=\"http://www.aperio.com/webservices/\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:SOAP-ENC=\"http://schemas.xmlsoap.org/soap/encoding/\" SOAP-ENV:encodingStyle=\"http://schemas.xmlsoap.org/soap/encoding/\"> <SOAP-ENV:Body> <Logon><Token>leQJYfWQ6wv_tJa6hhBZlWwgrRZ-mDywnfb9F4EfC1752Pt07NZDEGvFNYYPvpxkN0IvPTrPi0M=</Token><LoginName>gzuser</LoginName><Password>gzking</Password></Logon></SOAP-ENV:Body> </SOAP-ENV:Envelope> "; HttpClient hc = HttpClients.createDefault(); HttpPost httpPost = new HttpPost(url); httpPost.addHeader("Content-Type", "text/xml;charset=utf-8"); httpPost.addHeader("SOAPAction", "http://www.aperio.com/webservices/#Logon"); StringEntity myEntity = new StringEntity(body, ContentType.create("text/xml", "UTF-8")); httpPost.setEntity(myEntity); System.out.println(myEntity.getContentType()); System.out.println("Content-Length" + myEntity.getContentLength()); HttpResponse res = null; res = (CloseableHttpResponse) hc.execute(httpPost); HttpEntity entity = res.getEntity(); System.out.println(EntityUtils.toString(entity)); return token; }
From source file:org.pentaho.di.engine.configuration.impl.pentaho.scheduler.SchedulerRequestTest.java
private boolean compareContentOfStringEntities(StringEntity entity1, StringEntity entity2) { if (entity1.getContentLength() == entity2.getContentLength()) { try (InputStream stream1 = entity1.getContent(); InputStream stream2 = entity2.getContent()) { while (stream1.available() > 0) { if (stream1.read() != stream2.read()) { return false; }/*from w w w. j a va 2 s. c o m*/ } return true; } catch (IOException e) { return false; } } else { return false; } }
From source file:com.parse.ParseApacheHttpClientTest.java
@Test public void testGetParseResponse() throws IOException { int statusCode = 200; String reasonPhrase = "test reason"; ProtocolVersion protocol = new ProtocolVersion("HTTP", 1, 1); BasicStatusLine line = new BasicStatusLine(protocol, statusCode, reasonPhrase); BasicHttpResponse apacheResponse = new BasicHttpResponse(line); String content = "content"; StringEntity entity = new StringEntity(content); apacheResponse.setEntity(entity);/*from w ww . j a v a 2s. co m*/ apacheResponse.setHeader("Content-Length", String.valueOf(entity.getContentLength())); ParseApacheHttpClient parseClient = new ParseApacheHttpClient(10000, null); ParseHttpResponse parseResponse = parseClient.getResponse(apacheResponse); // Verify status code assertEquals(statusCode, parseResponse.getStatusCode()); // Verify reason phrase assertEquals(reasonPhrase, parseResponse.getReasonPhrase()); // Verify content length assertEquals(7, parseResponse.getTotalSize()); // Verify content // Can not read apacheResponse entity to compare since it has been read during // creating parseResponse assertArrayEquals(content.getBytes(), ParseIOUtils.toByteArray(parseResponse.getContent())); }
From source file:impalapayapis.ApiRequestBank.java
public String doPost() { CloseableHttpClient httpclient = HttpClients.createDefault(); StringEntity entity; String out = ""; try {// w ww .j a v a 2s . c om entity = new StringEntity(params); HttpPost httppost = new HttpPost(url); httppost.setEntity(entity); HttpResponse response = httpclient.execute(httppost); // for debugging System.out.println(entity.getContentType()); System.out.println(entity.getContentLength()); System.out.println(EntityUtils.toString(entity)); System.out.println(EntityUtils.toByteArray(entity).length); //System.out.println( "----------------------------------------"); System.out.println(response.getStatusLine()); System.out.println(url); BufferedReader rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent())); out = rd.readLine(); JsonElement root = new JsonParser().parse(out); String specificvalue = root.getAsJsonObject().get("replace with key of the value to retrieve") .getAsString(); System.out.println(specificvalue); /** * String line = ""; while ((line = rd.readLine()) != null) { * //System.out.println(line); } * */ } catch (UnsupportedEncodingException e) { logger.error("UnsupportedEncodingException for URL: '" + url + "'"); logger.error(ExceptionUtils.getStackTrace(e)); } catch (ClientProtocolException e) { logger.error("ClientProtocolException for URL: '" + url + "'"); logger.error(ExceptionUtils.getStackTrace(e)); } catch (IOException e) { logger.error("IOException for URL: '" + url + "'"); logger.error(ExceptionUtils.getStackTrace(e)); } return out; }
From source file:com.telefonica.iot.cygnus.backends.http.HttpBackend.java
/** * Does a Http request given a method, a relative URL (the final URL will be * composed by using this relative URL and the active Http endpoint), a list * of headers and the payload.//from w w w .ja va 2s. c om * * @param method * @param url * @param relative * @param headers * @param entity * @return A Http httpRes * @throws CygnusRuntimeError * @throws CygnusPersistenceError */ public JsonResponse doRequest(String method, String url, boolean relative, ArrayList<Header> headers, StringEntity entity) throws CygnusRuntimeError, CygnusPersistenceError { if (entity != null) { transactionRequestBytes += entity.getContentLength(); } // if JsonResponse response; if (relative) { // create the HttpFS URL String effectiveURL = (ssl ? "https://" : "http://") + host + ":" + port + url; if (krb5) { response = doPrivilegedRequest(method, effectiveURL, headers, entity); } else { response = doRequest(method, effectiveURL, headers, entity); } // if else return response; } else { if (krb5) { return doPrivilegedRequest(method, url, headers, entity); } else { return doRequest(method, url, headers, entity); } // if else } // if else }
From source file:org.ellis.yun.search.test.httpclient.HttpClientTest.java
@Test @SuppressWarnings("deprecation") public void testStringEntity() throws Exception { StringEntity myEntity = new StringEntity("important message", "UTF-8"); System.out.println(myEntity.getContentType() + ""); System.out.println(myEntity.getContentLength() + ""); System.out.println(EntityUtils.getContentCharSet(myEntity)); System.out.println(EntityUtils.toString(myEntity)); System.out.println(EntityUtils.toByteArray(myEntity).length + ""); }
From source file:org.alfresco.json.JSONUtilTest.java
@Test public void testSetMethodBody() throws Exception { JSONObject json = new JSONObject(); StringEntity se = JSONUtil.setMessageBody(json); // Check that UTF-8 is the encoding used assertEquals(new StringEntity("{}", "UTF-8").getContentLength(), se.getContentLength()); assertNotSame(new StringEntity("{}", "UTF-16").getContentLength(), se.getContentLength()); }