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: HttpAnsyncClient: is there a way to know the uri?
Date Tue, 20 Nov 2012 14:31:45 GMT
On Tue, 2012-11-20 at 06:43 +0100, Peter Joole wrote:
> Thank you both very much for the fast reply. The way I get the uri is as
> follows (for those who may need this too):
> 
> private getWebpage()
> {
> 
> final HttpContext test = new BasicHttpContext();
> 
>  client.execute(new HttpGet(url), test, new FutureCallback<HttpResponse>()
>  {
> 
> @Override
> public void completed(HttpResponse response)
> {
> 
> HttpRequest request = (HttpRequest) test.getAttribute("http.request");
> 
>        String host = request.getFirstHeader("Host").getValue();
> 
>    String get = request.getRequestLine().getUri();
> 
>          String uri = host + get;
> 
> latch.countDown();
> 
> }
> 
> @Override
> 
>  public void failed(Exception e)
> 
> {
> 
> latch.countDown();
> 
>  }
> 
> 
>  @Override
> 
>  public void cancelled()
> 
> {
> 
> latch.countDown();
> 
>  }
> 
> });
> }
> 
> So if I understand it right: you can request the uri from the HttpGet
> method and then you get the uri, which doesn't change (even though a
> redirect happens). So in other words, it is the original uri that you
> wanted to let the client execute. While in the HttpContext you can get the
> necessary information about the uri which is changed based on a possible
> redirect?
> 

Precisely.

> Then the only problem I have left is where I can find the protocol scheme,
> whether it is http or https. I can get to the ProtocolVersion which tells
> me that it's HTTP 1.0 or 1.1, but not if it's HTTPS or not. Is there a way
> to get this information?
> 

Again, HttpContext is your friend. One can obtain the target host as
'http.target_host' attribute of the execution context.

Oleg

> Peter
> 
> 
> 2012/11/18 Oleg Kalnichevski <olegk@apache.org>
> 
> > On Sun, 2012-11-18 at 15:39 -0500, Mark Greene wrote:
> > > Peter,
> > >
> > > One approach I use is when I construct my callback object, I pass in a
> > > reference to the HttpRequest. From there, you can extra the URI. This
> > > approach can also be achieved if you write your own Future wrapper as
> > well.
> > >
> > > Hope that helps.
> > >
> > > -Mark
> > >
> >
> > Besides, one can always get a reference to the actual request object
> > from the HttpContext instance associated with the response.
> >
> > Oleg
> >
> > > On Sun, Nov 18, 2012 at 1:15 PM, Peter Joole <peter.joole@gmail.com>
> > wrote:
> > >
> > > > I am wondering if there is a way to know the uri that the client had to
> > > > retrieve? I am using the client.execute() with the futurecallback as
> > > > described in the examples. But as far as I can see, you can't retrieve
> > the
> > > > uri it had to execute (whether it is httpget, httphead) in the
> > > > HttpResponse. Am I missing a way? Or do I need to use the HttpClient
> > with
> > > > the ThreadSafeClientConnManager to achieve this and pass along the uri?
> > > >
> >
> >
> >
> > ---------------------------------------------------------------------
> > 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