从不同的域下载文件,一个连接 [英] Download files from different domains with one connection

查看:129
本文介绍了从不同的域下载文件,一个连接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

那么可能这个问题听起来有点疯狂,但我只是想节省时间和engergy。打开连接需要时间和engergy在移动设备上,所以我想如果可能的话重用打开的连接。

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.

这可能发生,我需要从 example.com example.net 下载文件。这两个网站都在同一台服务器/ IP承载,因此应该可以得到两个文件的文件只有一个连接。

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());

问题是,当我不使用 HttpHost 每个呼叫建立一个新的连接。如果我使用这两个主机 HTTP头指向第一个域。我该如何解决呢?

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?

推荐答案

解决方案是安静简单(如果你发现它):

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

所以,因为这是同一个域/端口/模式和 DefaultHttpClient 将寻找参数 ClientPNames.VIRTUAL_HOST

我发现与源$ C ​​$ c中的解决方案,这是实际工作中使用由Android这是找到<一个href=\"https://$c$c.google.com/p/android-source-browsing/source/browse/src/org/apache/http/impl/client/DefaultRequestDirector.java?repo=platform--external--apache-http&name=ics-mr1-release#387\"相对=nofollow> code.google.com 。

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

这篇关于从不同的域下载文件,一个连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆