You are viewing a plain text version of this content. The canonical link for it is here.
Posted to httpclient-users@hc.apache.org by he...@dnbnor.no on 2008/09/26 14:04:41 UTC

RE: ResponseProcessCookies - CookieSpec not available inHTTPcontext

 


mvh
Hermod 
-----Original Message-----
From: Opstvedt, Hermod 
Sent: Friday, September 26, 2008 7:43 AM
To: 'HttpClient User Discussion'
Subject: RE: ResponseProcessCookies - CookieSpec not available
inHTTPcontext

Hi

On your last note: The content length is -1!?

Here is the sample code - Just run it:

package no.dnbnor.it01.test.httpclient;

import org.apache.http.HttpEntity;
import org.apache.http.HttpHost;
import org.apache.http.HttpRequest;
import org.apache.http.HttpVersion;
import org.apache.http.client.HttpClient;
import org.apache.http.client.ResponseHandler;
import org.apache.http.client.params.ClientPNames;
import org.apache.http.conn.ClientConnectionManager;
import org.apache.http.conn.params.ConnRoutePNames;
import org.apache.http.conn.scheme.PlainSocketFactory;
import org.apache.http.conn.scheme.Scheme;
import org.apache.http.conn.scheme.SchemeRegistry;
import org.apache.http.conn.scheme.SocketFactory;
import org.apache.http.conn.ssl.SSLSocketFactory;
import org.apache.http.impl.client.BasicResponseHandler;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.impl.conn.tsccm.ThreadSafeClientConnManager;
import org.apache.http.message.BasicHttpRequest;
import org.apache.http.params.BasicHttpParams;
import org.apache.http.params.HttpParams;
import org.apache.http.params.HttpProtocolParams;

/**
 * How to send a request via proxy using {@link HttpClient HttpClient}.
 *
 * @author <a href="mailto:rolandw at apache.org">Roland Weber</a>
 *
 * 
 *         <!-- empty lines above to avoid 'svn diff' context problems
-->
 * @version $Revision$
 *
 * @since 4.0
 */
public class ClientExecuteProxy {

	/**
	 * The default parameters. Instantiated in {@link #setup setup}.
	 */
	private static HttpParams defaultParameters = null;

	/**
	 * The scheme registry. Instantiated in {@link #setup setup}.
	 */
	private static SchemeRegistry supportedSchemes;

	/**
	 * Main entry point to this example.
	 * 
	 * @param args
	 *            ignored
	 */
	public final static void main(String[] args) throws Exception {

		System.setProperty("org.apache.commons.logging.Log",
	
"org.apache.commons.logging.impl.SimpleLog");
	
System.setProperty("org.apache.commons.logging.simplelog.showdatetime",
				"true");
		System
				.setProperty(
	
"org.apache.commons.logging.simplelog.log.httpclient.wire.header",
						"error");
		System
				.setProperty(
	
"org.apache.commons.logging.simplelog.log.org.apache.commons.httpclient"
,
						"error");
		// make sure to use a proxy that supports CONNECT
		final HttpHost target = new HttpHost("www.dnbnor.no",
443, "https");
		final HttpHost proxy = new HttpHost("proxy", 88,
"http");

		setup(); // some general setup

		HttpClient client = createHttpClient();

	
client.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY, proxy);
	
client.getParams().setParameter(ClientPNames.HANDLE_REDIRECTS, true);

		HttpRequest req = createRequest();
		HttpEntity entity = null;
		ResponseHandler<String> responseHandler = new
BasicResponseHandler();
		String responseBody =null;
		try {
				responseBody = client.execute(target,
req,
						responseHandler);
				int start = responseBody.indexOf("Srv:",
0) + 4;
				int next = responseBody.indexOf("\nT:",
0);
				if (start > 0 && next > 0) {
	
System.out.println(responseBody.substring(start, next));
			}

		} finally {
			if (entity != null)
				entity.consumeContent(); // release
connection gracefully
		}
	} // main

	private final static HttpClient createHttpClient() {

		ClientConnectionManager ccm = new
ThreadSafeClientConnManager(
				getParams(), supportedSchemes);
		// new SingleClientConnManager(getParams(),
supportedSchemes);

		DefaultHttpClient dhc = new DefaultHttpClient(ccm,
getParams());

		return dhc;
	}

	/**
	 * Performs general setup. This should be called only once.
	 */
	private final static void setup() {

		supportedSchemes = new SchemeRegistry();

		// Register the "http" and "https" protocol schemes,
they are
		// required by the default operator to look up socket
factories.
		SocketFactory sf =
PlainSocketFactory.getSocketFactory();
		supportedSchemes.register(new Scheme("http", sf, 80));
		sf = SSLSocketFactory.getSocketFactory();
		supportedSchemes.register(new Scheme("https", sf, 443));

		// prepare parameters
		HttpParams params = new BasicHttpParams();
		HttpProtocolParams.setVersion(params,
HttpVersion.HTTP_1_1);
		HttpProtocolParams.setContentCharset(params, "UTF-8");
		HttpProtocolParams.setUseExpectContinue(params, true);
		defaultParameters = params;

	} // setup

	private final static HttpParams getParams() {
		return defaultParameters;
	}

	/**
	 * Creates a request to execute in this example.
	 * 
	 * @return a request without an entity
	 */
	private final static HttpRequest createRequest() {

		HttpRequest req = new BasicHttpRequest(
				"GET",
	
"/seg-person/portallogin/select_login.jhtml?dyn_server=192.168.1.163:685
2&uid=01019945672",
				HttpVersion.HTTP_1_1);
		// ("OPTIONS", "*", HttpVersion.HTTP_1_1);

		return req;
	}

} // class ClientExecuteProxy
 


mvh
Hermod
-----Original Message-----
From: Oleg Kalnichevski [mailto:olegk@apache.org]
Sent: Thursday, September 25, 2008 8:13 PM
To: HttpClient User Discussion
Subject: RE: ResponseProcessCookies - CookieSpec not available
inHTTPcontext

On Wed, 2008-09-24 at 08:36 +0200, hermod.opstvedt@dnbnor.no wrote:
> Hi
> 
> Sorry if I was a bit sparse here.
> 
> I writing a test client that checks the availability of page2 in a 
> multi sequence dialog. If you do this in a browser:
> 
> 1. Enter a regular http URI for the site - You get redirected to a 
> https page.
> 2. Fill in a form field on that page and submit - The reponse is page2

> (also https).
> 
> 
> I was running a slightly modified version of the ClientExecuteProxy 
> sample where the I had 443 and https for the target. I then got:
> 
> ResponseProcessCookies - CookieSpec not available in HTTP context
> 

Hermod,

This sounds like a bug in HttpClient. Would it be a big deal for you to
reproduce the problem with a test case?


> So getting that response I assumed that I needed to set up a cookie 
> store - Hence my question.
> 
> I have now changed it to 80 and http, and it runs OK.
> 
> On a side note: If I run the ClientCustomContext, I get:
> 
> HTTP/1.1 200 OK
> Response content length: -1
> Chunked?: true
> 
> How do I get at the chuncked response? - I know that a chuncked 
> response comes from a persistant connection, but I have not found a 
> sample that shows how the API handles this type of response.
> 

HttpClient handles transfer encoding completely transparently. There is
nothing that needs to be on the consumer side. Just read data from the
content stream of the response entity.

Oleg  




> 
> Hermod
> 
> -----Original Message-----
> From: Oleg Kalnichevski [mailto:olegk@apache.org]
> Sent: Tuesday, September 23, 2008 10:59 PM
> To: HttpClient User Discussion
> Subject: Re: ResponseProcessCookies - CookieSpec not available in 
> HTTPcontext
> 
> On Tue, 2008-09-23 at 12:10 +0200, hermod.opstvedt@dnbnor.no wrote:
> > Hi
> > 
> > Sorry if this has been raised before, but I have searched and not 
> > found an answer to this problem
> > 
> > I am using 4.0-beta1 client, and when I issue a request, i get the 
> > following message:
> > ResponseProcessCookies - CookieSpec not available in HTTP context
> > 
> > And in the response from the server is that request can not be 
> > processed du to no support for cookies.
> > 
> > I have ste the following to no avail:
> > 	
> >
>
client.getParams().setParameter(ClientPNames.COOKIE_POLICY,CookiePolicy.
> > BROWSER_COMPATIBILITY);
> > 	
> 
> Hi Hermod
> 
> What is it exactly you are trying to do? Why are you trying to 
> override the default cookie policy? HttpClient 4.0 comes with a new 
> cookie policy that automatically picks up the best cookie spec for a 
> particular cookie header. If a cookie conforms to the RFC2965 
> HttpClient will handle it according to that spec. If a cookie is an 
> old fashion Netscape style cookie, HttpClient will use the Netscape 
> policy. If unsure, HttpClient will use the browser compatibility spec.
> 
> There should no longer be any need to tweak the default cookie policy.
> 
> > client.getParams().setParameter(ClientContext.COOKIE_STORE, new 
> > BasicCookieStore());
> > 
> 
> This is wrong. If you want to provide a custom cookie store, you have 
> to set it in the execution context. See this example:
> 
> http://svn.apache.org/repos/asf/httpcomponents/httpclient/trunk/module
> -c
> lient/src/examples/org/apache/http/examples/client/ClientCustomContext
> .j
> ava
> 
> Anyways, please post a _complete_ code sample that reproduces the 
> problem. I am not really that good at mind reading. Test cases / wire 
> logs work better for me.
> 
> Oleg
> 
> 
> > Hermod
> 
> 
> 
> 
> 
> > * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
> > *
> > * * *
> > 
> > This email with attachments is solely for the use of the individual 
> > or
> 
> > entity to whom it is addressed. Please also be aware that the DnB 
> > NOR Group cannot accept any payment orders or other legally binding 
> > correspondence with customers as a part of an email.
> > 
> > This email message has been virus checked by the anti virus programs

> > used in the DnB NOR Group.
> > 
> > * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
> > *
> > * * *
> > 
> > 
> > --------------------------------------------------------------------
> > - To unsubscribe, e-mail: httpclient-users-unsubscribe@hc.apache.org
> > For additional commands, e-mail: httpclient-users-help@hc.apache.org
> > 
> 
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: httpclient-users-unsubscribe@hc.apache.org
> For additional commands, e-mail: httpclient-users-help@hc.apache.org
> 
> * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
> * * *
> 
> This email with attachments is solely for the use of the individual or

> entity to whom it is addressed. Please also be aware that the DnB NOR 
> Group cannot accept any payment orders or other legally binding 
> correspondence with customers as a part of an email.
> 
> This email message has been virus checked by the anti virus programs 
> used in the DnB NOR Group.
> 
> * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
> * * *
> 
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: httpclient-users-unsubscribe@hc.apache.org
> For additional commands, e-mail: httpclient-users-help@hc.apache.org
> 


---------------------------------------------------------------------
To unsubscribe, e-mail: httpclient-users-unsubscribe@hc.apache.org
For additional commands, e-mail: httpclient-users-help@hc.apache.org

* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *

This email with attachments is solely for the use of the individual or
entity to whom it is addressed. Please also be aware that the DnB NOR Group
cannot accept any payment orders or other legally binding correspondence with
customers as a part of an email. 

This email message has been virus checked by the anti virus programs used
in the DnB NOR Group.

* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *


---------------------------------------------------------------------
To unsubscribe, e-mail: httpclient-users-unsubscribe@hc.apache.org
For additional commands, e-mail: httpclient-users-help@hc.apache.org


RE: ResponseProcessCookies - CookieSpec not available inHTTPcontext

Posted by Oleg Kalnichevski <ol...@apache.org>.
> Hi
> 
> On your last note: The content length is -1!?
> 

Yes, because the content length is not known (most likely because the
entity is chunk coded)


> Here is the sample code - Just run it:
> 

OK. I can reproduce the problem and confirm it is a bug. I'll fix it
when I am back from vacation in 10 days.

Oleg


---------------------------------------------------------------------
To unsubscribe, e-mail: httpclient-users-unsubscribe@hc.apache.org
For additional commands, e-mail: httpclient-users-help@hc.apache.org