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/07/13 12:13:35 UTC

Redirects question

Hello,

 

This is the first time I have to deal with redirects, and I’m a bit lost.
Let me explain my case:

 

I have a website in http://www.figueras.com, that immediately redirects
(through an apache rewrite rule) to
http://www.figueras.com/figueras/passintro-lang-ES.html and shows the home
page.

 

I have developed a simplified http proxy. When I point www.figueras.com to
my proxy, the redirect is not followed. The home page is shown, the browser
url remains in http://www.figueras.com and images,css, … are downloaded but
links are broken (because they lost the /figueras context).

 

Can HttpClient redirectStrategy help me with this, or I have to manage
redirects manually?

 

I have included this in my HttpClient instance:

this.objHttp.setRedirectStrategy(new VentusRedirectStrategy());

 

And the following code seems to be used for everybody on Internet:

 

public class VentusRedirectStrategy extends DefaultRedirectStrategy

{

public boolean isRedirected(HttpRequest request, HttpResponse response,
HttpContext context)  

 {

  boolean isRedirect = false;

  

  System.out.print(response.getStatusLine().getStatusCode() + " = " +
request.getRequestLine().getUri());

 

  try { isRedirect = super.isRedirected(request, response, context);
System.out.println(" , " + isRedirect); } 

  catch (ProtocolException e) 

  {

   // TODO Auto-generated catch block

   e.printStackTrace();

  }

  if (!isRedirect) 

  {

   int responseCode = response.getStatusLine().getStatusCode();

   if (responseCode == 301 || responseCode == 302) return true;

  }

  return isRedirect;

}

}

 

When I send http://www.figueras.com to my servlet, the above
System.out.println prints this:

301 = / , true

200 = /figueras/passintro-lang-ES.html , false

304 = /estils_new.css , false

304 = /html/js/bredax_all.js , false

304 = /main.js , false

304 = /menu_idiomes.css , false

200 = /img/logo_figueras.gif , false

304 = /jquery.js , false

200 = /img/logo_fis.gif , false

200 = /img/logo_enginnering.gif , false

200 = /img/logo_design.gif , false

200 = /img/logo_chairs.gif , false

304 = /img/contact_M.png , false

304 = /img/contact_L.png , false

304 = /img/contact_R.png , false

200 = /main.js , false

304 = /img/fondos/world_map.jpg , false

304 = /img/home_03.jpg , false

304 = /img/logos_home.png , false

304 = /img/home_04.jpg , false

304 = /img/home_02.jpg , false

304 = /img/home_01.jpg , false

304 = /img/diari.png , false

 

The redirectStrategy detects the first redirect (301) form
http://www.figueras.com and seems to redirect to
‘/figueras/passintro-lang-ES.html’.

 

Then many 304 response error code happens, and each of them provokes a :

org.apache.http.NoHttpResponseException: The target server failed to respond

        at
org.apache.http.impl.conn.DefaultHttpResponseParser.parseHead(DefaultHttpRes
ponseParser.java:95)

        at
org.apache.http.impl.conn.DefaultHttpResponseParser.parseHead(DefaultHttpRes
ponseParser.java:62)

        at
org.apache.http.impl.io.AbstractMessageParser.parse(AbstractMessageParser.ja
va:254)

        at
org.apache.http.impl.AbstractHttpClientConnection.receiveResponseHeader(Abst
ractHttpClientConnection.java:289)

        at
org.apache.http.impl.conn.DefaultClientConnection.receiveResponseHeader(Defa
ultClientConnection.java:252)

        at
org.apache.http.impl.conn.ManagedClientConnectionImpl.receiveResponseHeader(
ManagedClientConnectionImpl.java:191)

        at
org.apache.http.protocol.HttpRequestExecutor.doReceiveResponse(HttpRequestEx
ecutor.java:300)

        at
org.apache.http.protocol.HttpRequestExecutor.execute(HttpRequestExecutor.jav
a:127)

        at
org.apache.http.impl.client.DefaultRequestDirector.tryExecute(DefaultRequest
Director.java:713)

        at
org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDir
ector.java:518)

        at
org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.ja
va:906)

        at
org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.ja
va:805)

        at
com.vpfw.proxy.services.http.HttpService.sendRequest(HttpService.java:563)

 

I suppose that I have to detect the redirect and manually sends another
request to the new url. Otherwise, the browser does not show the new url.
And forget the redirectStrategy.

 

Is it ok? Or Am I missing something?

Any help would be very appreciated,

 

Thanks,

Joan.

 

 

 


RE: Redirects question

Posted by Joan Balaguero <jo...@grupoventus.com>.
Hello,

Problem solved.

Joan.

-----Mensaje original-----
De: Joan Balaguero [mailto:joan.balaguero@grupoventus.com] 
Enviado el: viernes, 13 de julio de 2012 12:14
Para: 'HttpClient User Discussion'
Asunto: Redirects question

Hello,

 

This is the first time I have to deal with redirects, and I’m a bit lost.
Let me explain my case:

 

I have a website in http://www.figueras.com, that immediately redirects
(through an apache rewrite rule) to
http://www.figueras.com/figueras/passintro-lang-ES.html and shows the home
page.

 

I have developed a simplified http proxy. When I point www.figueras.com to
my proxy, the redirect is not followed. The home page is shown, the browser
url remains in http://www.figueras.com and images,css, … are downloaded but
links are broken (because they lost the /figueras context).

 

Can HttpClient redirectStrategy help me with this, or I have to manage
redirects manually?

 

I have included this in my HttpClient instance:

this.objHttp.setRedirectStrategy(new VentusRedirectStrategy());

 

And the following code seems to be used for everybody on Internet:

 

public class VentusRedirectStrategy extends DefaultRedirectStrategy

{

public boolean isRedirected(HttpRequest request, HttpResponse response,
HttpContext context)  

 {

  boolean isRedirect = false;

  

  System.out.print(response.getStatusLine().getStatusCode() + " = " +
request.getRequestLine().getUri());

 

  try { isRedirect = super.isRedirected(request, response, context);
System.out.println(" , " + isRedirect); } 

  catch (ProtocolException e) 

  {

   // TODO Auto-generated catch block

   e.printStackTrace();

  }

  if (!isRedirect) 

  {

   int responseCode = response.getStatusLine().getStatusCode();

   if (responseCode == 301 || responseCode == 302) return true;

  }

  return isRedirect;

}

}

 

When I send http://www.figueras.com to my servlet, the above
System.out.println prints this:

301 = / , true

200 = /figueras/passintro-lang-ES.html , false

304 = /estils_new.css , false

304 = /html/js/bredax_all.js , false

304 = /main.js , false

304 = /menu_idiomes.css , false

200 = /img/logo_figueras.gif , false

304 = /jquery.js , false

200 = /img/logo_fis.gif , false

200 = /img/logo_enginnering.gif , false

200 = /img/logo_design.gif , false

200 = /img/logo_chairs.gif , false

304 = /img/contact_M.png , false

304 = /img/contact_L.png , false

304 = /img/contact_R.png , false

200 = /main.js , false

304 = /img/fondos/world_map.jpg , false

304 = /img/home_03.jpg , false

304 = /img/logos_home.png , false

304 = /img/home_04.jpg , false

304 = /img/home_02.jpg , false

304 = /img/home_01.jpg , false

304 = /img/diari.png , false

 

The redirectStrategy detects the first redirect (301) form
http://www.figueras.com and seems to redirect to
‘/figueras/passintro-lang-ES.html’.

 

Then many 304 response error code happens, and each of them provokes a :

org.apache.http.NoHttpResponseException: The target server failed to respond

        at
org.apache.http.impl.conn.DefaultHttpResponseParser.parseHead(DefaultHttpRes
ponseParser.java:95)

        at
org.apache.http.impl.conn.DefaultHttpResponseParser.parseHead(DefaultHttpRes
ponseParser.java:62)

        at
org.apache.http.impl.io.AbstractMessageParser.parse(AbstractMessageParser..j
a
va:254)

        at
org.apache.http.impl.AbstractHttpClientConnection.receiveResponseHeader(Abst
ractHttpClientConnection.java:289)

        at
org.apache.http.impl.conn.DefaultClientConnection.receiveResponseHeader(Defa
ultClientConnection.java:252)

        at
org.apache.http.impl.conn.ManagedClientConnectionImpl.receiveResponseHeader(
ManagedClientConnectionImpl.java:191)

        at
org.apache.http.protocol.HttpRequestExecutor.doReceiveResponse(HttpRequestEx
ecutor.java:300)

        at
org.apache.http.protocol.HttpRequestExecutor.execute(HttpRequestExecutor.jav
a:127)

        at
org.apache.http.impl.client.DefaultRequestDirector.tryExecute(DefaultRequest
Director.java:713)

        at
org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDir
ector.java:518)

        at
org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient..j
a
va:906)

        at
org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient..j
a
va:805)

        at
com.vpfw.proxy.services.http.HttpService.sendRequest(HttpService.java:563)

 

I suppose that I have to detect the redirect and manually sends another
request to the new url. Otherwise, the browser does not show the new url.
And forget the redirectStrategy.

 

Is it ok? Or Am I missing something?

Any help would be very appreciated,

 

Thanks,

Joan.

 

 

 


-----
No se encontraron virus en este mensaje.
Comprobado por AVG - www.avg.com
Versión: 2012.0.2195 / Base de datos de virus: 2437/5128 - Fecha de
publicación: 07/12/12


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