List of usage examples for org.apache.http.util EntityUtils toByteArray
public static byte[] toByteArray(HttpEntity httpEntity) throws IOException
From source file:net.javacrumbs.restfire.httpcomponents.HttpComponentsResponse.java
public HttpComponentsResponse(HttpClient httpClient, HttpRequestBase method) { try {//from www. j a va 2s .c o m long executionStart = System.currentTimeMillis(); HttpResponse response = httpClient.execute(method); duration = System.currentTimeMillis() - executionStart; HttpEntity responseEntity = response.getEntity(); if (responseEntity != null) { responseBody = EntityUtils.toByteArray(responseEntity); charset = getCharset(responseEntity); } else { responseBody = null; charset = null; } headers = buildHeaderMap(response); statusCode = response.getStatusLine().getStatusCode(); } catch (IOException e) { throw new IllegalArgumentException("Can not execute method", e); } finally { method.releaseConnection(); } }
From source file:edu.tum.cs.ias.knowrob.BarcodeWebLookup.java
public static String lookUpUpcDatabase(String ean) { String res = ""; if (ean != null && ean.length() > 0) { HttpClient httpclient = new DefaultHttpClient(); try {/*from w ww .java 2s . c om*/ HttpGet httpget = new HttpGet("http://www.upcdatabase.com/item/" + ean); httpget.getParams().setParameter(CoreProtocolPNames.HTTP_CONTENT_CHARSET, "UTF-8"); // Create a response handler ResponseHandler<byte[]> handler = new ResponseHandler<byte[]>() { public byte[] handleResponse(HttpResponse response) throws ClientProtocolException, IOException { HttpEntity entity = response.getEntity(); if (entity != null) { return EntityUtils.toByteArray(entity); } else { return null; } } }; String responseBody = new String(httpclient.execute(httpget, handler), "UTF-8"); // return null if not found if (responseBody.contains("Item Not Found")) return null; else if (responseBody.contains("Item Record")) { // Parse response document Matcher matcher = Pattern.compile("<tr><td>Description<\\/td><td><\\/td><td>(.*)<\\/td><\\/tr>") .matcher(responseBody); if (matcher.find()) { res = matcher.group(1); } } } catch (UnsupportedEncodingException uee) { uee.printStackTrace(); } catch (ClientProtocolException cpe) { cpe.printStackTrace(); } catch (IOException ioe) { ioe.printStackTrace(); } finally { if (httpclient != null) { httpclient.getConnectionManager().shutdown(); } } } return res; }
From source file:com.swisscom.refimpl.control.ServiceControl.java
public List<Service> retrieveServices() { try {//from www . java2s .c o m HttpResponse response = requestor.retrieveServices(Constants.MERCHANT_ID, true); Services srvs = MAPPER.readValue(EntityUtils.toByteArray(response.getEntity()), Services.class); Collections.sort(srvs.getServices(), Service.COMPARATOR_BY_SERVICE_ID); return srvs.getServices(); } catch (Exception e) { throw new RuntimeException(e); } }
From source file:com.johnson.grab.browser.HttpClientUtil.java
/** * Get content by url as string//from www. ja v a 2 s . co m * @param url * original url * @return * page content * @throws java.io.IOException */ public static String getContent(String url) throws CrawlException, IOException { // construct request HttpGet request = new HttpGet(url); request.setConfig(RequestConfig.custom().setConnectionRequestTimeout(CONNECTION_REQUEST_TIMEOUT) .setSocketTimeout(SOCKECT_TIMEOUT).build()); // construct response handler ResponseHandler<String> handler = new ResponseHandler<String>() { @Override public String handleResponse(final HttpResponse response) throws IOException { StatusLine status = response.getStatusLine(); // status if (status.getStatusCode() != HttpStatus.SC_OK) { throw new HttpResponseException(status.getStatusCode(), status.getReasonPhrase()); } // get encoding in header String encoding = getPageEncoding(response); boolean encodingFounded = true; if (StringUtil.isEmpty(encoding)) { encodingFounded = false; encoding = UniversalConstants.Encoding.ISO_8859_1; } // get content and find real encoding HttpEntity entity = response.getEntity(); if (entity == null) { return null; } // get content byte[] contentBytes = EntityUtils.toByteArray(entity); if (contentBytes == null) { return null; } // found encoding if (encodingFounded) { return new String(contentBytes, encoding); } // attempt to discover encoding String rawContent = new String(contentBytes, UniversalConstants.Encoding.DEFAULT); Matcher matcher = PATTERN_HTML_CHARSET.matcher(rawContent); if (matcher.find()) { String realEncoding = matcher.group(1); if (!encoding.equalsIgnoreCase(realEncoding)) { // bad luck :( return new String(rawContent.getBytes(encoding), realEncoding); } } // not found right encoding :) return rawContent; } }; // execute CloseableHttpClient client = HttpClientHolder.getClient(); return client.execute(request, handler); }
From source file:com.nanocrawler.data.Page.java
public void load(HttpEntity entity) throws Exception { contentType = null;// w ww . j a v a2s . c om Header type = entity.getContentType(); if (type != null) { contentType = type.getValue(); } contentEncoding = null; Header encoding = entity.getContentEncoding(); if (encoding != null) { contentEncoding = encoding.getValue(); } Charset charset = ContentType.getOrDefault(entity).getCharset(); if (charset != null) { contentCharset = charset.displayName(); } contentData = EntityUtils.toByteArray(entity); }
From source file:ch.mbae.pusher.transport.HttpClientPusherTransport.java
public PusherResponse fetch(URL url, String jsonData) throws PusherTransportException { PusherResponse response = new PusherResponse(); try {//from w w w .j av a 2 s . co m HttpPost post = new HttpPost(url.toURI()); post.addHeader("Content-Type", "application/json"); post.setEntity(new StringEntity(jsonData)); // executr post request HttpResponse httpResponse = this.httpClient.execute(post); // get the content response.setContent(EntityUtils.toByteArray(httpResponse.getEntity())); // extract and set headers response.setHeaders(this.extractHeaders(httpResponse)); // set http status response.setResponseCode(httpResponse.getStatusLine().getStatusCode()); } catch (URISyntaxException ex) { throw new PusherTransportException("bad uri syntax", ex); } catch (ClientProtocolException ex) { throw new PusherTransportException("bad client protocol", ex); } catch (IOException ex) { throw new PusherTransportException("i/o failed", ex); } return response; }
From source file:org.sonatype.nexus.repositories.metadata.Hc4RawTransport.java
@Override public byte[] readRawData(final String path) throws IOException { final HttpGet get = new HttpGet(createUrlWithPath(path)); get.setHeader("Accept", ContentType.APPLICATION_XML.getMimeType()); final HttpResponse response = httpClient.execute(get); try {// w ww .j a v a 2 s . c o m final int statusCode = response.getStatusLine().getStatusCode(); if (statusCode == 200 && response.getEntity() != null) { return EntityUtils.toByteArray(response.getEntity()); } else if (statusCode == 404) { return null; } else { throw new IOException("The response was not successful: " + response.getStatusLine()); } } finally { EntityUtils.consume(response.getEntity()); } }
From source file:com.sangupta.jerry.http.WebResponseHandler.java
/** * @see org.apache.http.client.ResponseHandler#handleResponse(org.apache.http.HttpResponse) */// ww w . j ava2 s .c o m @Override public WebResponse handleResponse(HttpResponse response, HttpContext localHttpContext) throws ClientProtocolException, IOException { StatusLine statusLine = response.getStatusLine(); HttpEntity entity = response.getEntity(); byte[] bytes = null; if (entity != null) { bytes = EntityUtils.toByteArray(entity); } final WebResponse webResponse = new WebResponse(bytes); // decipher from status line webResponse.responseCode = statusLine.getStatusCode(); webResponse.message = statusLine.getReasonPhrase(); // set size if (entity != null) { webResponse.size = entity.getContentLength(); } else { long value = 0; Header header = response.getFirstHeader(HttpHeaders.CONTENT_LENGTH); if (header != null) { String headerValue = header.getValue(); if (AssertUtils.isNotEmpty(headerValue)) { try { value = Long.parseLong(headerValue); } catch (Exception e) { // eat the exception } } } webResponse.size = value; } // content type if (entity != null && entity.getContentType() != null) { webResponse.contentType = entity.getContentType().getValue(); } // response headers final Header[] responseHeaders = response.getAllHeaders(); if (AssertUtils.isNotEmpty(responseHeaders)) { for (Header header : responseHeaders) { webResponse.headers.put(header.getName(), header.getValue()); } } // charset try { ContentType type = ContentType.get(entity); if (type != null) { webResponse.charSet = type.getCharset(); } } catch (UnsupportedCharsetException e) { // we are unable to find the charset for the content // let's leave it to be considered binary } // fill in the redirect uri chain RedirectLocations locations = (RedirectLocations) localHttpContext .getAttribute(HttpClientContext.REDIRECT_LOCATIONS); if (AssertUtils.isNotEmpty(locations)) { webResponse.setRedirectChain(locations.getAll()); } // return the object finally return webResponse; }
From source file:org.labcrypto.wideroid.helpers.DownloadHelper.java
public byte[] downloadAsByteArray(final String url) throws IOException { try {//w w w . j a v a 2 s . c o m AsyncTask<Void, Void, byte[]> asyncTask = new AsyncTask<Void, Void, byte[]>() { @Override protected byte[] doInBackground(Void... params) { HttpGet httpGet = null; try { HttpClient httpClient = AndroidHttpClient.newInstance(""); httpGet = new HttpGet(url); HttpResponse httpResponse = httpClient.execute(httpGet); if (httpResponse.getStatusLine().getStatusCode() != 200) { return null; } return EntityUtils.toByteArray(httpResponse.getEntity()); } catch (Exception e) { e.printStackTrace(); throw new RuntimeException(e); } finally { try { if (httpGet != null) { httpGet.abort(); } } catch (Exception e) { } } } }; asyncTask.execute(new Void[] {}); return asyncTask.get(); } catch (Exception e) { e.printStackTrace(); throw new RuntimeException(e); } }
From source file:org.netvogue.server.webmvc.pusher.HttpClientPusherTransport.java
@Override public PusherResponse fetch(URL url, String jsonData) throws PusherTransportException { PusherResponse response = new PusherResponse(); try {/* w w w. java 2 s . c om*/ HttpPost post = new HttpPost(url.toURI()); post.addHeader("Content-Type", "application/json"); post.setEntity(new StringEntity(jsonData)); // executr post request HttpResponse httpResponse = this.httpClient.execute(post); // get the content response.setContent(EntityUtils.toByteArray(httpResponse.getEntity())); // extract and set headers response.setHeaders(this.extractHeaders(httpResponse)); // set http status response.setResponseCode(httpResponse.getStatusLine().getStatusCode()); } catch (URISyntaxException ex) { throw new PusherTransportException("bad uri syntax", ex); } catch (ClientProtocolException ex) { throw new PusherTransportException("bad client protocol", ex); } catch (IOException ex) { throw new PusherTransportException("i/o failed", ex); } return response; }