commons-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Duttaroy, Debasish" <debasish.dutta...@fmr.com>
Subject [HttpClient] - Convert servlet cookies into HttpClient cookie
Date Mon, 11 Jun 2007 18:26:30 GMT
I need to pass in the cookies that I get from the HttpRequest (read, a
servlet request!) when I make a call to the back end using HttpClient.

What I do now is as follows:

1. Get all the javax.servlet.http.Cookie from the servlet request - 
	
	Cookie[] cookies = request.getCookies();

2. Now loop through all the cookies, and create a
org.apache.commons.httpclient.Cookie. Here is the method

	private org.apache.commons.httpclient.Cookie[]
getHttpClientCookies(Cookie[] cookies) {
		
		int numberOfCookies =0;
		if(cookies!= null){
			numberOfCookies = cookies.length;
		}
		org.apache.commons.httpclient.Cookie[] httpClientCookies
= new org.apache.commons.httpclient.Cookie[numberOfCookies]; 
		for (int i = 0; i < numberOfCookies; i++) {
			Cookie c = cookies[i];
			String domain = c.getDomain();
			String name = c.getName();
			String value = c.getValue();
			String path = c.getPath();
			boolean secure = c.getSecure();
			int maxAge = c.getMaxAge();
			org.apache.commons.httpclient.Cookie hCookie =
new org.apache.commons.httpclient.Cookie(
					domain, name, value, path,
maxAge, secure);
			httpClientCookies[i] = hCookie;
		}
		return httpClientCookies;
	}

3. Now, when making a back end call using a HttpClient, I add these
cookies. 

I am wondering if this is the right way of doing it. I did not find any
other way of doing it. Also, when I add the cookies, I add them to the
client. I want to add it to the method. Is there a way to do it?

Also, let me know if you have anything to share related to cookie
specification, cookie policy etc.

Let me know if you or anyone you know have done something similar.

Thanks
Debasish


---------------------------------------------------------------------
To unsubscribe, e-mail: commons-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-user-help@jakarta.apache.org


Mime
View raw message