Search in sources :

Example 11 with BasicHttpParams

use of org.apache.http.params.BasicHttpParams in project openhab1-addons by openhab.

the class StreamClientImpl method getRequestParams.

protected HttpParams getRequestParams(StreamRequestMessage requestMessage) {
    HttpParams localParams = new BasicHttpParams();
    localParams.setParameter(CoreProtocolPNames.PROTOCOL_VERSION, requestMessage.getOperation().getHttpMinorVersion() == 0 ? HttpVersion.HTTP_1_0 : HttpVersion.HTTP_1_1);
    // DefaultHttpClient adds HOST header automatically in its default processor
    // Let's add the user-agent header on every request
    HttpProtocolParams.setUserAgent(localParams, getConfiguration().getUserAgentValue(requestMessage.getUdaMajorVersion(), requestMessage.getUdaMinorVersion()));
    return new DefaultedHttpParams(localParams, globalParams);
}
Also used : BasicHttpParams(org.apache.http.params.BasicHttpParams) DefaultedHttpParams(org.apache.http.params.DefaultedHttpParams) HttpParams(org.apache.http.params.HttpParams) BasicHttpParams(org.apache.http.params.BasicHttpParams) DefaultedHttpParams(org.apache.http.params.DefaultedHttpParams)

Example 12 with BasicHttpParams

use of org.apache.http.params.BasicHttpParams in project XobotOS by xamarin.

the class SSLConnectionClosedByUserException method openConnection.

/**
     * Opens the connection to a http server or proxy.
     *
     * @return the opened low level connection
     * @throws IOException if the connection fails for any reason.
     */
@Override
AndroidHttpClientConnection openConnection(Request req) throws IOException {
    SSLSocket sslSock = null;
    if (mProxyHost != null) {
        // If we have a proxy set, we first send a CONNECT request
        // to the proxy; if the proxy returns 200 OK, we negotiate
        // a secure connection to the target server via the proxy.
        // If the request fails, we drop it, but provide the event
        // handler with the response status and headers. The event
        // handler is then responsible for cancelling the load or
        // issueing a new request.
        AndroidHttpClientConnection proxyConnection = null;
        Socket proxySock = null;
        try {
            proxySock = new Socket(mProxyHost.getHostName(), mProxyHost.getPort());
            proxySock.setSoTimeout(60 * 1000);
            proxyConnection = new AndroidHttpClientConnection();
            HttpParams params = new BasicHttpParams();
            HttpConnectionParams.setSocketBufferSize(params, 8192);
            proxyConnection.bind(proxySock, params);
        } catch (IOException e) {
            if (proxyConnection != null) {
                proxyConnection.close();
            }
            String errorMessage = e.getMessage();
            if (errorMessage == null) {
                errorMessage = "failed to establish a connection to the proxy";
            }
            throw new IOException(errorMessage);
        }
        StatusLine statusLine = null;
        int statusCode = 0;
        Headers headers = new Headers();
        try {
            BasicHttpRequest proxyReq = new BasicHttpRequest("CONNECT", mHost.toHostString());
            // 400 Bad Request
            for (Header h : req.mHttpRequest.getAllHeaders()) {
                String headerName = h.getName().toLowerCase();
                if (headerName.startsWith("proxy") || headerName.equals("keep-alive") || headerName.equals("host")) {
                    proxyReq.addHeader(h);
                }
            }
            proxyConnection.sendRequestHeader(proxyReq);
            proxyConnection.flush();
            // a loop is a standard way of dealing with them
            do {
                statusLine = proxyConnection.parseResponseHeader(headers);
                statusCode = statusLine.getStatusCode();
            } while (statusCode < HttpStatus.SC_OK);
        } catch (ParseException e) {
            String errorMessage = e.getMessage();
            if (errorMessage == null) {
                errorMessage = "failed to send a CONNECT request";
            }
            throw new IOException(errorMessage);
        } catch (HttpException e) {
            String errorMessage = e.getMessage();
            if (errorMessage == null) {
                errorMessage = "failed to send a CONNECT request";
            }
            throw new IOException(errorMessage);
        } catch (IOException e) {
            String errorMessage = e.getMessage();
            if (errorMessage == null) {
                errorMessage = "failed to send a CONNECT request";
            }
            throw new IOException(errorMessage);
        }
        if (statusCode == HttpStatus.SC_OK) {
            try {
                sslSock = (SSLSocket) getSocketFactory().createSocket(proxySock, mHost.getHostName(), mHost.getPort(), true);
            } catch (IOException e) {
                if (sslSock != null) {
                    sslSock.close();
                }
                String errorMessage = e.getMessage();
                if (errorMessage == null) {
                    errorMessage = "failed to create an SSL socket";
                }
                throw new IOException(errorMessage);
            }
        } else {
            // if the code is not OK, inform the event handler
            ProtocolVersion version = statusLine.getProtocolVersion();
            req.mEventHandler.status(version.getMajor(), version.getMinor(), statusCode, statusLine.getReasonPhrase());
            req.mEventHandler.headers(headers);
            req.mEventHandler.endData();
            proxyConnection.close();
            // request needs to be dropped
            return null;
        }
    } else {
        // if we do not have a proxy, we simply connect to the host
        try {
            sslSock = (SSLSocket) getSocketFactory().createSocket(mHost.getHostName(), mHost.getPort());
            sslSock.setSoTimeout(SOCKET_TIMEOUT);
        } catch (IOException e) {
            if (sslSock != null) {
                sslSock.close();
            }
            String errorMessage = e.getMessage();
            if (errorMessage == null) {
                errorMessage = "failed to create an SSL socket";
            }
            throw new IOException(errorMessage);
        }
    }
    // do handshake and validate server certificates
    SslError error = CertificateChainValidator.getInstance().doHandshakeAndValidateServerCertificates(this, sslSock, mHost.getHostName());
    // Inform the user if there is a problem
    if (error != null) {
        // need to.
        synchronized (mSuspendLock) {
            mSuspended = true;
        }
        // don't hold the lock while calling out to the event handler
        boolean canHandle = req.getEventHandler().handleSslErrorRequest(error);
        if (!canHandle) {
            throw new IOException("failed to handle " + error);
        }
        synchronized (mSuspendLock) {
            if (mSuspended) {
                try {
                    // Put a limit on how long we are waiting; if the timeout
                    // expires (which should never happen unless you choose
                    // to ignore the SSL error dialog for a very long time),
                    // we wake up the thread and abort the request. This is
                    // to prevent us from stalling the network if things go
                    // very bad.
                    mSuspendLock.wait(10 * 60 * 1000);
                    if (mSuspended) {
                        // mSuspended is true if we have not had a chance to
                        // restart the connection yet (ie, the wait timeout
                        // has expired)
                        mSuspended = false;
                        mAborted = true;
                        if (HttpLog.LOGV) {
                            HttpLog.v("HttpsConnection.openConnection():" + " SSL timeout expired and request was cancelled!!!");
                        }
                    }
                } catch (InterruptedException e) {
                // ignore
                }
            }
            if (mAborted) {
                // The user decided not to use this unverified connection
                // so close it immediately.
                sslSock.close();
                throw new SSLConnectionClosedByUserException("connection closed by the user");
            }
        }
    }
    // All went well, we have an open, verified connection.
    AndroidHttpClientConnection conn = new AndroidHttpClientConnection();
    BasicHttpParams params = new BasicHttpParams();
    params.setIntParameter(HttpConnectionParams.SOCKET_BUFFER_SIZE, 8192);
    conn.bind(sslSock, params);
    return conn;
}
Also used : SSLSocket(javax.net.ssl.SSLSocket) IOException(java.io.IOException) ProtocolVersion(org.apache.http.ProtocolVersion) BasicHttpRequest(org.apache.http.message.BasicHttpRequest) StatusLine(org.apache.http.StatusLine) BasicHttpParams(org.apache.http.params.BasicHttpParams) HttpParams(org.apache.http.params.HttpParams) Header(org.apache.http.Header) HttpException(org.apache.http.HttpException) ParseException(org.apache.http.ParseException) BasicHttpParams(org.apache.http.params.BasicHttpParams) Socket(java.net.Socket) SSLSocket(javax.net.ssl.SSLSocket)

Example 13 with BasicHttpParams

use of org.apache.http.params.BasicHttpParams in project robovm by robovm.

the class DefaultHttpClient method createHttpParams.

@Override
protected HttpParams createHttpParams() {
    HttpParams params = new BasicHttpParams();
    HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1);
    HttpProtocolParams.setContentCharset(params, HTTP.DEFAULT_CONTENT_CHARSET);
    /*
         * Android note: Send each request body without first asking the server
         * whether it will be accepted. Asking first slows down the common case
         * and results in "417 expectation failed" errors when a HTTP/1.0 server
         * is behind a proxy. http://b/2471595
         */
    HttpProtocolParams.setUseExpectContinue(params, // android-changed
    false);
    // determine the release version from packaged version info
    final VersionInfo vi = VersionInfo.loadVersionInfo("org.apache.http.client", getClass().getClassLoader());
    final String release = (vi != null) ? vi.getRelease() : VersionInfo.UNAVAILABLE;
    HttpProtocolParams.setUserAgent(params, "Apache-HttpClient/" + release + " (java 1.4)");
    return params;
}
Also used : VersionInfo(org.apache.http.util.VersionInfo) BasicHttpParams(org.apache.http.params.BasicHttpParams) HttpParams(org.apache.http.params.HttpParams) BasicHttpParams(org.apache.http.params.BasicHttpParams)

Example 14 with BasicHttpParams

use of org.apache.http.params.BasicHttpParams in project apps-android-commons by commons-app.

the class CommonsApplication method newHttpClient.

private AbstractHttpClient newHttpClient() {
    BasicHttpParams params = new BasicHttpParams();
    SchemeRegistry schemeRegistry = new SchemeRegistry();
    schemeRegistry.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80));
    final SSLSocketFactory sslSocketFactory = SSLSocketFactory.getSocketFactory();
    schemeRegistry.register(new Scheme("https", sslSocketFactory, 443));
    ClientConnectionManager cm = new ThreadSafeClientConnManager(params, schemeRegistry);
    params.setParameter(CoreProtocolPNames.USER_AGENT, "Commons/" + BuildConfig.VERSION_NAME + " (https://mediawiki.org/wiki/Apps/Commons) Android/" + Build.VERSION.RELEASE);
    return new DefaultHttpClient(cm, params);
}
Also used : Scheme(org.apache.http.conn.scheme.Scheme) ThreadSafeClientConnManager(org.apache.http.impl.conn.tsccm.ThreadSafeClientConnManager) SchemeRegistry(org.apache.http.conn.scheme.SchemeRegistry) BasicHttpParams(org.apache.http.params.BasicHttpParams) SSLSocketFactory(org.apache.http.conn.ssl.SSLSocketFactory) ClientConnectionManager(org.apache.http.conn.ClientConnectionManager) DefaultHttpClient(org.apache.http.impl.client.DefaultHttpClient)

Example 15 with BasicHttpParams

use of org.apache.http.params.BasicHttpParams in project openstack4j by ContainX.

the class ApacheHttpClientExecutor method create.

public static ApacheHttpClientExecutor create(Config config) {
    HttpParams params = new BasicHttpParams();
    if (config.getReadTimeout() > 0)
        HttpConnectionParams.setSoTimeout(params, config.getReadTimeout());
    if (config.getConnectTimeout() > 0)
        HttpConnectionParams.setConnectionTimeout(params, config.getConnectTimeout());
    HttpClient client = new DefaultHttpClient(params);
    if (config.getProxy() != null) {
        try {
            URL url = new URL(config.getProxy().getHost());
            HttpHost proxy = new HttpHost(url.getHost(), config.getProxy().getPort(), url.getProtocol());
            client.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY, proxy);
        } catch (MalformedURLException e) {
            e.printStackTrace();
        }
    }
    return new ApacheHttpClientExecutor(client);
}
Also used : BasicHttpParams(org.apache.http.params.BasicHttpParams) HttpParams(org.apache.http.params.HttpParams) MalformedURLException(java.net.MalformedURLException) HttpHost(org.apache.http.HttpHost) DefaultHttpClient(org.apache.http.impl.client.DefaultHttpClient) HttpClient(org.apache.http.client.HttpClient) BasicHttpParams(org.apache.http.params.BasicHttpParams) DefaultHttpClient(org.apache.http.impl.client.DefaultHttpClient) URL(java.net.URL)

Aggregations

BasicHttpParams (org.apache.http.params.BasicHttpParams)105 HttpParams (org.apache.http.params.HttpParams)75 DefaultHttpClient (org.apache.http.impl.client.DefaultHttpClient)50 IOException (java.io.IOException)41 HttpResponse (org.apache.http.HttpResponse)33 SchemeRegistry (org.apache.http.conn.scheme.SchemeRegistry)32 Scheme (org.apache.http.conn.scheme.Scheme)31 ThreadSafeClientConnManager (org.apache.http.impl.conn.tsccm.ThreadSafeClientConnManager)30 ClientConnectionManager (org.apache.http.conn.ClientConnectionManager)25 ClientProtocolException (org.apache.http.client.ClientProtocolException)16 HttpGet (org.apache.http.client.methods.HttpGet)16 HttpClient (org.apache.http.client.HttpClient)15 Test (org.junit.Test)15 HttpPost (org.apache.http.client.methods.HttpPost)14 SSLSocketFactory (org.apache.http.conn.ssl.SSLSocketFactory)14 Socket (java.net.Socket)11 URI (java.net.URI)11 StringEntity (org.apache.http.entity.StringEntity)11 TestHttpClient (io.undertow.testutils.TestHttpClient)10 KeyManagementException (java.security.KeyManagementException)9