Hi Oleg, Thanks very much for your help. It works except the bug with the cache but I found a workaround. I will try to fix it.
2013/12/23 Oleg Kalnichevski <ol...@apache.org> > On Mon, 2013-12-23 at 10:21 +0100, Francois-Xavier Bonnet wrote: > > Hi, > > > > I am trying to migrate to HttpClient 4.3 and I cannot find a way to do > what > > I used to do before with virtual hosts without using any deprecated api. > > My goal is to be able to send a request to a server but using a "Host" > > header with a different server name. > > > > I am using a HttpClient with cache and calling > > method org.apache.http.client.HttpClient.execute(HttpHost, HttpRequest, > > HttpContext) > > I found 4 different places I can set a host: > > > > - as the first parameter of execute method > > - in the URI of the request > > Hi Francois-Xavier > > The first parameter represents a physical connection point, that is, the > opposite endpoint of the outgoing connection. The host of the request > URI represents a 'virtual' address and can be just about anything the > target server is willing to accept as a valid host. The latter will end > up in the execution context as the target host attribute > > --- > CloseableHttpClient client = HttpClients.createSystem(); > HttpHost target = new HttpHost("www.google.com"); > HttpGet get = new HttpGet("http://www.google.ru/"); > client.execute(target, get); > --- > > > - by calling > > method > org.apache.http.protocol.HttpCoreContext.setTargetHost(HttpHost) > > This should produce the same result as above > --- > CloseableHttpClient client = HttpClients.createSystem(); > HttpHost target = new HttpHost("www.google.com"); > HttpGet get = new HttpGet("/"); > HttpClientContext context = HttpClientContext.create(); > context.setTargetHost(new HttpHost("www.google.ru")); > client.execute(target, get, context); > --- > > > - by setting a Host header in the request > > > > This is also valid but may produce side effects as HttpClient may be > unable to match host specific details to the correct host. > > > I have tried to debug and it looks like when send a request, the > > destination server is identified by using 1) the one ine the URI 2) the > > first parameter of execute method if no host is set in the uri 3) the > > targetHost in HttpContext. This host is then used to set targetHost is > > HttpContext and to determine the HttpRoute to the destination. > > > > My problems: > > > > - the cookies domains are matched against the targetHost (not the Host > > header) so the cookies are rejected > > As I said if one manually overrides the 'Host' header in the outgoing > request this indeed can happen. In the normal course of request > execution the 'Host' header is generated based on the target host > attribute and cookie domain of origin should be correctly matched. > > > - the cache key uses the host name in the HttpRoute which is also the > > targetHost so the cache entries for different virtual hosts on the > same > > server are going to be mixed > > > > This sounds like a bug and should be fixed. > > Oleg > > > > --------------------------------------------------------------------- > To unsubscribe, e-mail: httpclient-users-unsubscr...@hc.apache.org > For additional commands, e-mail: httpclient-users-h...@hc.apache.org > >