Search in sources :

Example 86 with BasicHttpParams

use of org.apache.http.params.BasicHttpParams in project undertow by undertow-io.

the class AbstractHttpContinueServletTestCase method testHttpContinueAcceptedWithChunkedRequest.

// UNDERTOW-162
@Test
public void testHttpContinueAcceptedWithChunkedRequest() throws IOException {
    accept = true;
    String message = "My HTTP Request!";
    HttpParams httpParams = new BasicHttpParams();
    httpParams.setParameter("http.protocol.wait-for-continue", Integer.MAX_VALUE);
    TestHttpClient client = getClient();
    client.setParams(httpParams);
    try {
        HttpPost post = new HttpPost(getServerAddress() + "/servletContext/path");
        post.addHeader("Expect", "100-continue");
        post.setEntity(new StringEntity(message) {

            @Override
            public long getContentLength() {
                return -1;
            }
        });
        HttpResponse result = client.execute(post);
        Assert.assertEquals(StatusCodes.OK, result.getStatusLine().getStatusCode());
        Assert.assertEquals(message, HttpClientUtils.readResponse(result));
    } finally {
        client.getConnectionManager().shutdown();
    }
}
Also used : HttpPost(org.apache.http.client.methods.HttpPost) StringEntity(org.apache.http.entity.StringEntity) BasicHttpParams(org.apache.http.params.BasicHttpParams) HttpParams(org.apache.http.params.HttpParams) HttpResponse(org.apache.http.HttpResponse) BasicHttpParams(org.apache.http.params.BasicHttpParams) TestHttpClient(io.undertow.testutils.TestHttpClient) Test(org.junit.Test)

Example 87 with BasicHttpParams

use of org.apache.http.params.BasicHttpParams in project undertow by undertow-io.

the class AbstractHttpContinueServletTestCase method testEmptyHttpContinue.

@Test
public void testEmptyHttpContinue() throws IOException {
    accept = true;
    String message = "My HTTP Request!";
    HttpParams httpParams = new BasicHttpParams();
    httpParams.setParameter("http.protocol.wait-for-continue", Integer.MAX_VALUE);
    TestHttpClient client = getClient();
    client.setParams(httpParams);
    try {
        HttpGet post = new HttpGet(getServerAddress() + "/servletContext/ignore");
        post.addHeader("Expect", "100-continue");
        HttpResponse result = client.execute(post);
        Assert.assertEquals(StatusCodes.OK, result.getStatusLine().getStatusCode());
        Assert.assertEquals("", HttpClientUtils.readResponse(result));
    } finally {
        client.getConnectionManager().shutdown();
    }
}
Also used : BasicHttpParams(org.apache.http.params.BasicHttpParams) HttpParams(org.apache.http.params.HttpParams) HttpGet(org.apache.http.client.methods.HttpGet) HttpResponse(org.apache.http.HttpResponse) BasicHttpParams(org.apache.http.params.BasicHttpParams) TestHttpClient(io.undertow.testutils.TestHttpClient) Test(org.junit.Test)

Example 88 with BasicHttpParams

use of org.apache.http.params.BasicHttpParams in project baker-android by bakerframework.

the class AndroidHttpClient method newInstance.

/**
 * Create a new HttpClient with reasonable defaults (which you can update).
 *
 * @param userAgent to report in your HTTP requests
 * @param context to use for caching SSL sessions (may be null for no caching)
 * @return AndroidHttpClient for you to use for all your requests.
 */
public static AndroidHttpClient newInstance(String userAgent, Context context) {
    HttpParams params = new BasicHttpParams();
    // Turn off stale checking.  Our connections break all the time anyway,
    // and it's not worth it to pay the penalty of checking every time.
    HttpConnectionParams.setStaleCheckingEnabled(params, false);
    HttpConnectionParams.setConnectionTimeout(params, SOCKET_OPERATION_TIMEOUT);
    HttpConnectionParams.setSoTimeout(params, SOCKET_OPERATION_TIMEOUT);
    HttpConnectionParams.setSocketBufferSize(params, 8192);
    // Don't handle redirects -- return them to the caller.  Our code
    // often wants to re-POST after a redirect, which we must do ourselves.
    HttpClientParams.setRedirecting(params, false);
    Object sessionCache = null;
    // Use a session cache for SSL sockets -- Froyo only
    if (null != context && null != sSslSessionCacheClass) {
        Constructor<?> ct;
        try {
            ct = sSslSessionCacheClass.getConstructor(Context.class);
            sessionCache = ct.newInstance(context);
        } catch (SecurityException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (NoSuchMethodException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IllegalArgumentException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (InstantiationException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
    // Set the specified user agent and register standard protocols.
    HttpProtocolParams.setUserAgent(params, userAgent);
    SchemeRegistry schemeRegistry = new SchemeRegistry();
    schemeRegistry.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80));
    SocketFactory sslCertificateSocketFactory = null;
    if (null != sessionCache) {
        Method getHttpSocketFactoryMethod;
        try {
            getHttpSocketFactoryMethod = SSLCertificateSocketFactory.class.getDeclaredMethod("getHttpSocketFactory", Integer.TYPE, sSslSessionCacheClass);
            sslCertificateSocketFactory = (SocketFactory) getHttpSocketFactoryMethod.invoke(null, SOCKET_OPERATION_TIMEOUT, sessionCache);
        } catch (SecurityException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (NoSuchMethodException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IllegalArgumentException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
    if (null == sslCertificateSocketFactory) {
        sslCertificateSocketFactory = SSLSocketFactory.getSocketFactory();
    }
    schemeRegistry.register(new Scheme("https", sslCertificateSocketFactory, 443));
    ClientConnectionManager manager = new ThreadSafeClientConnManager(params, schemeRegistry);
    // parameters without the funny call-a-static-method dance.
    return new AndroidHttpClient(manager, params);
}
Also used : Context(android.content.Context) ClientContext(org.apache.http.client.protocol.ClientContext) BasicHttpContext(org.apache.http.protocol.BasicHttpContext) HttpContext(org.apache.http.protocol.HttpContext) Scheme(org.apache.http.conn.scheme.Scheme) PlainSocketFactory(org.apache.http.conn.scheme.PlainSocketFactory) SSLSocketFactory(org.apache.http.conn.ssl.SSLSocketFactory) SSLCertificateSocketFactory(android.net.SSLCertificateSocketFactory) SocketFactory(org.apache.http.conn.scheme.SocketFactory) Method(java.lang.reflect.Method) ClientConnectionManager(org.apache.http.conn.ClientConnectionManager) InvocationTargetException(java.lang.reflect.InvocationTargetException) BasicHttpParams(org.apache.http.params.BasicHttpParams) HttpParams(org.apache.http.params.HttpParams) ThreadSafeClientConnManager(org.apache.http.impl.conn.tsccm.ThreadSafeClientConnManager) SSLCertificateSocketFactory(android.net.SSLCertificateSocketFactory) SchemeRegistry(org.apache.http.conn.scheme.SchemeRegistry) BasicHttpParams(org.apache.http.params.BasicHttpParams)

Example 89 with BasicHttpParams

use of org.apache.http.params.BasicHttpParams in project Notes by lguipeng.

the class TEvernoteHttpClient method getHTTPClient.

@Deprecated
private DefaultHttpClient getHTTPClient() {
    try {
        if (mConnectionManager != null) {
            mConnectionManager.closeExpiredConnections();
            mConnectionManager.closeIdleConnections(1, TimeUnit.SECONDS);
        } else {
            BasicHttpParams params = new BasicHttpParams();
            HttpConnectionParams.setConnectionTimeout(params, 10000);
            HttpConnectionParams.setSoTimeout(params, 20000);
            ConnManagerParams.setMaxTotalConnections(params, ConnManagerParams.DEFAULT_MAX_TOTAL_CONNECTIONS);
            ConnManagerParams.setTimeout(params, 10000);
            // Giving 18 connections to Evernote
            ConnPerRouteBean connPerRoute = new ConnPerRouteBean(18);
            ConnManagerParams.setMaxConnectionsPerRoute(params, connPerRoute);
            SchemeRegistry schemeRegistry = new SchemeRegistry();
            schemeRegistry.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80));
            schemeRegistry.register(new Scheme("https", SSLSocketFactory.getSocketFactory(), 443));
            mConnectionManager = new ThreadSafeClientConnManager(params, schemeRegistry);
            DefaultHttpClient httpClient = new DefaultHttpClient(mConnectionManager, params);
            httpClient.setKeepAliveStrategy(new ConnectionKeepAliveStrategy() {

                @Override
                public long getKeepAliveDuration(HttpResponse response, HttpContext context) {
                    // 2 minutes in millis
                    return 2 * 60 * 1000;
                }
            });
            httpClient.setReuseStrategy(new ConnectionReuseStrategy() {

                @Override
                public boolean keepAlive(HttpResponse response, HttpContext context) {
                    return true;
                }
            });
            mHttpClient = httpClient;
        }
    } catch (Exception ex) {
        return null;
    }
    return mHttpClient;
}
Also used : Scheme(org.apache.http.conn.scheme.Scheme) ThreadSafeClientConnManager(org.apache.http.impl.conn.tsccm.ThreadSafeClientConnManager) ConnectionKeepAliveStrategy(org.apache.http.conn.ConnectionKeepAliveStrategy) SchemeRegistry(org.apache.http.conn.scheme.SchemeRegistry) HttpContext(org.apache.http.protocol.HttpContext) HttpResponse(org.apache.http.HttpResponse) BasicHttpParams(org.apache.http.params.BasicHttpParams) ConnPerRouteBean(org.apache.http.conn.params.ConnPerRouteBean) ConnectionReuseStrategy(org.apache.http.ConnectionReuseStrategy) DefaultHttpClient(org.apache.http.impl.client.DefaultHttpClient) IOException(java.io.IOException) TTransportException(com.evernote.thrift.transport.TTransportException)

Example 90 with BasicHttpParams

use of org.apache.http.params.BasicHttpParams in project iaf by ibissource.

the class WebServiceNtlmSender method configure.

@Override
public void configure() throws ConfigurationException {
    super.configure();
    HttpParams httpParameters = new BasicHttpParams();
    HttpConnectionParams.setConnectionTimeout(httpParameters, getTimeout());
    HttpConnectionParams.setSoTimeout(httpParameters, getTimeout());
    httpClient = new DefaultHttpClient(connectionManager, httpParameters);
    httpClient.getAuthSchemes().register("NTLM", new NTLMSchemeFactory());
    CredentialFactory cf = new CredentialFactory(getAuthAlias(), getUsername(), getPassword());
    httpClient.getCredentialsProvider().setCredentials(new AuthScope(AuthScope.ANY_HOST, AuthScope.ANY_PORT), new NTCredentials(cf.getUsername(), cf.getPassword(), Misc.getHostname(), getAuthDomain()));
    if (StringUtils.isNotEmpty(getProxyHost())) {
        HttpHost proxy = new HttpHost(getProxyHost(), getProxyPort());
        httpClient.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY, proxy);
    }
}
Also used : BasicHttpParams(org.apache.http.params.BasicHttpParams) HttpParams(org.apache.http.params.HttpParams) CredentialFactory(nl.nn.adapterframework.util.CredentialFactory) HttpHost(org.apache.http.HttpHost) AuthScope(org.apache.http.auth.AuthScope) BasicHttpParams(org.apache.http.params.BasicHttpParams) DefaultHttpClient(org.apache.http.impl.client.DefaultHttpClient) NTCredentials(org.apache.http.auth.NTCredentials)

Aggregations

BasicHttpParams (org.apache.http.params.BasicHttpParams)120 HttpParams (org.apache.http.params.HttpParams)75 IOException (java.io.IOException)55 DefaultHttpClient (org.apache.http.impl.client.DefaultHttpClient)51 SchemeRegistry (org.apache.http.conn.scheme.SchemeRegistry)34 Scheme (org.apache.http.conn.scheme.Scheme)33 HttpResponse (org.apache.http.HttpResponse)32 ThreadSafeClientConnManager (org.apache.http.impl.conn.tsccm.ThreadSafeClientConnManager)32 ClientConnectionManager (org.apache.http.conn.ClientConnectionManager)27 Test (org.junit.Test)27 URI (java.net.URI)24 ArrayList (java.util.ArrayList)20 HttpUriRequest (org.apache.http.client.methods.HttpUriRequest)19 HttpServletRequest (javax.servlet.http.HttpServletRequest)18 HttpServletResponse (javax.servlet.http.HttpServletResponse)18 CloseableHttpClient (org.apache.http.impl.client.CloseableHttpClient)18 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)17 FilterConfig (javax.servlet.FilterConfig)17 ServletContext (javax.servlet.ServletContext)17 HaDescriptor (org.apache.knox.gateway.ha.provider.HaDescriptor)17