Example usage for org.apache.http.util EntityUtils getContentCharSet

List of usage examples for org.apache.http.util EntityUtils getContentCharSet

Introduction

In this page you can find the example usage for org.apache.http.util EntityUtils getContentCharSet.

Prototype

@Deprecated
    public static String getContentCharSet(HttpEntity httpEntity) throws ParseException 

Source Link

Usage

From source file:Main.java

private static String reverseGeocode(String url) {
    try {//  w  w  w .  j  a  v  a2 s  .c o  m
        HttpGet httpGet = new HttpGet(url);
        BasicHttpParams httpParams = new BasicHttpParams();
        DefaultHttpClient defaultHttpClient = new DefaultHttpClient(httpParams);
        HttpResponse httpResponse = defaultHttpClient.execute(httpGet);
        int responseCode = httpResponse.getStatusLine().getStatusCode();
        if (responseCode == HttpStatus.SC_OK) {
            String charSet = EntityUtils.getContentCharSet(httpResponse.getEntity());
            if (null == charSet) {
                charSet = "UTF-8";
            }
            String str = new String(EntityUtils.toByteArray(httpResponse.getEntity()), charSet);
            defaultHttpClient.getConnectionManager().shutdown();
            return str;
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    return null;
}

From source file:Main.java

public static String reverseGeocode(String url) {
    try {//from  w w  w  .  ja  va2  s . co m
        HttpGet httpGet = new HttpGet(url);
        BasicHttpParams httpParams = new BasicHttpParams();
        DefaultHttpClient defaultHttpClient = new DefaultHttpClient(httpParams);
        HttpResponse httpResponse = defaultHttpClient.execute(httpGet);
        int responseCode = httpResponse.getStatusLine().getStatusCode();
        if (responseCode == HttpStatus.SC_OK) {
            String charSet = EntityUtils.getContentCharSet(httpResponse.getEntity());
            if (null == charSet) {
                charSet = "UTF-8";
            }
            String str = new String(EntityUtils.toByteArray(httpResponse.getEntity()), charSet);
            defaultHttpClient.getConnectionManager().shutdown();
            Log.i("-----------", "str = " + str);
            return str;
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    return null;
}

From source file:Main.java

public static String gzipToString(final HttpEntity entity, final String defaultCharset)
        throws IOException, ParseException {
    if (entity == null) {
        throw new IllegalArgumentException("HTTP entity may not be null");
    }// ww w  .  j a  v a 2s . c  o  m
    InputStream instream = entity.getContent();
    if (instream == null) {
        return "";
    }
    // gzip logic start
    if (entity.getContentEncoding().getValue().contains("gzip")) {
        instream = new GZIPInputStream(instream);
    }
    // gzip logic end
    if (entity.getContentLength() > Integer.MAX_VALUE) {
        throw new IllegalArgumentException("HTTP entity too large to be buffered in memory");
    }
    int i = (int) entity.getContentLength();
    if (i < 0) {
        i = 4096;
    }
    String charset = EntityUtils.getContentCharSet(entity);
    if (charset == null) {
        charset = defaultCharset;
    }
    if (charset == null) {
        charset = HTTP.DEFAULT_CONTENT_CHARSET;
    }
    Reader reader = new InputStreamReader(instream, charset);
    CharArrayBuffer buffer = new CharArrayBuffer(i);
    try {
        char[] tmp = new char[1024];
        int l;
        while ((l = reader.read(tmp)) != -1) {
            buffer.append(tmp, 0, l);
        }
    } finally {
        reader.close();
    }
    return buffer.toString();
}

From source file:org.mobicents.servlet.restcomm.util.HttpUtils.java

public static Map<String, String> toMap(final HttpEntity entity) throws IllegalStateException, IOException {

    String contentType = null;// w ww  .  j a  va  2  s  .co m
    String charset = null;

    contentType = EntityUtils.getContentMimeType(entity);
    charset = EntityUtils.getContentCharSet(entity);

    List<NameValuePair> parameters = null;
    if (contentType != null && contentType.equalsIgnoreCase(CONTENT_TYPE)) {
        parameters = URLEncodedUtils.parse(entity);
    } else {
        final String content = EntityUtils.toString(entity, HTTP.ASCII);
        if (content != null && content.length() > 0) {
            parameters = new ArrayList<NameValuePair>();
            URLEncodedUtils.parse(parameters, new Scanner(content), charset);
        }
    }

    final Map<String, String> map = new HashMap<String, String>();
    for (final NameValuePair parameter : parameters) {
        map.put(parameter.getName(), parameter.getValue());
    }
    return map;
}

From source file:ru.algorithmist.jquant.infr.GAEHTTPClient.java

public String getContent(URI url) throws IOException {
    HttpResponse resp = client.execute(new HttpGet(url));
    HttpEntity entity = resp.getEntity();
    String encoding = EntityUtils.getContentCharSet(entity);

    logger.info("Character encoding  " + encoding);
    if (encoding == null) {
        encoding = "UTF-8";
    }//from w ww . jav  a 2s .com
    return new String(EntityUtils.toByteArray(entity), encoding);
}

From source file:com.francisli.processing.http.HttpResponse.java

HttpResponse(org.apache.http.HttpResponse response) throws IOException {
    this.response = response;

    StatusLine status = response.getStatusLine();
    this.statusCode = status.getStatusCode();
    this.statusMessage = status.getReasonPhrase();

    HttpEntity entity = response.getEntity();
    if (entity.getContentType() != null) {
        contentType = entity.getContentType().getValue();
    }//from  w  w  w. j av  a  2s. c om
    content = EntityUtils.toByteArray(entity);
    contentLength = content.length;
    contentCharSet = EntityUtils.getContentCharSet(entity);
    if (contentCharSet == null) {
        contentCharSet = HTTP.UTF_8;
    }
}

From source file:cn.jachohx.crawler.Page.java

/**
 * Loads the content of this page from a fetched
 * HttpEntity.//from  w w  w .j  av a  2 s  .  c o m
 */
public void load(HttpEntity entity) throws Exception {

    contentType = null;
    Header type = entity.getContentType();
    if (type != null) {
        contentType = type.getValue();
    }

    contentEncoding = null;
    Header encoding = entity.getContentEncoding();
    if (encoding != null) {
        contentEncoding = encoding.getValue();
    }

    contentCharset = EntityUtils.getContentCharSet(entity);

    contentData = EntityUtils.toByteArray(entity);

}

From source file:ru.algorithmist.jquant.infr.GAEHTTPClient.java

@Override
public String getContent(String url) throws IOException {
    logger.info("Query " + url);
    HttpResponse resp = client.execute(new HttpGet(url));
    HttpEntity entity = resp.getEntity();
    String encoding = EntityUtils.getContentCharSet(entity);

    logger.info("Character encoding  " + encoding);
    if (encoding == null) {
        encoding = "UTF-8";
    }// w w  w. j av  a 2 s. c o m
    return new String(EntityUtils.toByteArray(entity), encoding);
}

From source file:org.trancecode.xproc.step.HttpResponseHandler.java

@Override
public XProcHttpResponse handleResponse(final HttpResponse httpResponse) throws IOException {
    final XProcHttpResponse response = new XProcHttpResponse();
    final HttpEntity entity = httpResponse.getEntity();
    final String contentCharset = EntityUtils.getContentCharSet(entity) == null ? "utf-8"
            : EntityUtils.getContentCharSet(entity);
    final String contentType = constructContentType(entity.getContentType());
    ContentType contentMimeType = null;/* ww w.  j a v a 2s . co m*/
    try {
        contentMimeType = new ContentType(contentType);
    } catch (ParseException e) {
        contentMimeType = new ContentType("text", "plain", null);
    }
    if (!StringUtils.isEmpty(overrideContentType)) {
        contentMimeType = verifyContentType(contentMimeType, overrideContentType);
    }
    final BodypartResponseParser parser = new BodypartResponseParser(entity.getContent(), null,
            httpResponse.getParams(), contentType, contentCharset);

    if (!detailed) {
        if (!statusOnly) {
            if ("multipart".equals(contentMimeType.getPrimaryType())) {
                response.setNodes(constructMultipart(contentMimeType, contentType, parser));
            } else {
                final BodypartResponseParser.BodypartEntity part = parser.parseBodypart(false);
                final Iterable<XdmNode> body = constructBody(contentMimeType, contentType, part);
                if (body != null) {
                    response.setNodes(body);
                }
            }
        }
    } else {
        final SaxonBuilder builder = new SaxonBuilder(processor.getUnderlyingConfiguration());
        builder.startDocument();
        builder.startElement(XProcXmlModel.Elements.RESPONSE);
        builder.attribute(XProcXmlModel.Attributes.STATUS,
                Integer.toString(httpResponse.getStatusLine().getStatusCode()));
        if (!statusOnly) {
            final Header[] headers = httpResponse.getAllHeaders();
            for (final Header header : headers) {
                builder.startElement(XProcXmlModel.Elements.HEADER);
                builder.attribute(XProcXmlModel.Attributes.NAME, header.getName());
                builder.attribute(XProcXmlModel.Attributes.VALUE, header.getValue());
                builder.endElement();
            }
            if ("multipart".equals(contentMimeType.getPrimaryType())) {
                builder.nodes(constructMultipart(contentMimeType, contentType, parser));
            } else {
                final BodypartResponseParser.BodypartEntity part = parser.parseBodypart(false);
                final Iterable<XdmNode> body = constructBody(contentMimeType, contentType, part);
                if (body != null) {
                    builder.nodes(body);
                }
            }
        }
        builder.endElement();
        builder.endDocument();
        response.setNodes(ImmutableList.of(builder.getNode()));
    }
    EntityUtils.consume(entity);
    return response;
}

From source file:com.basistech.readability.HttpPageReader.java

/** {@inheritDoc}*/
@Override//from  w  w  w.j  a v a2 s  . c om
public String readPage(String url) throws PageReadException {
    LOG.info("Reading " + url);
    HttpParams httpParameters = new BasicHttpParams();
    // Set the timeout in milliseconds until a connection is established.
    int timeoutConnection = 3000;
    HttpConnectionParams.setConnectionTimeout(httpParameters, timeoutConnection);
    // Set the default socket timeout (SO_TIMEOUT) 
    // in milliseconds which is the timeout for waiting for data.
    int timeoutSocket = 10000;
    HttpConnectionParams.setSoTimeout(httpParameters, timeoutSocket);
    DefaultHttpClient httpclient = new DefaultHttpClient(httpParameters);
    HttpContext localContext = new BasicHttpContext();
    HttpGet get = new HttpGet(url);
    InputStream response = null;
    HttpResponse httpResponse = null;
    try {
        try {
            httpResponse = httpclient.execute(get, localContext);
            int resp = httpResponse.getStatusLine().getStatusCode();
            if (HttpStatus.SC_OK != resp) {
                LOG.error("Download failed of " + url + " status " + resp + " "
                        + httpResponse.getStatusLine().getReasonPhrase());
                return null;
            }
            String respCharset = EntityUtils.getContentCharSet(httpResponse.getEntity());
            return readContent(httpResponse.getEntity().getContent(), respCharset);
        } finally {
            if (response != null) {
                response.close();
            }
            if (httpResponse != null && httpResponse.getEntity() != null) {
                httpResponse.getEntity().consumeContent();
            }

        }
    } catch (IOException e) {
        LOG.error("Download failed of " + url, e);
        throw new PageReadException("Failed to read " + url, e);
    }
}