Example usage for com.squareup.okhttp Protocol HTTP_1_1

List of usage examples for com.squareup.okhttp Protocol HTTP_1_1

Introduction

In this page you can find the example usage for com.squareup.okhttp Protocol HTTP_1_1.

Prototype

Protocol HTTP_1_1

To view the source code for com.squareup.okhttp Protocol HTTP_1_1.

Click Source Link

Document

A plaintext framing that includes persistent connections.

Usage

From source file:twitter4j.AlternativeHttpClientImpl.java

License:Apache License

private void prepareOkHttpClient() {
    if (okHttpClient == null) {
        okHttpClient = new OkHttpClient();

        //set protocols
        List<Protocol> protocols = new ArrayList<Protocol>();
        protocols.add(Protocol.HTTP_1_1);
        if (sPreferHttp2)
            protocols.add(Protocol.HTTP_2);
        if (sPreferSpdy)
            protocols.add(Protocol.SPDY_3);
        okHttpClient.setProtocols(protocols);

        //connectionPool setup
        okHttpClient.setConnectionPool(new ConnectionPool(MAX_CONNECTIONS, KEEP_ALIVE_DURATION_MS));

        //redirect disable
        okHttpClient.setFollowSslRedirects(false);

        //for proxy
        if (isProxyConfigured()) {
            if (CONF.getHttpProxyUser() != null && !CONF.getHttpProxyUser().equals("")) {
                if (logger.isDebugEnabled()) {
                    logger.debug("Proxy AuthUser: " + CONF.getHttpProxyUser());
                    logger.debug("Proxy AuthPassword: " + CONF.getHttpProxyPassword().replaceAll(".", "*"));
                }//from   w  w  w  .  ja  va2 s.c om
                Authenticator.setDefault(new Authenticator() {
                    @Override
                    protected PasswordAuthentication getPasswordAuthentication() {
                        if (getRequestorType().equals(RequestorType.PROXY)) {
                            return new PasswordAuthentication(CONF.getHttpProxyUser(),
                                    CONF.getHttpProxyPassword().toCharArray());
                        } else {
                            return null;
                        }
                    }
                });
            }
            final Proxy proxy = new Proxy(Proxy.Type.HTTP,
                    InetSocketAddress.createUnresolved(CONF.getHttpProxyHost(), CONF.getHttpProxyPort()));
            if (logger.isDebugEnabled()) {
                logger.debug("Opening proxied connection(" + CONF.getHttpProxyHost() + ":"
                        + CONF.getHttpProxyPort() + ")");
            }
            okHttpClient.setProxy(proxy);
        }

        //connection timeout
        if (CONF.getHttpConnectionTimeout() > 0) {
            okHttpClient.setConnectTimeout(CONF.getHttpConnectionTimeout(), TimeUnit.MILLISECONDS);
        }

        //read timeout
        if (CONF.getHttpReadTimeout() > 0) {
            okHttpClient.setReadTimeout(CONF.getHttpReadTimeout(), TimeUnit.MILLISECONDS);
        }
    }
}