views:

41

answers:

2

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?

A: 

I'm guessing the scheme registry is or works pretty much like a HashMap. If I'm right, you can change the scheme for (e.g.) http simply by registering a new set of arguments.

So at whatever point you read in your config file, you can simply repeat the call

registry.register(new Scheme("http",PlainSocketFactory.getSocketFactory(), 80));

with a different port number.

Problem: registry is a local variable in the static block. Solution: do

static SchemeRegistry registry; 

outside (and preferrably before) your static block so you'll be able to access the variable, err, class field, at a later time.

To make this work, you also need to change the line

SchemeRegistry registry = new SchemeRegistry();

to

registry = new SchemeRegistry();

If you want to do the registration change from another class you can make registry public, or give it a static accessor or something.

Carl Smotricz
+1  A: 

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.

S73417H
oh, ok thanks. so no modification of the code is necessary then.
jax
Just for reference, if I allow the scheme to change, say to https, will I then need to create a new Scheme as mentioned by @Carl or will the system be able to work all this out when I pass https as the scheme when making the connection?
jax
You can register multiple Schemes, so, if you register a http and https scheme, then create a connection using a https:// URL, the https Scheme is used.
S73417H