0

Well this question may sounds a little crazy but I just want to save time and engergy. To open a connection needs time and engergy on a mobile device so I want to reuse open connections if possible.

It can happen that I'll need to download files from example.com and example.net. Both sites are hosted on the same server/ip so it should be possible to get documents from both documents with just one connection.

DefaultHttpClient client = new DefaultHttpClient();
HttpGet get = new HttpGet("http://example.com/robots.txt");
HttpGet get2 = new HttpGet("http://example.net/robots.txt");
URI uri = get.getURI();
HttpHost host = new HttpHost(uri.getHost(), uri.getPort(), uri.getScheme());
ResponseHandler<String> responseHandler = new BasicResponseHandler();
HttpResponse response = client.execute(host, get);
String data = responseHandler.handleResponse(response);
Log.v("test", "Downloaded " + data.length() + " bytes from " + get.getURI().toASCIIString());
response = client.execute(host, get2);
data = responseHandler.handleResponse(response);
Log.v("test", "Downloaded " + data.length() + " bytes from " + get2.getURI().toASCIIString());

The problem is when I don't use that HttpHost for each call is a new connection established. If I use that both Host HTTP headers points to the first domain. How can I fix that?

rekire
  • 47,260
  • 30
  • 167
  • 264

1 Answers1

0

The solution is quiet simple (if you found it):

HttpGet get2 = new HttpGet("http://example.com/robots.txt");
BasicHttpParams params = new BasicHttpParams();
params.setParameter(ClientPNames.VIRTUAL_HOST, new HttpHost("example.net", -1, "http"));
get2.setParams(params);

So will the connection be reused because it's the same domain/port/schema and the DefaultHttpClient will look for that parameter ClientPNames.VIRTUAL_HOST.

I found the solution with the sourcecode which is actuall used by android which is to find on code.google.com.

rekire
  • 47,260
  • 30
  • 167
  • 264