List of usage examples for org.apache.http.entity StringEntity setContentEncoding
public void setContentEncoding(Header header)
From source file:utils.UtilsFunction.java
private static String postJsonToServer(String url, String jsonData) throws UnsupportedEncodingException, IOException { HttpPost postRequest = new HttpPost(url); postRequest.setHeader("Content-Type", "application/json; charset=UTF-8"); StringEntity postData = new StringEntity(jsonData, Charset.forName("UTF-8")); //Header charsetHeader= new BasicHeader("charset", "UTF-8"); postData.setContentEncoding("UTF-8"); //Header h=postData.getContentEncoding(); postRequest.setEntity(postData);//from www .ja v a 2 s .c o m //System.out.println(h.getValue()); HttpResponse res = client.execute(postRequest); String dataResponse = getStringFromResponse(res); return dataResponse; }
From source file:com.spoiledmilk.ibikecph.util.HttpUtils.java
public static JsonNode deleteFromServer(String urlString, JSONObject objectToPost) { JsonNode ret = null;//from ww w . j a v a2 s . c om LOG.d("DELETE api request, url = " + urlString); HttpParams myParams = new BasicHttpParams(); HttpConnectionParams.setConnectionTimeout(myParams, CONNECTON_TIMEOUT); HttpConnectionParams.setSoTimeout(myParams, CONNECTON_TIMEOUT); HttpClient httpclient = new DefaultHttpClient(myParams); httpclient.getParams().setParameter(CoreProtocolPNames.USER_AGENT, Config.USER_AGENT); HTTPDeleteWithBody httpdelete = null; URL url = null; try { url = new URL(urlString); httpdelete = new HTTPDeleteWithBody(url.toString()); httpdelete.setHeader("Content-type", "application/json"); httpdelete.setHeader("Accept", ACCEPT); httpdelete.setHeader("LANGUAGE_CODE", IbikeApplication.getLanguageString()); StringEntity se = new StringEntity(objectToPost.toString(), HTTP.UTF_8); se.setContentEncoding(new BasicHeader(HTTP.CONTENT_TYPE, "application/json")); httpdelete.setEntity(se); HttpResponse response = httpclient.execute(httpdelete); String serverResponse = EntityUtils.toString(response.getEntity()); LOG.d("API response = " + serverResponse); ret = Util.stringToJsonNode(serverResponse); } catch (Exception e) { if (e != null && e.getLocalizedMessage() != null) LOG.e(e.getLocalizedMessage()); } return ret; }
From source file:com.spoiledmilk.ibikecph.util.HttpUtils.java
public static JsonNode putToServer(String urlString, JSONObject objectToPost) { JsonNode ret = null;/* w w w . ja va 2s . c om*/ LOG.d("PUT api request, url = " + urlString); HttpParams myParams = new BasicHttpParams(); HttpConnectionParams.setConnectionTimeout(myParams, CONNECTON_TIMEOUT); HttpConnectionParams.setSoTimeout(myParams, CONNECTON_TIMEOUT); HttpClient httpclient = new DefaultHttpClient(myParams); httpclient.getParams().setParameter(CoreProtocolPNames.USER_AGENT, Config.USER_AGENT); HttpPut httput = null; URL url = null; try { url = new URL(urlString); httput = new HttpPut(url.toString()); httput.setHeader("Content-type", "application/json"); httput.setHeader("Accept", ACCEPT); httput.setHeader("LANGUAGE_CODE", IbikeApplication.getLanguageString()); StringEntity se = new StringEntity(objectToPost.toString(), HTTP.UTF_8); se.setContentEncoding(new BasicHeader(HTTP.CONTENT_TYPE, "application/json")); httput.setEntity(se); HttpResponse response = httpclient.execute(httput); String serverResponse = EntityUtils.toString(response.getEntity()); LOG.d("API response = " + serverResponse); ret = Util.stringToJsonNode(serverResponse); } catch (Exception e) { if (e != null && e.getLocalizedMessage() != null) { LOG.e(e.getLocalizedMessage()); } } return ret; }
From source file:com.spoiledmilk.ibikecph.util.HttpUtils.java
public static JsonNode postToServer(String urlString, JSONObject objectToPost) { JsonNode ret = null;/*from www . j a v a 2 s .com*/ LOG.d("POST api request, url = " + urlString + " object = " + objectToPost.toString()); HttpParams myParams = new BasicHttpParams(); HttpConnectionParams.setConnectionTimeout(myParams, CONNECTON_TIMEOUT); HttpConnectionParams.setSoTimeout(myParams, CONNECTON_TIMEOUT); HttpClient httpclient = new DefaultHttpClient(myParams); httpclient.getParams().setParameter(CoreProtocolPNames.USER_AGENT, Config.USER_AGENT); HttpPost httppost = null; URL url = null; try { url = new URL(urlString); httppost = new HttpPost(url.toString()); httppost.setHeader("Content-type", "application/json"); httppost.setHeader("Accept", ACCEPT); httppost.setHeader("LANGUAGE_CODE", IbikeApplication.getLanguageString()); StringEntity se = new StringEntity(objectToPost.toString(), HTTP.UTF_8);// , HTTP.UTF_8 se.setContentEncoding(new BasicHeader(HTTP.CONTENT_TYPE, "application/json")); httppost.setEntity(se); HttpResponse response = httpclient.execute(httppost); String serverResponse = EntityUtils.toString(response.getEntity()); LOG.d("API response = " + serverResponse); ret = Util.stringToJsonNode(serverResponse); } catch (Exception e) { if (e != null && e.getLocalizedMessage() != null) { LOG.e(e.getLocalizedMessage()); } } return ret; }
From source file:dk.kk.ibikecphlib.util.HttpUtils.java
public static JsonNode deleteFromServer(String urlString, JSONObject objectToPost) { JsonNode ret = null;/*from w ww.j a v a 2 s . c om*/ LOG.d("DELETE api request, url = " + urlString); HttpParams myParams = new BasicHttpParams(); HttpConnectionParams.setConnectionTimeout(myParams, CONNECTON_TIMEOUT); HttpConnectionParams.setSoTimeout(myParams, CONNECTON_TIMEOUT); HttpClient httpclient = new DefaultHttpClient(myParams); httpclient.getParams().setParameter(CoreProtocolPNames.USER_AGENT, Config.USER_AGENT); HTTPDeleteWithBody httpdelete = null; URL url = null; try { url = new URL(urlString); httpdelete = new HTTPDeleteWithBody(url.toString()); httpdelete.setHeader("Content-type", "application/json"); httpdelete.setHeader("Accept", ACCEPT); httpdelete.setHeader("LANGUAGE_CODE", IBikeApplication.getLanguageString()); StringEntity se = new StringEntity(objectToPost.toString(), HTTP.UTF_8); se.setContentEncoding(new BasicHeader(HTTP.CONTENT_TYPE, "application/json")); httpdelete.setEntity(se); HttpResponse response = httpclient.execute(httpdelete); String serverResponse = EntityUtils.toString(response.getEntity()); LOG.d("API response = " + serverResponse); ret = Util.stringToJsonNode(serverResponse); } catch (Exception e) { if (e != null && e.getLocalizedMessage() != null) LOG.e(e.getLocalizedMessage()); } return ret; }
From source file:dk.kk.ibikecphlib.util.HttpUtils.java
public static JsonNode putToServer(String urlString, JSONObject objectToPost) { JsonNode ret = null;//from w w w . ja va 2 s.c o m LOG.d("PUT api request, url = " + urlString); HttpParams myParams = new BasicHttpParams(); HttpConnectionParams.setConnectionTimeout(myParams, CONNECTON_TIMEOUT); HttpConnectionParams.setSoTimeout(myParams, CONNECTON_TIMEOUT); HttpClient httpclient = new DefaultHttpClient(myParams); httpclient.getParams().setParameter(CoreProtocolPNames.USER_AGENT, Config.USER_AGENT); HttpPut httput = null; URL url = null; try { url = new URL(urlString); httput = new HttpPut(url.toString()); httput.setHeader("Content-type", "application/json"); httput.setHeader("Accept", ACCEPT); httput.setHeader("LANGUAGE_CODE", IBikeApplication.getLanguageString()); StringEntity se = new StringEntity(objectToPost.toString(), HTTP.UTF_8); se.setContentEncoding(new BasicHeader(HTTP.CONTENT_TYPE, "application/json")); httput.setEntity(se); HttpResponse response = httpclient.execute(httput); String serverResponse = EntityUtils.toString(response.getEntity()); LOG.d("API response = " + serverResponse); ret = Util.stringToJsonNode(serverResponse); } catch (Exception e) { if (e != null && e.getLocalizedMessage() != null) { LOG.e(e.getLocalizedMessage()); } } return ret; }
From source file:dk.kk.ibikecphlib.util.HttpUtils.java
public static JsonNode postToServer(String urlString, JSONObject objectToPost) { JsonNode ret = null;//from ww w .ja va 2s . co m LOG.d("POST api request, url = " + urlString + " object = " + objectToPost.toString()); HttpParams myParams = new BasicHttpParams(); HttpConnectionParams.setConnectionTimeout(myParams, CONNECTON_TIMEOUT); HttpConnectionParams.setSoTimeout(myParams, CONNECTON_TIMEOUT); HttpClient httpclient = new DefaultHttpClient(myParams); httpclient.getParams().setParameter(CoreProtocolPNames.USER_AGENT, Config.USER_AGENT); HttpPost httppost = null; URL url = null; try { url = new URL(urlString); httppost = new HttpPost(url.toString()); httppost.setHeader("Content-type", "application/json"); httppost.setHeader("Accept", ACCEPT); httppost.setHeader("LANGUAGE_CODE", IBikeApplication.getLanguageString()); StringEntity se = new StringEntity(objectToPost.toString(), HTTP.UTF_8);// , HTTP.UTF_8 se.setContentEncoding(new BasicHeader(HTTP.CONTENT_TYPE, "application/json")); httppost.setEntity(se); HttpResponse response = httpclient.execute(httppost); String serverResponse = EntityUtils.toString(response.getEntity()); LOG.d("API response = " + serverResponse); ret = Util.stringToJsonNode(serverResponse); } catch (Exception e) { if (e != null && e.getLocalizedMessage() != null) { LOG.e(e.getLocalizedMessage()); } } return ret; }
From source file:no.ntnu.wifimanager.ServerUtilities.java
public static void postJSON(String url, String JSONString) { HttpPost httpPost = new HttpPost(url); StringEntity se; try {/* w w w . java 2 s. co m*/ se = new StringEntity(JSONString); se.setContentType("application/json"); se.setContentEncoding(new BasicHeader(HTTP.CONTENT_TYPE, "application/json")); //httpPost.setHeader("Accept", "application/json"); httpPost.setEntity(se); HttpResponse resp = new DefaultHttpClient().execute(httpPost); } catch (UnsupportedEncodingException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (ClientProtocolException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } }
From source file:com.mywork.framework.util.RemoteHttpUtil.java
/** * ?? Post/*www . ja v a2 s. com*/ * @return */ public static String doPost(String contentUrl, Map<String, String> headerMap, String jsonBody) { String result = null; CloseableHttpResponse response = null; CloseableHttpClient httpClient = HttpClients.createDefault(); HttpPost post = new HttpPost(contentUrl); RequestConfig config = RequestConfig.custom().setConnectionRequestTimeout(TIMEOUT_SECONDS * 1000) .setConnectTimeout(TIMEOUT_SECONDS * 1000).setSocketTimeout(TIMEOUT_SECONDS * 1000).build(); post.setConfig(config); try { if (headerMap != null && !headerMap.isEmpty()) { for (Map.Entry<String, String> m : headerMap.entrySet()) { post.setHeader(m.getKey(), m.getValue()); } } if (jsonBody != null) { StringEntity entity = new StringEntity(jsonBody, "utf-8"); entity.setContentEncoding("UTF-8"); entity.setContentType("application/json"); post.setEntity(entity); } long start = System.currentTimeMillis(); response = httpClient.execute(post); HttpEntity entity = response.getEntity(); result = EntityUtils.toString(entity); logger.info("url = " + contentUrl + " request spend time = " + (System.currentTimeMillis() - start)); EntityUtils.consume(entity); } catch (ClientProtocolException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally { try { response.close(); } catch (IOException e) { e.printStackTrace(); } } return result; }
From source file:com.lostad.app.base.util.RequestUtil.java
public static String postJson(String url, String token, String dataJson) throws Exception { String json = null;/*from w ww. j a va2 s . c o m*/ HttpEntity resEntity = null; HttpClient client = HttpClientManager.getHttpClient(true); HttpPost post = new HttpPost(url); if (Validator.isNotEmpty(token)) { post.addHeader("token", token); } try { post.addHeader("Content-Type", "application/json;charset=UTF-8"); StringEntity se = new StringEntity(dataJson, "UTF-8"); se.setContentEncoding(new BasicHeader(HTTP.CONTENT_TYPE, "application/json")); post.setEntity(se); // HttpResponse response = client.execute(post, new BasicHttpContext()); resEntity = response.getEntity(); if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {// 200 // ? json = EntityUtils.toString(resEntity); } } catch (Exception e) { if (post != null) { post.abort();// } throw e; } finally { if (resEntity != null) { try { resEntity.consumeContent();//? } catch (IOException e) { e.printStackTrace(); } } client.getConnectionManager().closeExpiredConnections(); ///client.getConnectionManager().shutdown(); } return json; }