简体   繁体   中英

Reading URL content using Apache httpclient in Java 1.4

In Java 1.4, I am trying to make an connection to a URL, but since I want to configure a timeout, I am using Apache's httpclient instead of the old school URLConnection.openConnection();

So I have the following:

    HttpConnection conn = null;
    SimpleHttpConnectionManager httpMgr = new SimpleHttpConnectionManager(true);
    HostConfiguration hostConf = new HostConfiguration();
    hostConf.setHost(new HttpHost("http://www.google.com"));
    conn = httpMgr.getConnectionWithTimeout(hostConf, 30);
    conn.open();

But seems like everytime I try to open a connection, I would get a java.net.UnknownHostException: http://www.google.com , even to google.com.

Did I do something wrong..?

I think protocol (http://) is what breaks it. Try using "www.google.com" as a host name. HTTP CLient tutorial is here .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM