List of usage examples for org.apache.http.util EntityUtils toString
public static String toString(HttpEntity httpEntity) throws IOException, ParseException
From source file:cn.comgroup.tzmedia.server.util.mail.SendCloudMail.java
/** * Send email using SMTP server./* w w w. j a v a 2 s.co m*/ * * @param recipientEmail TO recipient * @param title title of the message * @param message message to be sent * connected state or if the message is not a MimeMessage */ public static void send(String recipientEmail, String title, String message) throws UnsupportedEncodingException, IOException { String url = "http://sendcloud.sohu.com/webapi/mail.send.json"; HttpClient httpclient = new DefaultHttpClient(); HttpPost httpost = new HttpPost(url); List nvps = new ArrayList(); // ??SendCloud????????????????? nvps.add(new BasicNameValuePair("api_user", "commobile_test_IxiZE1")); nvps.add(new BasicNameValuePair("api_key", "0tkHZ5vDdScYzRbn")); nvps.add(new BasicNameValuePair("from", "suport@tzzjmedia.net")); nvps.add(new BasicNameValuePair("to", recipientEmail)); nvps.add(new BasicNameValuePair("subject", title)); nvps.add(new BasicNameValuePair("html", message)); httpost.setEntity(new UrlEncodedFormEntity(nvps, "UTF-8")); // HttpResponse response = httpclient.execute(httpost); // ?? if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) { // // ?xml String result = EntityUtils.toString(response.getEntity()); Logger.getLogger(SendCloudMail.class.getName()).log(Level.INFO, result); } else { System.err.println("error"); } }
From source file:app.wz.HttpClient.java
public static String SendHttpPost(String URL, JSONObject jsonObjSend) { try {//from ww w. j a va2s . co m DefaultHttpClient httpclient = new DefaultHttpClient(); HttpPost httpPostRequest = new HttpPost(URL); StringEntity se; se = new StringEntity(jsonObjSend.toString()); // Set HTTP parameters httpPostRequest.setEntity(se); httpPostRequest.setHeader("Accept", "application/json"); httpPostRequest.setHeader("Content-type", "application/json"); httpPostRequest.setHeader("Accept-Encoding", "gzip"); // only set this parameter if you would like to use gzip compression long t = System.currentTimeMillis(); HttpResponse response = (HttpResponse) httpclient.execute(httpPostRequest); Log.i(TAG, "HTTPResponse received in [" + (System.currentTimeMillis() - t) + "ms]"); // Get hold of the response entity (-> the data): HttpEntity entity = response.getEntity(); if (entity != null) { return EntityUtils.toString(entity); // // Read the content stream // InputStream instream = entity.getContent(); // Header contentEncoding = response.getFirstHeader("Content-Encoding"); // if (contentEncoding != null && contentEncoding.getValue().equalsIgnoreCase("gzip")) { // instream = new GZIPInputStream(instream); // } // // // convert content stream to a String // String resultString= convertStreamToString(instream); // instream.close(); // resultString = resultString.substring(1,resultString.length()-1); // remove wrapping "[" and "]" // // // Transform the String into a JSONObject // JSONObject jsonObjRecv = new JSONObject(resultString); // // Raw DEBUG output of our received JSON object: // Log.i(TAG,"<JSONObject>\n"+jsonObjRecv.toString()+"\n</JSONObject>"); // // return jsonObjRecv; } } catch (Exception e) { // More about HTTP exception handling in another tutorial. // For now we just print the stack trace. e.printStackTrace(); } return null; }
From source file:org.smartloli.kafka.eagle.common.util.HttpClientUtils.java
/** * Send request by get method.//from w w w . j a va 2 s. co m * * @param uri: * http://ip:port/demo?httpcode=200&name=smartloli */ public static String doGet(String uri) { String result = ""; try { CloseableHttpClient client = null; CloseableHttpResponse response = null; try { HttpGet httpGet = new HttpGet(uri); client = HttpClients.createDefault(); response = client.execute(httpGet); HttpEntity entity = response.getEntity(); result = EntityUtils.toString(entity); } finally { if (response != null) { response.close(); } if (client != null) { client.close(); } } } catch (Exception e) { e.printStackTrace(); LOG.error("Do get request has error, msg is " + e.getMessage()); } return result; }
From source file:org.apache.marmotta.platform.core.services.http.response.StringBodyResponseHandler.java
@Override public String handleResponse(HttpResponse response) throws ClientProtocolException, IOException { if (response.getStatusLine().getStatusCode() == 200) { final HttpEntity entity = response.getEntity(); return entity != null ? EntityUtils.toString(entity) : null; }/*ww w . j av a 2 s . c o m*/ return null; }
From source file:co.cask.cdap.gateway.handlers.PingHandlerTest.java
@Test public void testStatus() throws Exception { HttpResponse response = GatewayFastTestsSuite.doGet("/status"); Assert.assertEquals(200, response.getStatusLine().getStatusCode()); Assert.assertEquals("OK.\n", EntityUtils.toString(response.getEntity())); }
From source file:com.mycompany.rakornas.getDataURL.java
String getData(String url) throws IOException { try (CloseableHttpClient httpclient = HttpClients.createDefault()) { HttpGet httpget = new HttpGet(url); System.out.println("Executing request " + httpget.getRequestLine()); ResponseHandler<String> responseHandler; responseHandler = new ResponseHandler<String>() { @Override// ww w .java 2s .c o m 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 responseURL = httpclient.execute(httpget, responseHandler); return responseURL; } }
From source file:com.ninehcom.userinfo.agent.SensitiveWordAgent.java
private JSONObject checkWord(String word) throws Exception { String host = sensitiveWordURL; Map<String, String> header = new HashMap<>(); header.put("accept", "*/*"); header.put("connection", "Keep-Alive"); header.put("user-agent", "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US) AppleWebKit/525.13 (KHTML, like Gecko) Chrome/0.2.149.27 Safari/525.13"); header.put("Content-Type", "application/json"); String body = word;/* www . java2 s .c o m*/ HttpResponse response = HttpUtils.doPost(host, "", "", header, null, body); String jsonResultStr = EntityUtils.toString(response.getEntity()); JSONObject jsonResult = new JSONObject(jsonResultStr); return jsonResult; }
From source file:org.winardiaris.uangku.getDataURL.java
String getData(String url) throws IOException { CloseableHttpClient httpclient = HttpClients.createDefault(); try {/* www . j ava 2 s .c o m*/ HttpGet httpget = new HttpGet(url); System.out.println("Executing request " + httpget.getRequestLine()); ResponseHandler<String> responseHandler; responseHandler = new ResponseHandler<String>() { @Override 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 responseURL = httpclient.execute(httpget, responseHandler); return responseURL; } finally { httpclient.close(); } }
From source file:com.fizzed.ninja.rocker.NinjaRockerIntegrationTest.java
@Test public void contentTypeAndCharset() throws Exception { HttpResponse response = this.ninjaTestBrowser.makeRequestAndGetResponse(ninjaTestServer.getBaseUrl() + "/", new HashMap<String, String>()); String body = EntityUtils.toString(response.getEntity()); assertThat(response.getFirstHeader("Content-Type").getValue().toLowerCase(), is("text/html; charset=utf-8")); assertThat(response.getFirstHeader("Content-Length"), is(not(nullValue()))); assertThat(SwissKnife.convert(response.getFirstHeader("Content-Length").getValue(), Integer.class), greaterThan(0));//from w ww.ja v a 2 s .co m }
From source file:com.qihoo.permmgr.util.d.java
public static String a(String paramString, int paramInt) { try {// w w w. ja v a2s. c o m HttpGet localHttpGet = new HttpGet(paramString); DefaultHttpClient localDefaultHttpClient = new DefaultHttpClient(); localDefaultHttpClient.getParams().setParameter("http.connection.timeout", Integer.valueOf(paramInt)); localDefaultHttpClient.getParams().setParameter("http.socket.timeout", Integer.valueOf(paramInt)); HttpResponse localHttpResponse = localDefaultHttpClient.execute(localHttpGet); if (200 == localHttpResponse.getStatusLine().getStatusCode()) { String str = EntityUtils.toString(localHttpResponse.getEntity()); return str; } } catch (Exception localException) { localException.printStackTrace(); } return null; }