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:at.general.solutions.android.ical.remote.HttpDownloadThread.java

@Override
public void run() {
    HttpParams params = new BasicHttpParams();
    ConnManagerParams.setMaxTotalConnections(params, 100);
    HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1);
    HttpProtocolParams.setUserAgent(params, USER_AGENT);

    SchemeRegistry schemeRegistry = new SchemeRegistry();
    schemeRegistry.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80));
    schemeRegistry.register(new Scheme("https", new EasySSLSocketFactory(), 443));

    ClientConnectionManager cm = new ThreadSafeClientConnManager(params, schemeRegistry);
    DefaultHttpClient client = new DefaultHttpClient(cm, params);

    if (useAuthentication) {
        client.getCredentialsProvider().setCredentials(new AuthScope(AuthScope.ANY_HOST, AuthScope.ANY_PORT),
                new UsernamePasswordCredentials(remoteUsername, remotePassword));
    }//w w  w. j  a  v  a 2 s  . c  o  m

    HttpGet get = new HttpGet(remoteUrl);

    try {
        super.sendInitMessage(R.string.downloading);

        HttpResponse response = client.execute(get);
        Log.d(LOG_TAG, response.getStatusLine().getReasonPhrase() + " "
                + isGoodResponse(response.getStatusLine().getStatusCode()));
        if (isGoodResponse(response.getStatusLine().getStatusCode())) {
            HttpEntity entity = response.getEntity();

            InputStream instream = entity.getContent();
            if (instream == null) {
                super.sendErrorMessage(R.string.couldnotConnectToRemoteserver);
                return;
            }
            if (entity.getContentLength() > Integer.MAX_VALUE) {
                super.sendErrorMessage(R.string.remoteFileTooLarge);
                return;
            }
            int i = (int) entity.getContentLength();
            if (i < 0) {
                i = 4096;
            }
            String charset = EntityUtils.getContentCharSet(entity);
            if (charset == null) {
                charset = encoding;
            }
            if (charset == null) {
                charset = HTTP.DEFAULT_CONTENT_CHARSET;
            }
            Reader reader = new InputStreamReader(instream, charset);
            CharArrayBuffer buffer = new CharArrayBuffer(i);

            super.sendMaximumMessage(i);

            try {
                char[] tmp = new char[1024];
                int l;
                while ((l = reader.read(tmp)) != -1) {
                    buffer.append(tmp, 0, l);
                    super.sendProgressMessage(buffer.length());
                }
            } finally {
                reader.close();
            }

            super.sendFinishedMessage(buffer.toString());
        } else {
            int errorMsg = R.string.couldnotConnectToRemoteserver;
            if (isAccessDenied(response.getStatusLine().getStatusCode())) {
                errorMsg = R.string.accessDenied;
            } else if (isFileNotFound(response.getStatusLine().getStatusCode())) {
                errorMsg = R.string.remoteFileNotFound;
            }
            super.sendErrorMessage(errorMsg);
        }
    } catch (UnknownHostException e) {
        super.sendErrorMessage(R.string.unknownHostException, e);
        Log.e(LOG_TAG, "Error occured", e);
    } catch (Throwable e) {
        super.sendErrorMessage(R.string.couldnotConnectToRemoteserver, e);
        Log.e(LOG_TAG, "Error occured", e);
    }

    finally {
        client.getConnectionManager().shutdown();
    }
}

From source file:id.nci.stm_9.HkpKeyServer.java

@Override
public String get(long keyId) throws QueryException {
    HttpClient client = new DefaultHttpClient();
    try {/*w  ww .  j  a v  a2s .c  om*/
        HttpGet get = new HttpGet("http://" + mHost + ":" + mPort + "/pks/lookup?op=get&search=0x"
                + PgpKeyHelper.convertKeyToHex(keyId));

        HttpResponse response = client.execute(get);
        if (response.getStatusLine().getStatusCode() != HttpStatus.SC_OK) {
            throw new QueryException("not found");
        }

        HttpEntity entity = response.getEntity();
        InputStream is = entity.getContent();
        String data = readAll(is, EntityUtils.getContentCharSet(entity));
        Matcher matcher = PgpHelper.PGP_PUBLIC_KEY.matcher(data);
        if (matcher.find()) {
            return matcher.group(1);
        }
    } catch (IOException e) {
        // nothing to do, better luck on the next keyserver
    } finally {
        client.getConnectionManager().shutdown();
    }

    return null;
}

From source file:org.openintents.openpgp.keyserver.HkpKeyServer.java

@Override
public String get(long keyId) throws QueryException {
    HttpClient client = new DefaultHttpClient();
    try {//from  w  w w  .  jav  a  2s  .c  o  m
        HttpGet get = new HttpGet(
                "http://" + mHost + ":" + mPort + "/pks/lookup?op=get&search=0x" + KeyInfo.hexFromKeyId(keyId));

        HttpResponse response = client.execute(get);
        if (response.getStatusLine().getStatusCode() != HttpStatus.SC_OK) {
            throw new QueryException("not found");
        }

        HttpEntity entity = response.getEntity();
        InputStream is = entity.getContent();
        String data = readAll(is, EntityUtils.getContentCharSet(entity));
        Matcher matcher = PGP_PUBLIC_KEY.matcher(data);
        if (matcher.find()) {
            return matcher.group(1);
        }
    } catch (IOException e) {
        e.printStackTrace();
        // nothing to do, better luck on the next keyserver
    } finally {
        client.getConnectionManager().shutdown();
    }

    return null;
}

From source file:org.couch4j.http.HttpConnectionManager.java

private JSONObject fromResponseStream(HttpEntity entity) throws IOException {
    InputStream is = entity.getContent();
    Reader reader = new InputStreamReader(is, EntityUtils.getContentCharSet(entity));
    CharArrayWriter w = new CharArrayWriter();
    StreamUtils.copy(reader, w);/*from   ww w  . ja va 2 s  . com*/
    JSONObject json = JSONObject.fromObject(w.toString());
    StreamUtils.closeSilently(reader);
    StreamUtils.closeSilently(w);
    entity.consumeContent(); // finish
    return json;
}

From source file:com.suning.mobile.ebuy.lottery.network.util.Caller.java

/**
 * //www  .ja  v a 2  s . c o m
 * @Description:
 * @Author 12050514 wangwt
 * @Date 2013-1-7
 */
private NetworkBean get(List<NameValuePair> list, String action, boolean withCache) {

    NetworkBean subean = new NetworkBean();
    list.add(new BasicNameValuePair("dcode", mApplication.getDeviceId()));
    list.add(new BasicNameValuePair("versionCode", String.valueOf(mApplication.getVersionCode())));
    String tempUrl = getTempUrl();
    String result = null;
    String url = tempUrl + action + "?" + URLEncodedUtils.format(list, LotteryApiConfig.ENCODE);
    if (mRequestCache != null && withCache) {
        result = mRequestCache.get(url);
    }

    if (result != null) {
        Log.d(TAG, "Caller.get [cached] " + url);
        subean = parseStringToSuBean(subean, result);
    } else {
        HttpGet request = new HttpGet(url);
        // request.addHeader("Accept-Encoding", "gzip");
        LogUtil.d("http url", request.getURI().toString());
        try {
            mClient.setCookieStore(SuningEBuyApplication.getInstance().getCookieStore());
            HttpResponse response = mClient.execute(request);
            if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
                HttpEntity entity = response.getEntity();
                if (entity != null) {
                    Header header = entity.getContentEncoding();
                    if (header != null) {
                        String contentEncoding = header.getValue();
                        if (contentEncoding != null) {
                            if (contentEncoding.contains("gzip")) {
                                entity = new GzipDecompressingEntity(entity);
                            }
                        }
                    }
                    String charset = EntityUtils.getContentCharSet(entity) == null ? LotteryApiConfig.ENCODE
                            : EntityUtils.getContentCharSet(entity);

                    result = new String(EntityUtils.toByteArray(entity), charset);
                    subean = parseStringToSuBean(subean, result);
                }
            } else {
                subean.setResult(2);
                subean.setMessage(mApplication.getResources().getString(R.string.cp_lottery_networkerror));
            }
        } catch (ClientProtocolException e) {
            LogUtil.logException(e);
            subean.setResult(2);
            subean.setMessage(mApplication.getResources().getString(R.string.cp_lottery_networkerror));
        } catch (IOException e) {
            LogUtil.logException(e);
            subean.setResult(2);
            subean.setMessage(mApplication.getResources().getString(R.string.cp_lottery_networkerror));
        } catch (NullPointerException e) {
            LogUtil.logException(e);
        } finally {
            if (mRequestCache != null && withCache && subean.getResult() == 0) {
                // 
                mRequestCache.put(url, result);
            }
        }
    }
    if (result != null) {
        LogUtil.d("http result", result);
    }

    return subean;
}

From source file:org.couch4j.http.HttpConnectionManager.java

private JSONArray arrayFromResponseStream(HttpEntity entity) throws IOException {
    InputStream is = entity.getContent();
    Reader reader = new InputStreamReader(is, EntityUtils.getContentCharSet(entity));
    CharArrayWriter w = new CharArrayWriter();
    StreamUtils.copy(reader, w);/* w ww  .  j  a  va2  s .  c  o  m*/
    JSONArray json = JSONArray.fromObject(w.toString());
    StreamUtils.closeSilently(reader);
    StreamUtils.closeSilently(w);
    entity.consumeContent(); // finish
    return json;
}

From source file:com.anton.gavel.ComplaintSubmission.java

public void submit() {

    //Body of your click handler
    Thread trd = new Thread(new Runnable() {
        @Override/*w  ww .j  a v  a 2 s  . c om*/
        public void run() {
            String formPath = "http://www.hamilton.ca/Hamilton.Portal/Templates/COHShell.aspx?NRMODE=Published&NRORIGINALURL=%2fCityDepartments%2fCorporateServices%2fITS%2fForms%2bin%2bDevelopment%2fMunicipal%2bLaw%2bEnforcement%2bOnline%2bComplaint%2bForm%2ehtm&NRNODEGUID=%7b4319AA7C-7E5E-4D65-9F46-CCBEC9AB86E0%7d&NRCACHEHINT=Guest";
            String hideString = "document.getElementById('mainform').style.display = 'none';";
            //this javascript will be added to the page if we are successful - we can look
            //for it to identify whether our form was successfully submitted

            // Create a new HttpClient and Post Header
            HttpClient httpClient = new DefaultHttpClient();

            ArrayList<NameValuePair> parameters = new ArrayList<NameValuePair>();

            for (String key : post_params.keySet()) {
                parameters.add(new BasicNameValuePair(key, post_params.get(key)));
                String value = post_params.get(key);
                if (value != null && value.length() > 0) {
                    value = value.substring(0, Math.min(70, value.length()));
                }
                Log.d("Runs", "key: '" + key + "', value: '" + value);
            }
            //convert to a list of name-value pairs (dum, dee dum  -  duuuuumm...)

            HttpPost request = new HttpPost(formPath);

            //set up handler/bundle to give output to main thread
            Handler handler = ((GavelMain) mContext).submissionHandler;
            Message msg = handler.obtainMessage();
            Bundle bundle = new Bundle();

            try {
                request.setEntity(new UrlEncodedFormEntity(parameters));

                HttpResponse httpResponse = httpClient.execute(request);
                HttpEntity responseEntity = httpResponse.getEntity();

                String response = responseEntity == null ? ""
                        : EntityUtils.toString(responseEntity, EntityUtils.getContentCharSet(responseEntity));
                //read the html page posted in response to our request

                appendLog(response); // logs html page response to a text file on sd card

                bundle.putBoolean("succeeded",
                        httpResponse.getStatusLine().getStatusCode() == 200 && response.contains(hideString));

                msg.setData(bundle);
                handler.sendMessage(msg);
            } catch (ClientProtocolException e) {
                bundle.putBoolean("succeeded", false);
                msg.setData(bundle);
                handler.sendMessage(msg);
            } catch (IOException e) {
                bundle.putBoolean("succeeded", false);
                msg.setData(bundle);
                handler.sendMessage(msg);
            } //code to do the HTTP request            
        }
    });
    trd.start();

    //

    return;

}

From source file:com.tiaoin.crawl.plugin.util.PageFetcherImpl.java

/**
 * EntityPage//w w w.ja  v  a2 s  .c o m
 * @date 2013-1-7 ?11:22:06
 * @param entity
 * @return
 * @throws Exception
 */
private Page load(HttpEntity entity) throws Exception {
    Page page = new Page();

    //ContentType
    String contentType = null;
    Header type = entity.getContentType();
    if (type != null)
        contentType = type.getValue();
    page.setContentType(contentType);

    //?
    String contentEncoding = null;
    Header encoding = entity.getContentEncoding();
    if (encoding != null)
        contentEncoding = encoding.getValue();
    page.setEncoding(contentEncoding);

    //
    String contentCharset = EntityUtils.getContentCharSet(entity);
    page.setCharset(contentCharset);

    //????
    String charset = config.getCharset();
    String content = this.read(entity.getContent(), charset);
    page.setContent(content);
    if (charset == null || charset.trim().length() == 0)
        page.setContentData(content.getBytes());
    else
        page.setContentData(content.getBytes(charset));

    return page;
}

From source file:com.sap.cloudlabs.connectivity.proxy.ProxyServlet.java

/**
 * Process response received from backend service and copy it to origin response of 
 * client./*from   w w  w. j ava2 s .  co m*/
 * 
 * @param request
 *            origin request of this Web application
 * @param response
 *            origin response of this Web application; this is where the
 *            backend response is copied to
 * @param backendResponse
 *            the response of the backend service
 * @param proxyUrl
 *            the URL that should replace the <code>rewriteUrl</code>
 * @param rewriteUrl
 *            the URL that should be rewritten 
 */
private void processBackendResponse(HttpServletRequest request, HttpServletResponse response,
        HttpResponse backendResponse, String proxyUrl, String rewriteUrl) throws IOException, ServletException {
    // copy response status code
    int status = backendResponse.getStatusLine().getStatusCode();
    response.setStatus(status);
    LOGGER.debug("backend response status code: " + status);

    // filter the headers to suppress the authentication dialog (only for
    // 401 - unauthorized)
    List<String> blockedHeaders = null;
    if (status == HttpServletResponse.SC_UNAUTHORIZED && request.getHeader("authorization") != null
            && request.getHeader("suppress-www-authenticate") != null) {
        blockedHeaders = Arrays.asList(new String[] { "www-authenticate" });
    } else {
        // for rewriting the URLs in the response, content-length, content-encoding 
        // and transfer-encoding (for chunked content) headers are removed and handled specially.
        blockedHeaders = Arrays
                .asList(new String[] { "content-length", "transfer-encoding", "content-encoding" });
    }

    // copy backend response headers and content
    LOGGER.debug("backend response headers: ");
    for (Header header : backendResponse.getAllHeaders()) {
        if (!blockedHeaders.contains(header.getName().toLowerCase())) {
            response.addHeader(header.getName(), header.getValue());
            LOGGER.debug("    => " + header.getName() + ": " + header.getValue());
        } else {
            LOGGER.debug("    => " + header.getName() + ": blocked response header");
        }
    }

    handleContentEncoding(backendResponse);

    // pipe and return the response
    HttpEntity entity = backendResponse.getEntity();
    if (entity != null) {
        // rewrite URL in the content of the response to make sure that
        // internal URLs point to the proxy servlet as well

        // determine charset and content as String
        @SuppressWarnings("deprecation")
        String charset = EntityUtils.getContentCharSet(entity);
        String content = EntityUtils.toString(entity);

        LOGGER.debug("URL rewriting:");
        LOGGER.debug("    => rewriteUrl: " + rewriteUrl);
        LOGGER.debug("    => proxyUrl: " + proxyUrl);

        // replace the rewriteUrl with the targetUrl
        content = content.replaceAll(rewriteUrl, proxyUrl);

        // get the bytes and open a stream (by default HttpClient uses ISO-8859-1)
        byte[] contentBytes = charset != null ? content.getBytes(charset) : content.getBytes(ISO_8859_1);
        InputStream is = new ByteArrayInputStream(contentBytes);

        // set the new content length
        response.setContentLength(contentBytes.length);

        // return the modified content
        pipe(is, response.getOutputStream());
    }

}

From source file:org.sufficientlysecure.keychain.keyimport.HkpKeyServer.java

@Override
public String get(String keyIdHex) throws QueryException {
    HttpClient client = new DefaultHttpClient();
    try {/*from  w ww .  j a v a 2 s  . c  o  m*/
        String query = "http://" + mHost + ":" + mPort + "/pks/lookup?op=get&options=mr&search=" + keyIdHex;
        Log.d(Constants.TAG, "hkp keyserver get: " + query);
        HttpGet get = new HttpGet(query);
        HttpResponse response = client.execute(get);
        if (response.getStatusLine().getStatusCode() != HttpStatus.SC_OK) {
            throw new QueryException("not found");
        }

        HttpEntity entity = response.getEntity();
        InputStream is = entity.getContent();
        String data = readAll(is, EntityUtils.getContentCharSet(entity));
        Matcher matcher = PgpHelper.PGP_PUBLIC_KEY.matcher(data);
        if (matcher.find()) {
            return matcher.group(1);
        }
    } catch (IOException e) {
        // nothing to do, better luck on the next keyserver
    } finally {
        client.getConnectionManager().shutdown();
    }

    return null;
}