List of usage examples for org.apache.http.entity StringEntity writeTo
public void writeTo(OutputStream outputStream) throws IOException
From source file:io.lqd.sdk.model.LQNetworkRequest.java
public LQNetworkResponse sendRequest(String token) { String response = null;//from ww w . j a v a 2s. co m int responseCode = -1; InputStream err = null; OutputStream out = null; BufferedOutputStream bout = null; BufferedReader boin = null; HttpURLConnection connection = null; try { URL url = new URL(this.getUrl()); connection = (HttpURLConnection) url.openConnection(); connection.setRequestMethod(this.getHttpMethod()); connection.setRequestProperty("Authorization", "Token " + token); connection.setRequestProperty("User-Agent", USER_AGENT); connection.setRequestProperty("Accept", "application/vnd.lqd.v1+json"); connection.setRequestProperty("Content-Type", "application/json"); connection.setRequestProperty("Accept-Encoding", "gzip"); connection.setDoInput(true); if (this.getJSON() != null) { connection.setDoOutput(true); out = connection.getOutputStream(); bout = new BufferedOutputStream(out); final StringEntity stringEntity = new StringEntity(this.getJSON(), "UTF-8"); stringEntity.writeTo(bout); } responseCode = connection.getResponseCode(); err = connection.getErrorStream(); GZIPInputStream gzip = new GZIPInputStream(connection.getInputStream()); boin = new BufferedReader(new InputStreamReader(gzip, "UTF-8")); response = boin.readLine(); } catch (IOException e) { LQLog.http("Failed due to " + e + " responseCode " + responseCode); LQLog.http("Error " + inputStreamToString(err)); } finally { if (connection != null) connection.disconnect(); try { if (out != null) out.close(); } catch (IOException e) { } try { if (err != null) err.close(); } catch (IOException e) { } try { if (bout != null) bout.close(); } catch (IOException e) { } try { if (boin != null) boin.close(); } catch (IOException e) { } } if ((response != null) || ((responseCode >= 200) && (responseCode < 300))) { LQLog.http("HTTP Success " + response); return new LQNetworkResponse(responseCode, response); } return new LQNetworkResponse(responseCode); }
From source file:com.logicoy.pdmp.pmpi.http.SimpleWebClient.java
public PMPIHttpClientResponse sendRequest(SessionObject SessionObj, SimpleWebClientSettings settings) { HttpRequestBase httpRequest = null;/*from w ww . j a va2 s . c om*/ //HttpPost httpPost = null; //HttpGet httpGet = null; try { if (settings.getAction() == SimpleWebClientSettings.ActionType.POST) { httpRequest = new HttpPost(settings.getUri()); } else { httpRequest = new HttpGet(settings.getUri()); } httpRequest.setHeader("Content-Type", settings.getContentType()); httpRequest.setHeader("Allow-Auto-Redirect", String.valueOf(settings.getAutoRedirect())); httpRequest.setHeader("Keep-Alive", String.valueOf(settings.getKeepAlive())); if (settings.getAccept() != null && !settings.getAccept().equals("")) { httpRequest.setHeader("Accept", settings.getAccept()); } if (settings.getMessageBody() != null && !settings.getMessageBody().equals("") && settings.getAction() == SimpleWebClientSettings.ActionType.POST) { byte[] postData = settings.getMessageBody().getBytes(Charset.forName("UTF-8")); StringEntity entity = new StringEntity(settings.getMessageBody(), ContentType.create(settings.getContentType(), Charset.forName("UTF-8"))); ByteArrayOutputStream baos = new ByteArrayOutputStream(); entity.writeTo(baos); HttpPost httpPost = (HttpPost) httpRequest; httpPost.setEntity(entity); } try { httpclient.setCookieStore(SessionObj.getCookieStore()); httpResponse = httpclient.execute(httpRequest); } catch (IOException ioe) { logger.log(Level.SEVERE, "Error while posting data", ioe); throw ioe; } PMPIHttpClientResponse resp = new PMPIHttpClientResponse(); populateResponseObject(httpResponse, resp); System.out .println("Status code returned from server : " + httpResponse.getStatusLine().getStatusCode()); System.out.println("Status resson phrase returned from server : " + httpResponse.getStatusLine().getReasonPhrase()); //EntityUtils.consume(httpResponse.getEntity()); return resp; } catch (Exception e) { ok = false; logger.log(Level.SEVERE, e.getMessage(), e); return null; } finally { if (httpRequest != null) { httpRequest.releaseConnection(); } } }
From source file:no.digipost.android.api.ApiAccess.java
public String postput(Context context, final int httpType, final String uri, final StringEntity json) throws DigipostClientException, DigipostApiException, DigipostAuthenticationException { HttpsURLConnection httpsClient; InputStream result = null;//from w w w . j a va2 s .c o m try { URL url = new URL(uri); httpsClient = (HttpsURLConnection) url.openConnection(); if (httpType == POST) { httpsClient.setRequestMethod("POST"); } else if (httpType == PUT) { httpsClient.setRequestMethod("PUT"); } httpsClient.setRequestProperty(ApiConstants.CONTENT_TYPE, ApiConstants.APPLICATION_VND_DIGIPOST_V2_JSON); httpsClient.setRequestProperty(ApiConstants.ACCEPT, ApiConstants.APPLICATION_VND_DIGIPOST_V2_JSON); httpsClient.setRequestProperty(ApiConstants.AUTHORIZATION, ApiConstants.BEARER + TokenStore.getAccess()); OutputStream outputStream; outputStream = new BufferedOutputStream(httpsClient.getOutputStream()); if (json != null) { json.writeTo(outputStream); } outputStream.flush(); int statusCode = httpsClient.getResponseCode(); try { NetworkUtilities.checkHttpStatusCode(context, statusCode); } catch (DigipostInvalidTokenException e) { OAuth.updateAccessTokenWithRefreshToken(context); return postput(context, httpType, uri, json); } if (statusCode == NetworkUtilities.HTTP_STATUS_NO_CONTENT) { return NetworkUtilities.SUCCESS_NO_CONTENT; } try { result = httpsClient.getInputStream(); } catch (IllegalStateException e) { Log.e(TAG, e.getMessage()); } } catch (IOException e) { throw new DigipostClientException(context.getString(R.string.error_your_network)); } return JSONUtilities.getJsonStringFromInputStream(result); }