You are viewing a plain text version of this content. The canonical link for it is here.
Posted to httpclient-users@hc.apache.org by Puneet Lakhina <pu...@gmail.com> on 2006/07/16 21:51:30 UTC

Tutorial possible error

Hi,
I am really new to this api. I went through the tutorial and tried doing the
very basic thing. Just getting the reponse based on a http request.

HttpClient client  = new HttpClient();
GetMethod method = new GetMethod("www.google.com");
try {
client.executeMethod(method);
System.out.println(new String(method.getResponseBody());
}catch(Exception e) {
e.printStackTrace();
}

But this throws an exception
java.lang.IllegalArgumentException: host parameter is null
        at org.apache.commons.httpclient.HttpConnection.<init>(
HttpConnection.java:205)
        at org.apache.commons.httpclient.HttpConnection.<init>(
HttpConnection.java:154)
        at
org.apache.commons.httpclient.SimpleHttpConnectionManager.getConnectionWithTimeout
(SimpleHttpConnectionManager.java:149)
        at org.apache.commons.httpclient.HttpMethodDirector.executeMethod(
HttpMethodDirector.java:152)
        at org.apache.commons.httpclient.HttpClient.executeMethod(
HttpClient.java:396)
        at org.apache.commons.httpclient.HttpClient.executeMethod(
HttpClient.java:324)


While browsing through the examples i found the following thig used..

client.getHostConfiguration().setHost("www.google.com");

so i tried this and then it worked fine.
I am using jdk 1.5
httpclient version 3.0.1
logging version 1.1
codec version 1.3

is there something wrong with the way im doing things??
because thats the way its suggested in the url, sans the retry code.

My redone code looks like this

HttpClient client  = new HttpClient();
client.getHostConfiguration().setHost("www.google.com");
GetMethod method = new GetMethod("www.google.com");
try {
client.executeMethod(method);
System.out.println(new String(method.getResponseBody());
}catch(Exception e) {
e.printStackTrace();
}

Thanks
-- 
Puneet

Re: Tutorial possible error

Posted by Roland Weber <RO...@de.ibm.com>.
Hello Puneet,

> I am really new to this api. I went through the tutorial and tried doing 
the
> very basic thing. Just getting the reponse based on a http request.
> 
> HttpClient client  = new HttpClient();
> GetMethod method = new GetMethod("www.google.com");

"www.google.com" is _not_ a URL. Use "http://www.google.com/".
Browsers make that transformation automatically. But HttpClient
is not a browser.

> try {
> client.executeMethod(method);
> System.out.println(new String(method.getResponseBody());
> }catch(Exception e) {
> e.printStackTrace();
> }

Never write code that fails to release the connection.
NEVER. Not even in the dumbest "hello world". Just don't.

cheers,
  Roland


---------------------------------------------------------------------
To unsubscribe, e-mail: httpclient-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: httpclient-user-help@jakarta.apache.org