Example usage for org.apache.http.protocol HTTP DEFAULT_CONTENT_CHARSET

List of usage examples for org.apache.http.protocol HTTP DEFAULT_CONTENT_CHARSET

Introduction

In this page you can find the example usage for org.apache.http.protocol HTTP DEFAULT_CONTENT_CHARSET.

Prototype

String DEFAULT_CONTENT_CHARSET

To view the source code for org.apache.http.protocol HTTP DEFAULT_CONTENT_CHARSET.

Click 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));
    }/*from   w w  w.  j a  va2s .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:au.com.wallaceit.reddinator.RedditData.java

private boolean createHttpClient() {
    // Initialize client
    HttpParams params = new BasicHttpParams();
    HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1);
    HttpProtocolParams.setContentCharset(params, HTTP.DEFAULT_CONTENT_CHARSET);
    HttpProtocolParams.setUseExpectContinue(params, true);
    HttpConnectionParams.setConnectionTimeout(params, 12000);
    HttpConnectionParams.setSoTimeout(params, 10000);
    SchemeRegistry schReg = new SchemeRegistry();
    schReg.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80));
    schReg.register(new Scheme("https", org.apache.http.conn.ssl.SSLSocketFactory.getSocketFactory(), 443));
    ClientConnectionManager conMgr = new ThreadSafeClientConnManager(params, schReg);
    //cookieStore = new BasicCookieStore();
    httpclient = new DefaultHttpClient(conMgr, params);
    httpclient.getParams().setParameter(CoreProtocolPNames.USER_AGENT, userAgent);
    //httpclient.setCookieStore(cookieStore);
    return true;//ww w  .j  av a 2  s .c o m
}

From source file:com.navercorp.pinpoint.profiler.modifier.connector.httpclient4.interceptor.AbstractHttpRequestExecute.java

/**
 * copy: EntityUtils/*from w w  w  . j a v  a2 s.c  om*/
 * Get the entity content as a String, using the provided default character set
 * if none is found in the entity.
 * If defaultCharset is null, the default "ISO-8859-1" is used.
 *
 * @param entity must not be null
 * @param defaultCharset character set to be applied if none found in the entity
 * @return the entity content as a String. May be null if
 *   {@link HttpEntity#getContent()} is null.
 * @throws ParseException if header elements cannot be parsed
 * @throws IllegalArgumentException if entity is null or if content length > Integer.MAX_VALUE
 * @throws IOException if an error occurs reading the input stream
 */
public static String entityUtilsToString(final HttpEntity entity, final String defaultCharset, int maxLength)
        throws IOException, ParseException {
    if (entity == null) {
        throw new IllegalArgumentException("HTTP entity may not be null");
    }
    final InputStream instream = entity.getContent();
    if (instream == null) {
        return null;
    }
    try {
        if (entity.getContentLength() > Integer.MAX_VALUE) {
            return "HTTP entity too large to be buffered in memory length:" + entity.getContentLength();
        }
        String charset = getContentCharSet(entity);
        if (charset == null) {
            charset = defaultCharset;
        }
        if (charset == null) {
            charset = HTTP.DEFAULT_CONTENT_CHARSET;
        }
        Reader reader = new InputStreamReader(instream, charset);
        final StringBuilder buffer = new StringBuilder(maxLength * 2);
        char[] tmp = new char[1024];
        int l;
        while ((l = reader.read(tmp)) != -1) {
            buffer.append(tmp, 0, l);
            if (buffer.length() >= maxLength) {
                break;
            }
        }
        return buffer.toString();
    } finally {
        instream.close();
    }
}

From source file:com.yibu.kuaibu.app.glApplication.java

private HttpClient createHttpClient() {
    HttpParams params = new BasicHttpParams();
    HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1);
    HttpProtocolParams.setContentCharset(params, HTTP.DEFAULT_CONTENT_CHARSET);
    HttpProtocolParams.setUseExpectContinue(params, true);
    HttpConnectionParams.setConnectionTimeout(params, 20 * 1000);
    HttpConnectionParams.setSoTimeout(params, 20 * 1000);
    HttpConnectionParams.setSocketBufferSize(params, 8192);
    SchemeRegistry schReg = new SchemeRegistry();
    schReg.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80));
    schReg.register(new Scheme("https", SSLSocketFactory.getSocketFactory(), 443));

    ClientConnectionManager connMgr = new ThreadSafeClientConnManager(params, schReg);

    return new DefaultHttpClient(connMgr, params);
}

From source file:org.wso2.cdm.agent.proxy.ServerApiAccess.java

public static String getResponseBodyContent(final HttpEntity entity) throws IOException, ParseException {

    if (entity == null) {
        throw new IllegalArgumentException("HTTP entity may not be null");
    }//from  w w w. j a  va 2  s. c om

    InputStream instream = entity.getContent();

    if (instream == null) {
        return "";
    }

    if (entity.getContentLength() > Integer.MAX_VALUE) {
        throw new IllegalArgumentException(

                "HTTP entity too large to be buffered in memory");
    }

    String charset = getContentCharSet(entity);

    if (charset == null) {

        charset = HTTP.DEFAULT_CONTENT_CHARSET;

    }

    Reader reader = new InputStreamReader(instream, charset);

    StringBuilder buffer = new StringBuilder();

    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.ametro.app.ApplicationEx.java

private HttpClient createHttpClient() {
    if (Log.isLoggable(Constants.LOG_TAG_MAIN, Log.DEBUG)) {
        Log.d(Constants.LOG_TAG_MAIN, "Create HTTP client");
    }//from   w w w  .  ja v a 2  s . c o  m
    HttpParams params = new BasicHttpParams();
    HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1);
    HttpProtocolParams.setContentCharset(params, HTTP.DEFAULT_CONTENT_CHARSET);
    HttpProtocolParams.setUseExpectContinue(params, true);
    HttpConnectionParams.setConnectionTimeout(params, HTTP_CONNECTION_TIMEOUT);
    HttpConnectionParams.setSoTimeout(params, HTTP_SOCKET_TIMEOUT);

    SchemeRegistry schReg = new SchemeRegistry();
    schReg.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80));
    schReg.register(new Scheme("https", SSLSocketFactory.getSocketFactory(), 443));
    ClientConnectionManager conMgr = new ThreadSafeClientConnManager(params, schReg);
    return new DefaultHttpClient(conMgr, params);
}

From source file:com.sayar.requests.impl.HttpClientRequestHandler.java

protected HttpClient getHttpClient(final RequestArguments args) {
    // The HttpClient is heavy and should be instantiated once per app.
    // It holds a thread pool for requests and is threadsafe.

    if (HttpClientRequestHandler.HttpClient == null) {
        // Reference:
        // http://na.isobar.com/2011/best-way-to-use-httpclient-in-android/
        final SchemeRegistry registry = new SchemeRegistry();
        registry.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80));
        // TODO: WE SHOULD BE THROWING AN EXCEMPTION IF CONTEXT IS NULL
        registry.register(new Scheme("https", this.getHttpsSocketFactory(args.getContext()), 443));

        final HttpParams basicparms = new BasicHttpParams();
        HttpProtocolParams.setContentCharset(basicparms, HTTP.DEFAULT_CONTENT_CHARSET);
        HttpProtocolParams.setHttpElementCharset(basicparms, HTTP.DEFAULT_CONTENT_CHARSET);
        HttpProtocolParams.setVersion(basicparms, args.getHttpVersion());
        HttpProtocolParams.setUseExpectContinue(basicparms, true);
        HttpProtocolParams.setUserAgent(basicparms, args.getUserAgent());

        // TODO: Need sensible defaults.
        HttpConnectionParams.setLinger(basicparms, -1);
        // HttpConnectionParams.setSocketBufferSize(basicparms, ???);
        // HttpConnectionParams.setStaleCheckingEnabled(basicparms, false);
        HttpConnectionParams.setConnectionTimeout(basicparms, HttpClientRequestHandler.CONNECTION_TIMEOUT);
        HttpConnectionParams.setSoTimeout(basicparms, HttpClientRequestHandler.SO_TIMEOUT);
        // HttpConnectionParams.setTcpNoDelay(basicparms, true);

        final ClientConnectionManager cm = new ThreadSafeClientConnManager(basicparms, registry);

        HttpClientRequestHandler.HttpClient = new DefaultHttpClient(cm, basicparms);
    }/* ww  w  .  ja  va2s.co m*/
    return HttpClientRequestHandler.HttpClient;
}

From source file:org.wso2.mdm.agent.proxy.utils.ServerUtilities.java

public static String getResponseBodyContent(final HttpEntity entity) throws IOException, ParseException {

    InputStream instream = entity.getContent();

    if (entity.getContentLength() > Integer.MAX_VALUE) {
        throw new IllegalArgumentException("HTTP entity too large to be buffered in memory.");
    }//  w  ww .  j av a 2  s  .  co  m

    String charset = getContentCharSet(entity);

    if (charset == null) {
        charset = HTTP.DEFAULT_CONTENT_CHARSET;
    }

    Reader reader = new InputStreamReader(instream, charset);
    StringBuilder buffer = new StringBuilder();

    try {
        char[] bufferSize = new char[1024];
        int length;

        while ((length = reader.read(bufferSize)) != -1) {
            buffer.append(bufferSize, 0, length);
        }
    } finally {
        reader.close();
    }

    return buffer.toString();

}

From source file:com.calebgomer.roadkill_reporter.AsyncReporter.java

private static String _getResponseBody(final HttpEntity entity) throws IOException, ParseException {
    if (entity == null) {
        throw new IllegalArgumentException("HTTP entity may not be null");
    }/*from   w w  w  .  java  2  s. c  o  m*/
    InputStream instream = entity.getContent();
    if (instream == null) {
        return "";
    }
    if (entity.getContentLength() > Integer.MAX_VALUE) {
        throw new IllegalArgumentException("HTTP entity too large to be buffered in memory");
    }
    String charset = getContentCharSet(entity);
    if (charset == null) {
        charset = HTTP.DEFAULT_CONTENT_CHARSET;
    }
    Reader reader = new InputStreamReader(instream, charset);
    StringBuilder buffer = new StringBuilder();
    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:com.navercorp.pinpoint.plugin.httpclient4.interceptor.DefaultClientExchangeHandlerImplStartMethodInterceptor.java

/**
 * copy: EntityUtils Get the entity content as a String, using the provided default character set if none is found in the entity. If defaultCharset is null, the default "ISO-8859-1" is used.
 *
 * @param entity//from  ww  w  .j av a 2s  . c o m
 *            must not be null
 * @param defaultCharset
 *            character set to be applied if none found in the entity
 * @return the entity content as a String. May be null if {@link HttpEntity#getContent()} is null.
 * @throws ParseException
 *             if header elements cannot be parsed
 * @throws IllegalArgumentException
 *             if entity is null or if content length > Integer.MAX_VALUE
 * @throws IOException
 *             if an error occurs reading the input stream
 */
@SuppressWarnings("deprecation")
public static String entityUtilsToString(final HttpEntity entity, final String defaultCharset, int maxLength)
        throws Exception {
    if (entity == null) {
        throw new IllegalArgumentException("HTTP entity may not be null");
    }
    if (entity.getContentLength() > Integer.MAX_VALUE) {
        return "HTTP entity is too large to be buffered in memory length:" + entity.getContentLength();
    }
    if (entity.getContentType().getValue().startsWith("multipart/form-data")) {
        return "content type is multipart/form-data. content length:" + entity.getContentLength();
    }

    String charset = getContentCharSet(entity);

    if (charset == null) {
        charset = defaultCharset;
    }
    if (charset == null) {
        charset = HTTP.DEFAULT_CONTENT_CHARSET;
    }

    FixedByteArrayOutputStream outStream = new FixedByteArrayOutputStream(maxLength);
    entity.writeTo(outStream);

    String entityValue = outStream.toString(charset);

    if (entity.getContentLength() > maxLength) {
        StringBuilder sb = new StringBuilder();
        sb.append(entityValue);
        sb.append("HTTP entity large length: ");
        sb.append(entity.getContentLength());
        sb.append(" )");
        return sb.toString();
    }

    return entityValue;
}