hc-httpclient-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Oleg Kalnichevski <ol...@apache.org>
Subject Re: Redirected - but where? (revisited)
Date Mon, 15 Dec 2008 20:35:29 GMT
Stuart White wrote:
> Hmmm... no, it doesn't seem fixed to me.
> 
> This morning I grabbed the latest svn trunk snapshots of httpcode and
> httpclient.  For httpclient I'm using revision 726436, and for
> httpcore I'm using revision 726437.
> 
> I execute the following code:
> 
> package example;
> 
> import org.apache.http.HttpEntity;
> import org.apache.http.HttpHost;
> import org.apache.http.HttpResponse;
> import org.apache.http.client.HttpClient;
> import org.apache.http.client.methods.HttpGet;
> import org.apache.http.client.methods.HttpUriRequest;
> import org.apache.http.protocol.ExecutionContext;
> import org.apache.http.protocol.HttpContext;
> import org.apache.http.protocol.BasicHttpContext;
> import org.apache.http.impl.client.DefaultHttpClient;
> import org.apache.http.conn.params.ConnRoutePNames;
> import java.net.URL;
> import java.net.URI;
> import java.net.MalformedURLException;
> 
> public class redirect
> {
>    public static void main(String[] args)
>    {
>       try
>       {
>          DefaultHttpClient httpclient = new DefaultHttpClient();
> 
>          HttpHost proxy = new HttpHost("localhost", 8080);
> 
>          httpclient.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY,
> proxy);
> 
>          HttpContext localContext = new BasicHttpContext();
>          HttpGet httpget = new HttpGet("http://www.google.com");
> 
>          HttpResponse response = httpclient.execute(httpget, localContext);
>          HttpEntity entity = response.getEntity();
> 
>          System.out.println("----------------------------------------");
>          System.out.println(response.getStatusLine());
>          if (entity != null) {
>             System.out.println("Response content length: " +
> entity.getContentLength());
>          }
>          if (entity != null) {
>             entity.consumeContent();
>          }
> 
>          System.out.println("ResolvedURL=" +
> getLocation(localContext).toString());
>       }
>       catch (Exception ex)
>       {
>          ex.printStackTrace();
>       }
>    }
> 
>    private static URL getLocation(HttpContext context) throws
> MalformedURLException
>    {
>       HttpUriRequest finalRequest =
> (HttpUriRequest)context.getAttribute(ExecutionContext.HTTP_REQUEST);
>       HttpHost host = (HttpHost)
> context.getAttribute(ExecutionContext.HTTP_TARGET_HOST);
> 
>       StringBuilder uriString = new StringBuilder();
>       uriString.append(host.getSchemeName() + "://");
>       uriString.append(host.getHostName());
>       if (host.getPort() != -1)
>       {
>          uriString.append(":" + host.getPort());
>       }
>       uriString.append(finalRequest.getURI().normalize().toString());
>       return new URL(uriString.toString());
>    }
> }
> 

There is a small problem with your code. The following getLocation 
implementation will produce the expected result.

private static URL getLocation(HttpContext context)
  throws MalformedURLException {
         HttpUriRequest finalRequest = (HttpUriRequest) context
                 .getAttribute(ExecutionContext.HTTP_REQUEST);
         return finalRequest.getURI().toURL();
     }

----------------------------------------
HTTP/1.1 200 OK
Response content length: 6341
ResolvedURL=http://www.google.com/

> And what it prints out is:
> 
> ----------------------------------------
> HTTP/1.1 200 OK
> Response content length: -1
> ResolvedURL=http://www.google.comhttp://www.google.com
> 
> Am I doing something wrong?  Thanks!
> 

Yep.

Oleg

> ---------------------------------------------------------------------
> 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


Mime
View raw message