Re: Proxy authentication + Basic authentication on the Client instance
Awesome, that did the trick. Thanks Oleg. On Sun, Dec 22, 2013 at 10:42 PM, Oleg Kalnichevski ol...@apache.orgwrote: On Sun, 2013-12-22 at 20:55 -1000, Arul Dhesiaseelan wrote: Hi, I am using a proxy to connect to a web service. Both proxy and web service are secured with different set of credentials. I am trying to perform a GET request using Apache HttpClient 4.3.1. URI proxyUri = URI.create(http://localhost:9000;); URI targetUri = URI.create(http://localhost:8080;); final HttpHost proxyHost = new HttpHost(proxyUri.getHost(), proxyUri.getPort(), proxyUri.getScheme()); final HttpHost targetHost = new HttpHost(targetUri.getHost(), targetUri.getPort(), targetUri.getScheme()); CredentialsProvider proxyCredentials = new BasicCredentialsProvider(); proxyCredentials.setCredentials( new AuthScope(proxyUri.getHost(), proxyUri.getPort()), new UsernamePasswordCredentials(joe, secret) ); CredentialsProvider serviceCredentials = new BasicCredentialsProvider(); serviceCredentials.setCredentials( new AuthScope(targetHost.getHostName(), targetHost.getPort()), new UsernamePasswordCredentials(user, password)); CloseableHttpClient httpclient = HttpClients.custom() .setDefaultCredentialsProvider(serviceCredentials).build();// this sets only service creds It looks like you can set only one set of credentials at any time. I could not set both on the client. I tried something like this, but it would not allow me to set proxy credentials on the request config. RequestConfig config = RequestConfig.custom().setProxy(proxyHost).build(); HttpGet httpGet = new HttpGet(/); httpGet.setConfig(config); System.out.println(executing request to + targetHost + via + proxyHost); CloseableHttpResponse response = httpclient.execute(targetHost, httpGet); How can I use both of these credentials on the client? Is this supported? Thanks! Arul Try this --- URI proxyUri = URI.create(http://localhost:9000;); URI targetUri = URI.create(http://localhost:8080;); HttpHost proxyHost = new HttpHost(proxyUri.getHost(), proxyUri.getPort(), proxyUri.getScheme()); HttpHost targetHost = new HttpHost(targetUri.getHost(), targetUri.getPort(), targetUri.getScheme()); CredentialsProvider credsProvider = new BasicCredentialsProvider(); credsProvider.setCredentials( new AuthScope(proxyUri.getHost(), proxyUri.getPort()), new UsernamePasswordCredentials(joe, secret) ); credsProvider.setCredentials( new AuthScope(targetHost.getHostName(), targetHost.getPort()), new UsernamePasswordCredentials(user, password)); CloseableHttpClient httpclient = HttpClients.custom() .setDefaultCredentialsProvider(credsProvider).build(); --- Oleg - To unsubscribe, e-mail: httpclient-users-unsubscr...@hc.apache.org For additional commands, e-mail: httpclient-users-h...@hc.apache.org
Proxy authentication + Basic authentication on the Client instance
Hi, I am using a proxy to connect to a web service. Both proxy and web service are secured with different set of credentials. I am trying to perform a GET request using Apache HttpClient 4.3.1. URI proxyUri = URI.create(http://localhost:9000;); URI targetUri = URI.create(http://localhost:8080;); final HttpHost proxyHost = new HttpHost(proxyUri.getHost(), proxyUri.getPort(), proxyUri.getScheme()); final HttpHost targetHost = new HttpHost(targetUri.getHost(), targetUri.getPort(), targetUri.getScheme()); CredentialsProvider proxyCredentials = new BasicCredentialsProvider(); proxyCredentials.setCredentials( new AuthScope(proxyUri.getHost(), proxyUri.getPort()), new UsernamePasswordCredentials(joe, secret) ); CredentialsProvider serviceCredentials = new BasicCredentialsProvider(); serviceCredentials.setCredentials( new AuthScope(targetHost.getHostName(), targetHost.getPort()), new UsernamePasswordCredentials(user, password)); CloseableHttpClient httpclient = HttpClients.custom() .setDefaultCredentialsProvider(serviceCredentials).build();// this sets only service creds It looks like you can set only one set of credentials at any time. I could not set both on the client. I tried something like this, but it would not allow me to set proxy credentials on the request config. RequestConfig config = RequestConfig.custom().setProxy(proxyHost).build(); HttpGet httpGet = new HttpGet(/); httpGet.setConfig(config); System.out.println(executing request to + targetHost + via + proxyHost); CloseableHttpResponse response = httpclient.execute(targetHost, httpGet); How can I use both of these credentials on the client? Is this supported? Thanks! Arul
Re: CoreConnectionPNames.MIN_CHUNK_LIMIT equivalent in 4.3.1
Ok cool. I was thinking of configuring it at runtime, but I think it may not be needed. Thanks! ARul On Sun, Dec 1, 2013 at 11:39 PM, Oleg Kalnichevski ol...@apache.org wrote: On Sun, 2013-12-01 at 13:14 -1000, ARul wrote: Hi Oleg, I was looking into BHttpConnectionBase#createOutputStream method. To change the default chunk size (2048), I believe the only way is to implement a custom http client connection and override createOuptputStream(). Here is what I have so far. public static class CustomManagedHttpClientConnection extends DefaultManagedHttpClientConnection { private final int chunkSize; public CustomManagedHttpClientConnection(final String id, final int buffersize, final int chunkSize) { super(id, buffersize); this.chunkSize = chunkSize; } @Override protected OutputStream createOutputStream(long len, SessionOutputBuffer outbuffer) { if (len == ContentLengthStrategy.CHUNKED) { return new ChunkedOutputStream(chunkSize, outbuffer); } else if (len == ContentLengthStrategy.IDENTITY) { return new IdentityOutputStream(outbuffer); } else { return new ContentLengthOutputStream(outbuffer, len); } } } public static class CustomManagedHttpClientConnectionFactory extends ManagedHttpClientConnectionFactory { private final int chunkSize; public CustomManagedHttpClientConnectionFactory(int chunkSize) { this.chunkSize = chunkSize; } @Override public ManagedHttpClientConnection create(HttpRoute route, ConnectionConfig config) { final String id = http-outgoing- + Long.toString(COUNTER.getAndIncrement()); return new CustomManagedHttpClientConnection(id, config.getBufferSize(), chunkSize); } } Usage: int chunkSize = 1024; HttpConnectionFactoryHttpRoute, ManagedHttpClientConnection connFactory = new CustomManagedHttpClientConnectionFactory(chunkSize); PoolingHttpClientConnectionManager connManager = new PoolingHttpClientConnectionManager(connFactory); CloseableHttpClient client = HttpClients.createMinimal(connManager); Do you recommend a better approach? I cannot suggest anything better. Is there a way to configure this per-request instead of client? Why would you want to do that in the first place? Oleg - To unsubscribe, e-mail: httpclient-users-unsubscr...@hc.apache.org For additional commands, e-mail: httpclient-users-h...@hc.apache.org
Re: CoreConnectionPNames.MIN_CHUNK_LIMIT equivalent in 4.3.1
Hi Oleg, I was looking into BHttpConnectionBase#createOutputStream method. To change the default chunk size (2048), I believe the only way is to implement a custom http client connection and override createOuptputStream(). Here is what I have so far. public static class CustomManagedHttpClientConnection extends DefaultManagedHttpClientConnection { private final int chunkSize; public CustomManagedHttpClientConnection(final String id, final int buffersize, final int chunkSize) { super(id, buffersize); this.chunkSize = chunkSize; } @Override protected OutputStream createOutputStream(long len, SessionOutputBuffer outbuffer) { if (len == ContentLengthStrategy.CHUNKED) { return new ChunkedOutputStream(chunkSize, outbuffer); } else if (len == ContentLengthStrategy.IDENTITY) { return new IdentityOutputStream(outbuffer); } else { return new ContentLengthOutputStream(outbuffer, len); } } } public static class CustomManagedHttpClientConnectionFactory extends ManagedHttpClientConnectionFactory { private final int chunkSize; public CustomManagedHttpClientConnectionFactory(int chunkSize) { this.chunkSize = chunkSize; } @Override public ManagedHttpClientConnection create(HttpRoute route, ConnectionConfig config) { final String id = http-outgoing- + Long.toString(COUNTER.getAndIncrement()); return new CustomManagedHttpClientConnection(id, config.getBufferSize(), chunkSize); } } Usage: int chunkSize = 1024; HttpConnectionFactoryHttpRoute, ManagedHttpClientConnection connFactory = new CustomManagedHttpClientConnectionFactory(chunkSize); PoolingHttpClientConnectionManager connManager = new PoolingHttpClientConnectionManager(connFactory); CloseableHttpClient client = HttpClients.createMinimal(connManager); Do you recommend a better approach? Is there a way to configure this per-request instead of client? - ARul On Wed, Nov 13, 2013 at 8:29 AM, Arul Dhesiaseelan ar...@acm.org wrote: Thanks Oleg for clearing this up. On Wed, Nov 13, 2013 at 7:06 AM, Oleg Kalnichevski ol...@apache.orgwrote: On Wed, 2013-11-13 at 06:54 -1000, Arul Dhesiaseelan wrote: Using chunked encoding to write the entity. So, this property is not intended to control the chunk size of the entity? No, it is not. I admit that javadocs may be unclear about it but I did try to describe its purpose to my best abilities. If you want to change the default chunk size from 2048 to something else you should be looking at the BHttpConnectionBase#createOutputStream method. Oleg - Arul On Wed, Nov 13, 2013 at 6:43 AM, Oleg Kalnichevski ol...@apache.org wrote: On Wed, 2013-11-13 at 06:32 -1000, Arul Dhesiaseelan wrote: Hi Oleg, Using it to control the chunk size on the Client, which can be configurable. Chunks of what? MIN_CHUNK_LIMIT has nothing to do with chunk coding. Misleading name was one of the reasons why this parameter was discontinued. Oleg - Arul On Wed, Nov 13, 2013 at 5:40 AM, Oleg Kalnichevski ol...@apache.org wrote: On Tue, 2013-11-12 at 18:49 -1000, Arul Dhesiaseelan wrote: Hi, CoreConnectionPNames is deprecated. I could not find MIN_CHUNK_LIMIT configuration in 4.3.1. The closest I can see is ConnectionConfig.bufferSize. Not sure if they are the same. Any idea? Arul There is no direct equivalent of MIN_CHUNK_LIMIT in 4.3. What are you using this parameter for? Oleg - To unsubscribe, e-mail: httpclient-users-unsubscr...@hc.apache.org For additional commands, e-mail: httpclient-users-h...@hc.apache.org - To unsubscribe, e-mail: httpclient-users-unsubscr...@hc.apache.org For additional commands, e-mail: httpclient-users-h...@hc.apache.org - To unsubscribe, e-mail: httpclient-users-unsubscr...@hc.apache.org For additional commands, e-mail: httpclient-users-h...@hc.apache.org
Re: how to use CookieSpecs
Hi Mike, This should do the trick. RequestConfig config = RequestConfig.custom().setCookieSpec(CookieSpecs.IGNORE_COOKIES).build(); HttpUriRequest request = RequestBuilder.get().setUri(url).setConfig(config).build(); - Arul On Sun, Nov 24, 2013 at 1:41 PM, Mike Wertheim m...@hyperreal.org wrote: I'm upgrading to from version 4.2.5 of HttpClient to version 4.3.1. My code has these 2 lines: final Request request = Request.Get(url); request.config(ClientPNames.COOKIE_POLICY, CookiePolicy.IGNORE_COOKIES); Since request.config is now deprecated, what should I replace that second line with? I see that version 4.3 provides a new class called org.apache.http.client.config.CookieSpecs. The class's javadoc indicates that this is probably the right class to use, but I don't see any documentation that describes how to use it or examples that show how to use it.
Re: CoreConnectionPNames.MIN_CHUNK_LIMIT equivalent in 4.3.1
Hi Oleg, Using it to control the chunk size on the Client, which can be configurable. - Arul On Wed, Nov 13, 2013 at 5:40 AM, Oleg Kalnichevski ol...@apache.org wrote: On Tue, 2013-11-12 at 18:49 -1000, Arul Dhesiaseelan wrote: Hi, CoreConnectionPNames is deprecated. I could not find MIN_CHUNK_LIMIT configuration in 4.3.1. The closest I can see is ConnectionConfig.bufferSize. Not sure if they are the same. Any idea? Arul There is no direct equivalent of MIN_CHUNK_LIMIT in 4.3. What are you using this parameter for? Oleg - To unsubscribe, e-mail: httpclient-users-unsubscr...@hc.apache.org For additional commands, e-mail: httpclient-users-h...@hc.apache.org
Re: CoreConnectionPNames.MIN_CHUNK_LIMIT equivalent in 4.3.1
Using chunked encoding to write the entity. So, this property is not intended to control the chunk size of the entity? - Arul On Wed, Nov 13, 2013 at 6:43 AM, Oleg Kalnichevski ol...@apache.org wrote: On Wed, 2013-11-13 at 06:32 -1000, Arul Dhesiaseelan wrote: Hi Oleg, Using it to control the chunk size on the Client, which can be configurable. Chunks of what? MIN_CHUNK_LIMIT has nothing to do with chunk coding. Misleading name was one of the reasons why this parameter was discontinued. Oleg - Arul On Wed, Nov 13, 2013 at 5:40 AM, Oleg Kalnichevski ol...@apache.org wrote: On Tue, 2013-11-12 at 18:49 -1000, Arul Dhesiaseelan wrote: Hi, CoreConnectionPNames is deprecated. I could not find MIN_CHUNK_LIMIT configuration in 4.3.1. The closest I can see is ConnectionConfig.bufferSize. Not sure if they are the same. Any idea? Arul There is no direct equivalent of MIN_CHUNK_LIMIT in 4.3. What are you using this parameter for? Oleg - To unsubscribe, e-mail: httpclient-users-unsubscr...@hc.apache.org For additional commands, e-mail: httpclient-users-h...@hc.apache.org - To unsubscribe, e-mail: httpclient-users-unsubscr...@hc.apache.org For additional commands, e-mail: httpclient-users-h...@hc.apache.org
Re: CoreConnectionPNames.MIN_CHUNK_LIMIT equivalent in 4.3.1
Thanks Oleg for clearing this up. On Wed, Nov 13, 2013 at 7:06 AM, Oleg Kalnichevski ol...@apache.org wrote: On Wed, 2013-11-13 at 06:54 -1000, Arul Dhesiaseelan wrote: Using chunked encoding to write the entity. So, this property is not intended to control the chunk size of the entity? No, it is not. I admit that javadocs may be unclear about it but I did try to describe its purpose to my best abilities. If you want to change the default chunk size from 2048 to something else you should be looking at the BHttpConnectionBase#createOutputStream method. Oleg - Arul On Wed, Nov 13, 2013 at 6:43 AM, Oleg Kalnichevski ol...@apache.org wrote: On Wed, 2013-11-13 at 06:32 -1000, Arul Dhesiaseelan wrote: Hi Oleg, Using it to control the chunk size on the Client, which can be configurable. Chunks of what? MIN_CHUNK_LIMIT has nothing to do with chunk coding. Misleading name was one of the reasons why this parameter was discontinued. Oleg - Arul On Wed, Nov 13, 2013 at 5:40 AM, Oleg Kalnichevski ol...@apache.org wrote: On Tue, 2013-11-12 at 18:49 -1000, Arul Dhesiaseelan wrote: Hi, CoreConnectionPNames is deprecated. I could not find MIN_CHUNK_LIMIT configuration in 4.3.1. The closest I can see is ConnectionConfig.bufferSize. Not sure if they are the same. Any idea? Arul There is no direct equivalent of MIN_CHUNK_LIMIT in 4.3. What are you using this parameter for? Oleg - To unsubscribe, e-mail: httpclient-users-unsubscr...@hc.apache.org For additional commands, e-mail: httpclient-users-h...@hc.apache.org - To unsubscribe, e-mail: httpclient-users-unsubscr...@hc.apache.org For additional commands, e-mail: httpclient-users-h...@hc.apache.org - To unsubscribe, e-mail: httpclient-users-unsubscr...@hc.apache.org For additional commands, e-mail: httpclient-users-h...@hc.apache.org
CoreConnectionPNames.MIN_CHUNK_LIMIT equivalent in 4.3.1
Hi, CoreConnectionPNames is deprecated. I could not find MIN_CHUNK_LIMIT configuration in 4.3.1. The closest I can see is ConnectionConfig.bufferSize. Not sure if they are the same. Any idea? Thanks, Arul
Request Redirect in 4.3.x
Hi, I have this code using 4.2.x APIs for controlling redirect at the request. request.getParams().setBooleanParameter(ClientPNames.HANDLE_REDIRECTS, false); This allows me to configure redirect per-request, instead of the global redirect client config. How do I implement this in 4.3.x as getParams() is deprecated? I have this code for the client: requestConfigBuilder.setRedirectsEnabled(true); RequestConfig requestConfig = requestConfigBuilder.build(); clientBuilder.setDefaultRequestConfig(requestConfig); clientBuilder.build(); I would like to override redirects at the request. Is this possible with the new APIs? Thanks, Arul
Re: Request Redirect in 4.3.x
Nevermind, I see the RequestBuilder which has an API to set the redirects on the RequestConfig. requestBuilder.setConfig(RequestConfig.custom().setRedirectsEnabled(false).build()); requestBuilder.setEntity(entity); requestBuilder.build() - Arul On Sat, Nov 9, 2013 at 4:58 PM, Arul Dhesiaseelan ar...@acm.org wrote: Hi, I have this code using 4.2.x APIs for controlling redirect at the request. request.getParams().setBooleanParameter(ClientPNames.HANDLE_REDIRECTS, false); This allows me to configure redirect per-request, instead of the global redirect client config. How do I implement this in 4.3.x as getParams() is deprecated? I have this code for the client: requestConfigBuilder.setRedirectsEnabled(true); RequestConfig requestConfig = requestConfigBuilder.build(); clientBuilder.setDefaultRequestConfig(requestConfig); clientBuilder.build(); I would like to override redirects at the request. Is this possible with the new APIs? Thanks, Arul
Re: HTTP URL results in SSLHandshake error
Thanks for the response. The fact that the siteminder token( for SSO) in the cookie is not getting accepted, the request gets forwarded to a login page(which is HTTPS) and that explains the SSL Handshake exception. If the problem is then because the reused connections retain some of the data from previous requests, do you know if there are any ways to clean up before making a new request? From: Oleg Kalnichevski ol...@apache.org To: HttpClient User Discussion httpclient-users@hc.apache.org Sent: Thursday, December 11, 2008 9:29:38 PM Subject: Re: HTTP URL results in SSLHandshake error On Thu, 2008-12-11 at 01:48 -0800, Arul Govindarajan wrote: Turns out that the problem is not with the SSL. Well, that can well be but 'java.security.cert.CertificateException: Certificate not Trusted' can be caused by a problem with the SSL context setup only. But, the authentication cookie stops getting accepted on the service side after a while (I couldnt set a pattern on how long it takes). The same cookie works if I send it from standalone java code. My suspicion is on the fact that connections are pooled. And I have a feeling that there is some residue of previous requests are retained in the connection objects and are causing this to fail. Is that possible scenario? If so, are there any ways to make sure the connection is completely cleaned up and does not have any carry over from one request to another, using the same connection. Any help will be really appreciated. Thanks, Arul Does the target server require client authentication? Oleg From: Arul Govindarajan arul_g...@yahoo.com To: httpclient-users@hc.apache.org Sent: Thursday, December 11, 2008 1:08:33 AM Subject: HTTP URL results in SSLHandshake error Hi, I am using HttpClient 3.1 for an application that calls a service. This application takes the cookie from the original request and passes it on to the service to enable (use) SSO. I am accessing the service thru HTTP (no SSL). However, I am running into this exception javax.net.ssl.SSLHandshakeException: java.security.cert.CertificateException: Certificate not Trusted I am not sure how a HTTP url goes into, seemingly, HTTPS. Any clues or workarounds to resolve this issue? Thanks, Arul Here is excerpts from my code... connectionManager = new MultiThreadedHttpConnectionManager(); connectionManager.getParams().setMaxTotalConnections(30); client = new HttpClient(connectionManager); GetMethod method = new GetMethod(url); method.setFollowRedirects(false); method.getParams().setParameter(HttpMethodParams.RETRY_HANDLER, new DefaultHttpMethodRetryHandler(0, false)); method.setRequestHeader(Cookie, cookie); try { long startTime = System.currentTimeMillis(); int statusCode = client.executeMethod(method); logger.info(method.getURI() + took + (System.currentTimeMillis() - startTime) + ms); if (statusCode != HttpStatus.SC_OK) { logger.error(Failed: + method.getStatusLine() + : + url); } String responseBody = method.getResponseBodyAsString(); if(statusCode == HttpStatus.SC_OK) { res = responseBody; } else { throw new Exception(String.valueOf(method.getStatusCode()) + method.getStatusText()); } } catch (HttpException he) { logger.error(Fatal error: + he.getMessage()); throw he; } catch (Exception e) { logger.error(Fatal error: + e.getMessage()); throw e; } finally { // Release the connection. method.releaseConnection(); } - To unsubscribe, e-mail: httpclient-users-unsubscr...@hc.apache.org For additional commands, e-mail: httpclient-users-h...@hc.apache.org