List of usage examples for org.apache.http.util EntityUtils getContentCharSet
@Deprecated public static String getContentCharSet(HttpEntity httpEntity) throws ParseException
From source file:com.flicklib.service.HttpClientSourceLoader.java
private Source buildSource(String url, HttpResponse response, HttpRequestBase httpMethod, HttpContext ctx) throws IOException { LOGGER.info("Finished loading at " + httpMethod.getURI().toString()); final HttpEntity entity = response.getEntity(); String responseCharset = EntityUtils.getContentCharSet(entity); String contentType = EntityUtils.getContentMimeType(entity); LOGGER.info("Response charset = " + responseCharset); String content = EntityUtils.toString(entity); HttpUriRequest req = (HttpUriRequest) ctx.getAttribute(ExecutionContext.HTTP_REQUEST); HttpHost target = (HttpHost) ctx.getAttribute(ExecutionContext.HTTP_TARGET_HOST); URI resultUri;/*from w w w . ja va 2s.co m*/ try { resultUri = (target != null && req != null) ? new URI(target.toURI() + req.getURI().toString()) : httpMethod.getURI(); } catch (URISyntaxException e) { e.printStackTrace(); resultUri = httpMethod.getURI(); } // String contentType = URLConnection.guessContentTypeFromName(url) return new Source(resultUri.toString(), content, contentType, url); }
From source file:com.mgmtp.perfload.core.client.web.http.DefaultHttpClientManager.java
/** * Executes an HTTP request using the internal {@link HttpClient} instance encapsulating the * Http response in the returns {@link ResponseInfo} object. This method takes to time * measurements around the request execution, one after calling * {@link HttpClient#execute(org.apache.http.client.methods.HttpUriRequest, HttpContext)} (~ * first-byte measurment) and the other one later after the complete response was read from the * stream. These measurements are return as properties of the {@link ResponseInfo} object. *///from w w w. jav a 2 s. c o m @Override public ResponseInfo executeRequest(final HttpRequestBase request, final HttpContext context, final UUID requestId) throws IOException { request.addHeader(EXECUTION_ID_HEADER, executionId.toString()); request.addHeader(OPERATION_HEADER, operation); request.addHeader(REQUEST_ID_HEADER, requestId.toString()); String uri = request.getURI().toString(); String type = request.getMethod(); TimeInterval tiBeforeBody = new TimeInterval(); TimeInterval tiTotal = new TimeInterval(); tiBeforeBody.start(); tiTotal.start(); long timestamp = System.currentTimeMillis(); HttpResponse response = getHttpClient().execute(request, context); tiBeforeBody.stop(); // This actually downloads the response body: HttpEntity entity = response.getEntity(); byte[] body = EntityUtils.toByteArray(entity); tiTotal.stop(); StatusLine statusLine = response.getStatusLine(); int statusCode = statusLine.getStatusCode(); String statusMsg = statusLine.getReasonPhrase(); String responseCharset = EntityUtils.getContentCharSet(entity); String contentType = EntityUtils.getContentMimeType(entity); String bodyAsString = bodyAsString(contentType, responseCharset, body); Header[] headers = response.getAllHeaders(); Map<String, String> responseHeaders = newHashMapWithExpectedSize(headers.length); for (Header header : headers) { responseHeaders.put(header.getName(), header.getValue()); } return new ResponseInfo(type, uri, statusCode, statusMsg, responseHeaders, body, bodyAsString, responseCharset, contentType, timestamp, tiBeforeBody, tiTotal, executionId, requestId); }
From source file:com.servoy.extensions.plugins.http.Response.java
/** * Get the charset of the response body. * * @sample/*from ww w. j a v a2 s .c om*/ * var charset = response.getCharset(); */ public String js_getCharset() { return EntityUtils.getContentCharSet(res.getEntity()); }
From source file:org.eclipse.mylyn.internal.hudson.core.client.HudsonOperation.java
private void updateCrumb(IOperationMonitor monitor) throws IOException { HttpGet request = createGetRequest(baseUrl()); HttpResponse response = getClient().execute(request, monitor); try {/* w ww. ja va 2 s . c o m*/ InputStream in = HttpUtil.getResponseBodyAsStream(response.getEntity(), monitor); try { String charSet = EntityUtils.getContentCharSet(response.getEntity()); BufferedReader reader = new BufferedReader( new InputStreamReader(in, (charSet != null) ? charSet : "UTF-8")); //$NON-NLS-1$ HtmlStreamTokenizer tokenizer = new HtmlStreamTokenizer(reader, null); for (Token token = tokenizer.nextToken(); token.getType() != Token.EOF; token = tokenizer .nextToken()) { if (token.getType() == Token.TAG) { // <script>crumb.init(".crumb", "8aae0557456447d391f81f2ef2eafa4d");</script> HtmlTag tag = (HtmlTag) token.getValue(); if (tag.getTagType() == Tag.SCRIPT) { String text = HtmlUtil.getTextContent(tokenizer); Pattern pattern = Pattern.compile("crumb.init\\(\".*\",\\s*\"([a-zA-Z0-9]*)\"\\)"); //$NON-NLS-1$ Matcher matcher = pattern.matcher(text); if (matcher.find()) { getClient().getContext().setAttribute(ID_CONTEXT_CRUMB, matcher.group(1)); break; } } } } } catch (ParseException e) { // ignore } finally { in.close(); } } finally { HttpUtil.release(request, response, monitor); } }
From source file:org.pixmob.feedme.net.NetworkClient.java
public void downloadUnreadEntries(List<ContentValues> entries) throws IOException { final Map<String, String> params = new HashMap<String, String>(4); params.put("client", clientId); params.put("n", prefs.getString(SP_KEY_NUMBER_OF_ITEMS, "50")); params.put("ck", String.valueOf(System.currentTimeMillis())); final String continuation = prefs.getString(SP_KEY_CONTINUATION, null); if (continuation != null) { params.put("c", continuation); }//from w w w . ja v a 2s. c om final HttpGet req = new HttpGet(createServiceUri("/atom/user/-/state/com.google/reading-list", params)); prepareRequest(req); Log.i(TAG, "Sending request for downloading entries: " + req.getURI().toASCIIString()); final EntriesParser.Results parseResults = new EntriesParser.Results(); parseResults.entries = entries; HttpResponse resp = null; FileOutputStream output = null; int statusCode = 0; try { resp = client.execute(req); final StatusLine statusLine = resp.getStatusLine(); statusCode = statusLine.getStatusCode(); Log.i(TAG, "Entries download status code: " + statusCode); if (statusCode != 200) { throw new IOException("Entries download error"); } final HttpEntity entity = resp.getEntity(); final InputStream input = entity.getContent(); parser.parse(input, EntityUtils.getContentCharSet(entity), parseResults); prefsEditor.putString(SP_KEY_CONTINUATION, parseResults.continuation); Features.getFeature(SharedPreferencesSaverFeature.class).save(prefsEditor); } catch (IOException e) { throw new NetworkClientException("Failed to get unread entries", req.getURI().toString(), statusCode, e); } finally { if (output != null) { try { output.close(); } catch (IOException ignore) { } } closeResources(req, resp); } }
From source file:net.bitquill.delicious.api.DeliciousClient.java
private final XmlPullParser getXmlParser(HttpEntity e) throws XmlPullParserException, IllegalStateException, IOException { if (mXmlParserFactory == null) { mXmlParserFactory = XmlPullParserFactory.newInstance(); }//from ww w.j a v a 2s . c om XmlPullParser parser = mXmlParserFactory.newPullParser(); String charSet = EntityUtils.getContentCharSet(e); if (charSet == null) { charSet = HTTP.DEFAULT_CONTENT_CHARSET; } parser.setInput(e.getContent(), charSet); return parser; }
From source file:bigbird.benchmark.BenchmarkWorker.java
public void run() { HttpResponse response = null;/*from w w w . j a v a2s . c o m*/ DefaultHttpClientConnection conn = new DefaultHttpClientConnection(); String hostname = targetHost.getHostName(); int port = targetHost.getPort(); if (port == -1) { port = 80; } // Populate the execution context this.context.setAttribute(ExecutionContext.HTTP_CONNECTION, conn); this.context.setAttribute(ExecutionContext.HTTP_TARGET_HOST, this.targetHost); // this.context.setAttribute(ExecutionContext.HTTP_REQUEST, this.request); stats.start(); for (int i = 0; i < count; i++) { try { HttpRequest req = requestGenerator.generateRequest(count); if (!conn.isOpen()) { Socket socket = null; if ("https".equals(targetHost.getSchemeName())) { SocketFactory socketFactory = SSLSocketFactory.getDefault(); socket = socketFactory.createSocket(hostname, port); } else { socket = new Socket(hostname, port); } conn.bind(socket, params); } try { // Prepare request this.httpexecutor.preProcess(req, this.httpProcessor, this.context); // Execute request and get a response response = this.httpexecutor.execute(req, conn, this.context); // Finalize response this.httpexecutor.postProcess(response, this.httpProcessor, this.context); } catch (HttpException e) { stats.incWriteErrors(); if (this.verbosity >= 2) { System.err.println("Failed HTTP request : " + e.getMessage()); } continue; } verboseOutput(req, response); if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) { stats.incSuccessCount(); } else { stats.incFailureCount(); continue; } HttpEntity entity = response.getEntity(); String charset = EntityUtils.getContentCharSet(entity); if (charset == null) { charset = HTTP.DEFAULT_CONTENT_CHARSET; } long contentlen = 0; if (entity != null) { InputStream instream = entity.getContent(); int l = 0; while ((l = instream.read(this.buffer)) != -1) { stats.incTotalBytesRecv(l); contentlen += l; if (this.verbosity >= 4) { String s = new String(this.buffer, 0, l, charset); System.out.print(s); } } instream.close(); } if (this.verbosity >= 4) { System.out.println(); System.out.println(); } if (!keepalive || !this.connstrategy.keepAlive(response, this.context)) { conn.close(); } stats.setContentLength(contentlen); } catch (IOException ex) { ex.printStackTrace(); stats.incFailureCount(); if (this.verbosity >= 2) { System.err.println("I/O error: " + ex.getMessage()); } } } stats.finish(); if (response != null) { Header header = response.getFirstHeader("Server"); if (header != null) { stats.setServerName(header.getValue()); } } try { conn.close(); } catch (IOException ex) { ex.printStackTrace(); stats.incFailureCount(); if (this.verbosity >= 2) { System.err.println("I/O error: " + ex.getMessage()); } } }
From source file:wsattacker.http.transport.SoapHttpClient.java
/** * Sends the SOAP message to the initialized endpoint destination * //w w w. ja v a 2 s .c o m * @param soap * @return * @throws IOException */ public SoapResponse sendSoap(String soap) throws IOException { final StringEntity httpBody = new StringEntity(soap); post.setEntity(httpBody); int maxTries = MAX_RETRY_NUMBER; HttpResponse httpResponse = null; while (httpResponse == null) { try { httpResponse = client.execute(post); } catch (IOException ex) { if (maxTries == 0) { throw ex; } else { maxTries--; LOG.warn(ex.getLocalizedMessage()); LOG.warn("Trying to send the message once more"); LOG.debug(ex); } } } SoapResponse soapResponse = new SoapResponse(); if (LOG.isDebugEnabled()) { LOG.debug(httpResponse.getStatusLine()); } soapResponse.setStatusLine(httpResponse.getStatusLine().toString()); for (Header h : httpResponse.getAllHeaders()) { final String name = h.getName(); final String value = h.getValue(); if (LOG.isDebugEnabled()) { final String headerDebug = name + ": " + value; LOG.debug(headerDebug); } HttpHeader newHeader = new HttpHeader(name, value); soapResponse.getHeaders().add(newHeader); } LOG.debug("waiting for response: "); final HttpEntity entity = httpResponse.getEntity(); final String charset = EntityUtils.getContentCharSet(entity); final String responseString = EntityUtils.toString(entity, charset); soapResponse.setBody(responseString); return soapResponse; }
From source file:com.servoy.extensions.plugins.http.HttpProvider.java
/** * @param url//from w w w .j a va2s . co m * @param httpClientName * @param username * @param password * @return */ private String getPageData(String url, String httpClientName, String username, String password) { try { DefaultHttpClient client = getOrCreateHTTPclient(httpClientName, url); HttpGet get = new HttpGet(url); HttpResponse res = client.execute(get); // you can get the status from here... (return code) BasicCredentialsProvider bcp = new BasicCredentialsProvider(); if (username != null) { URL _url = createURLFromString(url); bcp.setCredentials(new AuthScope(_url.getHost(), _url.getPort()), new UsernamePasswordCredentials(username, password)); client.setCredentialsProvider(bcp); } lastPageEncoding = EntityUtils.getContentCharSet(res.getEntity()); return EntityUtils.toString(res.getEntity()); } catch (Exception e) { Debug.error(e); return "";//$NON-NLS-1$ } }
From source file:org.ellis.yun.search.test.httpclient.HttpClientTest.java
@Test @SuppressWarnings("deprecation") public void testStringEntity() throws Exception { StringEntity myEntity = new StringEntity("important message", "UTF-8"); System.out.println(myEntity.getContentType() + ""); System.out.println(myEntity.getContentLength() + ""); System.out.println(EntityUtils.getContentCharSet(myEntity)); System.out.println(EntityUtils.toString(myEntity)); System.out.println(EntityUtils.toByteArray(myEntity).length + ""); }