Using: org.apache.http
I am using the following code to download files, most of the url setup is done statically to save creating the object every time.
private static final HttpClient httpClient;
static {
HttpParams params = new BasicHttpParams();
HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1);
HttpProtocolParams.setContentCharset(params, HTTP.UTF_8);
HttpProtocolParams.setUseExpectContinue(params, false);
HttpConnectionParams.setConnectionTimeout(params, 10000);
HttpConnectionParams.setSoTimeout(params, 10000);
ConnManagerParams.setMaxTotalConnections(params, 100);
ConnManagerParams.setTimeout(params, 30000);
SchemeRegistry registry = new SchemeRegistry();
registry.register(new Scheme("http",PlainSocketFactory.getSocketFactory(), 80)); //TODO port and schema should be coming from the strings file
ThreadSafeClientConnManager manager = new ThreadSafeClientConnManager(params, registry);
httpClient = new DefaultHttpClient(manager, params);
//httpClient.getParams().setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, 30000);
}
I can call it using
private HttpUrlRequest(String host, int port, String path, List<NameValuePair> query, List<NameValuePair> post)
I want to caller to be able to specify the port number to the URL. But because
registry.register(new Scheme("http",PlainSocketFactory.getSocketFactory(), 80));
is specified statically, wouldn’t I have to change that port number also? How can I solve this?
The port specified by a Scheme is the default port and not necessarily the port used in the actual connection. The port can be specified in each request URL. For instance, if the Scheme default port is 80 but the request URL is http://address.com:8080, then 8080 is used. If the port is not specified in the URL, then the default is used.