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 Joan Balaguero <jo...@grupoventus.com> on 2012/10/01 00:52:06 UTC
Problem with cookies
Hello,
Im having an strange problem with some lost cookies.
Ive developed a small test program that sends a get request to
http://www.viajeselcorteingles.es/viajes/vuelos_hoteles/programas/listado_ho
teles.asp?codwebor=ESP
<http://www.viajeselcorteingles.es/viajes/vuelos_hoteles/programas/listado_h
oteles.asp?codwebor=ESP&paquetesDinamicos=N&descuentoResid=N&copagina=NAC&fo
lleto=&codPais=CAT&codiArea=BCN&descripcion=Barcelona&codiZona=&fechaIni=201
21114&fechaFin=20121115&numHabit=1&numAdultos1=2>
&paquetesDinamicos=N&descuentoResid=N&copagina=NAC&folleto=&codPais=CAT&codi
Area=BCN&descripcion=Barcelona&codiZona=&fechaIni=20121114&fechaFin=20121115
&numHabit=1&numAdultos1=2
and prints the response cookies:
Set-Cookie = VIAJESES=r3238671101; path=/; expires=Fri, 1 Jan 2016 01:01:50
GMT
Set-Cookie = session%2Did=03048646336583665; Path=/
Set-Cookie = ASPSESSIONIDSSSRTCDT=FEIIKPMAOMGEIFPHGGIBMIAJ; Path=/
If I execute the same get N times, I receive the same cookies N times.
But when I send the get request through my http proxy (developed by me),
adding these lines of code:
HttpHost proxy = new HttpHost("95.17.8.174", 8051, "http");
objHttp.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY, proxy);
the first time I perform the get request, I get the same cookies (with
different values):
set-cookie = VIAJESES=r235171774; path=/; expires=Fri, 1 Jan 2016 01:01:50
GMT
set-cookie = session%2Did=049851416475118569; Path=/
set-cookie = ASPSESSIONIDQQSDTTAA=PBIIAIJBNOPOFDEJKGCIICFP; Path=/
But in the next executions, some cookies are lost. Below the session%2Did
cookie does not appear:
set-cookie = VIAJESES=r235171774; path=/; expires=Fri, 1 Jan 2016 01:01:50
GMT
set-cookie = ASPSESSIONIDCADDAQTT=GCHGNBPDPFHEIABIODPLPCCP; Path=/
And in this execution, only the first cookie is received:
set-cookie = VIAJESES=r235171774; path=/; expires=Fri, 1 Jan 2016 01:01:50
GMT
Then, I stop Tomcat (where my proxy servlet is running) and I see the
following messages:
30-sep-2012 23:32:48 org.apache.catalina.loader.WebappClassLoader
clearThreadLocalMap
GRAVE: The web application [] created a ThreadLocal with key of type
[org.apache.http.impl.cookie.DateUtils$DateFormatHolder$1] (value
[org.apache.http.impl.cookie.DateUtils$DateFormatHolder$1@63a9ab54]) and a
value of type [java.lang.ref.SoftReference] (value
[java.lang.ref.SoftReference@182f4aea]) but failed to remove it when the web
application was stopped. This is very likely to create a memory leak.
When Tomcat is started again, I repeat the sequence and get the same result:
with the first execution I get the 3 cookies, and in next executions I
always lose session%2Did and, sometimes, also ASPSESSIONID
... In fact,
just the first execution after a Tomcat restart shows the 3 cookies.
After spending days and days trying to find out whats going on, Ive seen
the problem. If I create a new HttpClient every time I perform a request,
works OK. If I create an HttpClient in the servlet init method, and
subsequent gets are performed using the same instance of HttpClient, then it
doesnt work.
This is the servlet:
public class ProxyServletTest extends HttpServlet
{
private static final long serialVersionUID = -1597058980193608683L;
private DefaultHttpClient objHttp;
public void init() { this.objHttp = new DefaultHttpClient(); }
public void service(HttpServletRequest request, HttpServletResponse
response) throws ServletException, IOException
{
HttpResponse httpresponse = null;
try
{
this.objHttp = new DefaultHttpClient(); <--- If I add this line,
works.
HttpGet objGet = new
HttpGet(request.getRequestURL().append("?").append(request.getQueryString())
.toString());
httpresponse = this.objHttp.execute(objGet);
HeaderIterator it = httpresponse.headerIterator();
while (it.hasNext())
{
Header h = it.nextHeader();
System.out.println(h.getName() + " = " + h.getValue());
response.addHeader(h.getName(), h.getValue());
}
}
finally
{
EntityUtils.consume(httpresponse.getEntity());
}
}
}
Could anybody tell me whats wrong?
Thanks in advance,
Joan.