user590444
user590444

Reputation: 4322

HttpClient exception: java.lang.IllegalArgumentException: host parameter is null

I have next code

    URL targetUrl = ...
    HttpClient client = new HttpClient(connectionManager);
    GetMethod getMethod = new GetMethod();
    getMethod.setPath(targetUrl.getPath());

    HostConfiguration hostConfiguration = getConfiguration(targetUrl) //unknown lib code

    client.executeMethod(hostConfiguration, getMethod);

In some cases(on some hosts) I get

java.lang.IllegalArgumentException: host parameter is null"

on client.executeMethod call.

Why may this happen?

Upvotes: 4

Views: 26252

Answers (4)

Du-Lacoste
Du-Lacoste

Reputation: 12757

This typically happens when HttpClient or HttpMethod used does not have a properly set target host or URL.

In my case URL was not properly set so it was NULL.

Double check that the URL is being set properly and not dynamically created from external source. i.e. in my case it was via OpenSearch data.

Or for the testing purposes, you could trying hardcode the URL you wish to have to make sure it is simply because of an URL issue.

Upvotes: 0

Nirbhay Rana
Nirbhay Rana

Reputation: 4347

This error means that you are using incomplete url or without protocol. For example String url ="google.com". Change it to String url="http://google.com". It will work now

Upvotes: 1

Christophe Roussy
Christophe Roussy

Reputation: 16999

The error message is misleading...

You must add the protocol in front of the host, something like HTTP:// or whatever you want to use. There may be other circumstances in which this happens, according to this blog article, but setHostConfiguration has been deprecated so this only applies to legacy code.

The client code should catch it earlier instead of failing so deep in the system, how can incorrect data go that far?

Upvotes: 8

user590444
user590444

Reputation: 4322

This is a write exampe of proxy http://svn.apache.org/viewvc/httpcomponents/oac.hc3x/trunk/src/examples/ProxyTunnelDemo.java?view=co In my case problem was in hostConfiguration creation

Upvotes: 0

Related Questions