|
View:
New views
2 Messages
—
Rating Filter:
Alert me
|
|
|
The Cookie jar's lid seems stuckHey,
Due to our search engine for one of our sites being hosted on a separate domain, we can't do AJAX requests to the original domain. This isn't much of a problem, as I figured I'd just use HttpClient to make a servlet which is a proxy to the Apache server. So... to ensure session state is still maintained, which is a requirement, I will just proxy the cookies the client sends me as well. I made the following method (the connection manager and parameters are final static members of the class): private HttpEntity proxyRequest(String baseUrl, String getParameters, Cookie[] cookies) throws Exception { DefaultHttpClient httpClient = new DefaultHttpClient(connManager, httpParams); HttpContext context = new BasicHttpContext(httpClient.getDefaultContext()); HttpGet request = new HttpGet(baseUrl + basketUri); CookieStore cookieStore = httpClient.getCookieStore(); for (Cookie c : cookies) { BasicClientCookie newCookie = new BasicClientCookie(c.getName(), c.getValue()); newCookie.setDomain(c.getDomain()); newCookie.setPath(c.getPath()); newCookie.setComment(c.getComment()); if (c.getMaxAge() > 0) { long expiryTime = new Date().getTime() + c.getMaxAge(); newCookie.setExpiryDate(new Date(expiryTime)); } cookieStore.addCookie(newCookie); } HttpResponse response = httpClient.execute(request, context); HttpEntity entity = response.getEntity(); if (entity == null) { throw new NoHttpResponseException("Basket request failed to return a response."); } return entity; } The Cookie[] I pass in, is an array of cookies are received by HttpServletRequest.getCookies(). I stepped the code, and the addCookie() method is definitely being called. When recieving the response on the other side, I make a dump of all received cookies to the output. This output is received by HttpClient and returned to the servlet's output stream. Then what I'm getting is a dump of "0" cookies. So the request is successful, but the cookies added to the store is not being sent to the client. Can anyone see what I'm doing wrong? -- Quintin Beukes --------------------------------------------------------------------- To unsubscribe, e-mail: httpclient-users-unsubscribe@... For additional commands, e-mail: httpclient-users-help@... |
|
|
Re: The Cookie jar's lid seems stuckOn Wed, 2008-07-02 at 11:59 +0200, Quintin Beukes wrote:
> Hey, > > Due to our search engine for one of our sites being hosted on a > separate domain, we can't do AJAX requests to the original domain. > This isn't much of a problem, as I figured I'd just use HttpClient to > make a servlet which is a proxy to the Apache server. > > So... to ensure session state is still maintained, which is a > requirement, I will just proxy the cookies the client sends me as > well. > > I made the following method (the connection manager and parameters are > final static members of the class): > > private HttpEntity proxyRequest(String baseUrl, String > getParameters, Cookie[] cookies) throws Exception > { > DefaultHttpClient httpClient = new DefaultHttpClient(connManager, > httpParams); > HttpContext context = new BasicHttpContext(httpClient.getDefaultContext()); > HttpGet request = new HttpGet(baseUrl + basketUri); > CookieStore cookieStore = httpClient.getCookieStore(); > > for (Cookie c : cookies) > { > BasicClientCookie newCookie = new BasicClientCookie(c.getName(), > c.getValue()); > newCookie.setDomain(c.getDomain()); > newCookie.setPath(c.getPath()); > newCookie.setComment(c.getComment()); > if (c.getMaxAge() > 0) > { > long expiryTime = new Date().getTime() + c.getMaxAge(); > newCookie.setExpiryDate(new Date(expiryTime)); > } > cookieStore.addCookie(newCookie); > } > > HttpResponse response = httpClient.execute(request, context); > HttpEntity entity = response.getEntity(); > > if (entity == null) > { > throw new NoHttpResponseException("Basket request failed to > return a response."); > } > > return entity; > } > > The Cookie[] I pass in, is an array of cookies are received by > HttpServletRequest.getCookies(). > > I stepped the code, and the addCookie() method is definitely being > called. When recieving the response on the other side, I make a dump > of all received cookies to the output. This output is received by > HttpClient and returned to the servlet's output stream. > > Then what I'm getting is a dump of "0" cookies. So the request is > successful, but the cookies added to the store is not being sent to > the client. > > Can anyone see what I'm doing wrong? > Quintin, Adding cookies to the cookie store is just a part of the story. A cookie need to match the origin server in order to be included in subsequent HTTP requests. Apparently there is something wrong with the domain or the expiry date. Take a look at the RequestAddCookies protocol interceptor Hope this helps Oleg --------------------------------------------------------------------- To unsubscribe, e-mail: httpclient-users-unsubscribe@... For additional commands, e-mail: httpclient-users-help@... |
| Free Forum Powered by Nabble | Forum Help |