List of usage examples for org.apache.http.entity StringEntity setContentEncoding
public void setContentEncoding(Header header)
From source file:retsys.client.http.HttpHelper.java
public HttpPost getHttpPostObj(String operation, String body) throws IOException { HttpPost post = null;/*from ww w. j a v a 2 s. c o m*/ post = new HttpPost(getHttpUrl(hostName, hostPort, context) + "/" + operation); StringEntity se = new StringEntity(body); se.setContentEncoding("UTF-8"); se.setContentType("application/json"); post.setEntity(se); return post; }
From source file:retsys.client.http.HttpHelper.java
public HttpPut getHttpPutObj(String operation, String body) throws IOException { HttpPut put = null;/* w ww . j a v a 2 s. c o m*/ put = new HttpPut(getHttpUrl(hostName, hostPort, context) + "/" + operation); StringEntity se = new StringEntity(body); se.setContentEncoding("UTF-8"); se.setContentType("application/json"); put.setEntity(se); return put; }
From source file:com.joken.notice.message.util.HttpRequestHandler.java
/** * ??post/*from w w w . j a v a 2s. c o m*/ * @Auther Hanzibin * @date 3:18:48 PM,Mar 11, 2016 * @return String */ public String sendPostRequest() { CloseableHttpResponse httpResponse = null; PoolingHttpClientConnectionManager connManager = new PoolingHttpClientConnectionManager(); connManager.setMaxTotal(300); connManager.setDefaultMaxPerRoute(20); if (requestConfig == null) { requestConfig = RequestConfig.custom().setConnectionRequestTimeout(10000) .setStaleConnectionCheckEnabled(true).setConnectTimeout(10000).setSocketTimeout(10000).build(); } CloseableHttpClient httpClient = HttpClients.custom().setConnectionManager(connManager) .setDefaultRequestConfig(requestConfig).build(); HttpPost hp = new HttpPost(url); try { StringEntity entity = new StringEntity(postData, Charset.forName("UTF-8"));//? entity.setContentEncoding("UTF-8"); entity.setContentType("application/json"); hp.setEntity(entity); httpResponse = httpClient.execute(hp); return EntityUtils.toString(httpResponse.getEntity()); } catch (Throwable t) { t.printStackTrace(); log.info("??Http" + postData.toString() + ""); } finally { try { hp.releaseConnection(); } catch (Exception e) { } } return null; }
From source file:fi.mikuz.boarder.connection.ConnectionManager.java
public ConnectionManager(ConnectionListener connectionListener, final String url, final HashMap<String, String> sendList) { this.connectionListener = connectionListener; Thread t = new Thread() { public void run() { JSONObject json = new JSONObject(); try { HttpClient httpclient = new DefaultHttpClient(); HttpPost httppost = new HttpPost(url); if (sendList != null) { for (String key : sendList.keySet()) { json.put(key, sendList.get(key)); }/*ww w. j a v a 2 s . c o m*/ } json.put(InternetMenu.HTML_FILTER, false); StringEntity se = new StringEntity(json.toString()); se.setContentEncoding(new BasicHeader(HTTP.CONTENT_TYPE, "application/json")); httppost.setEntity(se); if (GlobalSettings.getSensitiveLogging()) Log.v(TAG, "Sending to " + url + ": " + json.toString()); HttpResponse response = httpclient.execute(httppost); InputStream in = response.getEntity().getContent(); //Get the data in the entity String result = convertStreamToString(in); try { if (GlobalSettings.getSensitiveLogging()) Log.v(TAG, "Got from " + url + ": " + result); else Log.v(TAG, "Got answer from " + url); connectionSuccessfulResponse = new ConnectionSuccessfulResponse(new JSONObject(result), ConnectionUtils.getUrlConnectionId(url)); mHandler.post(connectionSuccessful); } catch (JSONException e) { Log.e(TAG, "Couldn't convert to JSON object", e); connectionErrorResponse = new ConnectionErrorResponse(Html.fromHtml(result).toString(), url); mHandler.post(connectionError); } } catch (Exception e) { String error = "Cannot establish connection"; Log.e(TAG, error); connectionErrorResponse = new ConnectionErrorResponse(error, ConnectionUtils.getUrlConnectionId(url)); mHandler.post(connectionError); } } }; t.start(); }
From source file:csic.ceab.movelab.beepath.Fix.java
public boolean uploadJSON(Context context) { String response = ""; String uploadurl = Util.URL_FIXES_JSON; try {/* ww w .j ava 2 s .c o m*/ // Create a new HttpClient and Post Header HttpPost httppost = new HttpPost(uploadurl); HttpParams myParams = new BasicHttpParams(); HttpConnectionParams.setConnectionTimeout(myParams, 10000); HttpConnectionParams.setSoTimeout(myParams, 60000); HttpConnectionParams.setTcpNoDelay(myParams, true); httppost.setHeader("Content-type", "application/json"); HttpClient httpclient = new DefaultHttpClient(); StringEntity se = new StringEntity(exportJSON(context).toString(), HTTP.UTF_8); se.setContentEncoding(new BasicHeader(HTTP.CONTENT_TYPE, "application/json")); httppost.setEntity(se); // Execute HTTP Post Request ResponseHandler<String> responseHandler = new BasicResponseHandler(); response = httpclient.execute(httppost, responseHandler); } catch (ClientProtocolException e) { } catch (IOException e) { } if (response.contains("SUCCESS")) { return true; } else { return false; } }
From source file:eu.musesproject.client.connectionmanager.HttpConnectionsHelper.java
/** * Http post implementation /*from w ww. j a v a2 s . c o m*/ * @param url * @param data * @return httpResponse * @throws ClientProtocolException * @throws IOException */ public synchronized HttpResponse doPost(String type, String url, String data) throws ClientProtocolException, IOException { HttpResponse httpResponse = null; HttpPost httpPost = new HttpPost(url); HttpParams httpParameters = new BasicHttpParams(); HttpConnectionParams.setConnectionTimeout(httpParameters, TIMEOUT); DefaultHttpClient httpclient = new DefaultHttpClient(httpParameters); StringEntity s = new StringEntity(data.toString()); s.setContentEncoding("UTF-8"); s.setContentType("application/xml"); httpPost.addHeader("connection-type", type); httpPost.setEntity(s); httpPost.addHeader("accept", "application/xml"); if (cookie == null || cookie.isExpired(new Date())) { try { httpResponse = httpclient.execute(httpPost); List<Cookie> cookies = httpclient.getCookieStore().getCookies(); if (cookies.isEmpty()) { Log.d(TAG, "None"); } else { cookie = cookies.get(0); cookieExpiryDate = cookie.getExpiryDate(); Log.d(TAG, "Curent cookie expiry : " + cookieExpiryDate); } } catch (ClientProtocolException e) { Log.d(TAG, e.getMessage()); } catch (Exception e) { Log.d(TAG, e.getMessage()); } } else { httpPost.addHeader("accept", "application/xml"); httpclient.getCookieStore().addCookie(cookie); try { httpResponse = httpclient.execute(httpPost); } catch (ClientProtocolException e) { Log.d(TAG, e.getMessage()); } catch (Exception e) { Log.d(TAG, e.getMessage()); } } return httpResponse; }
From source file:eu.musesproject.client.connectionmanager.HttpConnectionsHelper.java
/** * POST (HTTPS)/*from w ww .ja v a2 s . c o m*/ * @param url * @param data * @return * @throws ClientProtocolException * @throws IOException */ public HttpResponse doSecurePost(Request request) throws ClientProtocolException, IOException { HttpResponse httpResponse = null; HttpPost httpPost = null; TLSManager tlsManager = new TLSManager(); DefaultHttpClient httpclient = tlsManager.getTLSHttpClient(); if (httpclient != null) { HttpParams httpParameters = new BasicHttpParams(); HttpConnectionParams.setConnectionTimeout(httpParameters, TIMEOUT); httpPost = new HttpPost(request.getUrl()); StringEntity s = new StringEntity(request.getData().toString()); s.setContentEncoding("UTF-8"); s.setContentType("application/xml"); httpPost.setEntity(s); httpPost.addHeader("accept", "application/xml"); httpPost.addHeader("connection-type", request.getType()); httpPost.setHeader("poll-interval", getInStringSeconds(request.getPollInterval())); } if (cookie != null && !cookie .isExpired(new Date()) /*!isSessionExpired(new Date(), AlarmReceiver.LAST_SENT_POLL_INTERVAL)*/) { httpclient.getCookieStore().addCookie(cookie); try { httpResponse = httpclient.execute(httpPost); } catch (ClientProtocolException e) { Log.d(TAG, e.getMessage()); } catch (IOException e) { Log.d(TAG, e.getMessage()); } } else { try { httpResponse = httpclient.execute(httpPost); List<Cookie> cookies = httpclient.getCookieStore().getCookies(); if (!cookies.isEmpty()) { cookie = cookies.get(0); } } catch (ClientProtocolException e) { Log.d(TAG, e.getMessage()); } catch (IOException e) { Log.d(TAG, e.getMessage()); } } AlarmReceiver.LAST_SENT_POLL_INTERVAL = Integer.parseInt(request.getPollInterval()); return httpResponse; }
From source file:de.lgblaumeiser.ptm.cli.rest.RestUtils.java
/** * Post a call to the rest api. Expects that a numerical id is returned as * part of the creation call./*from ww w. j av a 2 s . c o m*/ * * @param apiName * Name of the api * @param bodyData * Body of the post data, this is a flat map that is converted * into a flat json * @return The Id of the created or manipulated object */ public Long post(String apiName, Map<String, String> bodyData) { try { final HttpPost request = new HttpPost(baseUrl + apiName); StringEntity bodyJson = new StringEntity(jsonMapper.writeValueAsString(bodyData)); bodyJson.setContentType("application/json"); bodyJson.setContentEncoding("UTF-8"); request.setEntity(bodyJson); HttpResponse response = clientConnector.execute(request); checkState( response.getStatusLine().getStatusCode() == 201 || response.getStatusLine().getStatusCode() == 200, "Cannot access server properly, Status " + response.getStatusLine() + ", URI: " + apiName); String uri = apiName; if (response.getStatusLine().getStatusCode() == 201) { uri = response.getHeaders("Location")[0].getValue(); } return Long.parseLong(uri.substring(uri.lastIndexOf("/") + 1)); } catch (IOException e) { throw new IllegalStateException(e); } }
From source file:com.nextgis.uikobserver.HttpSendData.java
@Override protected Void doInBackground(String... urls) { if (IsNetworkAvailible(mContext)) { String sPostBody = urls[0]; // Create a new HttpClient and Post Header HttpClient httpclient = new DefaultHttpClient(); HttpPost httppost = new HttpPost("http://gis-lab.info:8090/"); HttpParams params = httppost.getParams(); params.setParameter(ClientPNames.HANDLE_REDIRECTS, Boolean.FALSE); HttpContext localContext = new BasicHttpContext(); try {/*from w ww. j av a 2s .co m*/ StringEntity se = new StringEntity(sPostBody, "UTF8"); se.setContentEncoding(new BasicHeader(HTTP.CONTENT_TYPE, "application/json")); httppost.setEntity(se); httppost.setHeader("Content-type", "application/json"); // Execute HTTP Post Request HttpResponse response = httpclient.execute(httppost, localContext); Bundle bundle = new Bundle(); if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) { bundle.putBoolean("error", false); } else { bundle.putBoolean("error", true); } bundle.putInt("src", mnType); Message msg = new Message(); msg.setData(bundle); if (mEventReceiver != null) { mEventReceiver.sendMessage(msg); } } catch (ClientProtocolException e) { mError = e.getMessage(); cancel(true); } catch (IOException e) { mError = e.getMessage(); cancel(true); } } else { Bundle bundle = new Bundle(); bundle.putBoolean("error", true); bundle.putString("err_msq", mContext.getString(R.string.sNetworkUnreach)); bundle.putInt("src", mnType); Message msg = new Message(); msg.setData(bundle); if (mEventReceiver != null) { mEventReceiver.sendMessage(msg); } } return null; }
From source file:org.nebula.framework.client.NebulaRestClient.java
public <T> T post(Request request) throws Exception { request.setAccessId(accessId);//from www . j a va 2 s . c o m Class requestClass = request.getClass(); String uri = requestMapper.getUri(requestClass); Class responseClass = requestMapper.getResponse(requestClass); HttpPost httpPost = new HttpPost(uri); httpPost.setHeader(Request.AUTHORIZATION_HEADER, request.toSignedSignature(secretKey)); httpPost.setConfig(requestConfig); String requestBodyJson = toJson(request); StringEntity s = new StringEntity(requestBodyJson); s.setContentEncoding("UTF-8"); s.setContentType("application/json"); httpPost.setEntity(s); logger.debug("post request" + requestClass.getSimpleName() + ", to " + uri + " with : " + requestBodyJson); return execute(httpPost, responseClass); }