HttpClient不是一个浏览器,而是一个客户端HTTP传输类库。HttpClient作用是传输和接收HTTP消息。在HttpClient模块中用到了两个重要的类:HttpGet和HttpPost。这两个类分别用来提交HTTPGET和HTTPPOST请求。
接下来我们将使用以上的理论作为基础,然后进行一次连接http://www.ip138.com/ips138.asp查询ip所在地的实践:
首先我们要下的jar包:
httpclient-4.2.jar httpclient-cache-4.2.jar httpcore-4.2.jar httpmime-4.2.jar commons-logging-1.1.jar
我的查询ip的源码如下:
package com.java; import java.io.IOException; import org.apache.http.HttpEntity; import org.apache.http.HttpResponse; import org.apache.http.client.methods.HttpGet; import org.apache.http.impl.client.DefaultHttpClient; import org.apache.http.util.EntityUtils; import org.htmlparser.NodeFilter; import org.htmlparser.Parser; import org.htmlparser.filters.TagNameFilter; import org.htmlparser.util.NodeList; public class iptest { public static String ip2addr(String ip) throws Exception { DefaultHttpClient httpclient = new DefaultHttpClient(); String html = ""; try { HttpGet httpget = null; //创建HttpGet对象 httpget = new HttpGet("http://www.ip138.com/ips138.asp?ip=" + ip + "&action=2"); //使用execute方法发送HTTPGET请求,并返回HttpResponse对象 HttpResponse response = httpclient.execute(httpget); //使用getEntity方法获得返回结果 HttpEntity entity = response.getEntity(); //读取response响应内容 html = EntityUtils.toString(entity,"GB2312"); //关闭底层流 EntityUtils.consume(entity); } catch (IOException e) { throw e; } finally { httpclient.getConnectionManager().shutdown(); } /** * 利用Parser解析HTML,将标签<li>下的内容存储到nodeList里,并获取第一个<li>下的内容,用split分割后获取最终的结果是 日本 */ Parser myParser =Parser.createParser(html, "gb2312"); NodeFilter filter =new TagNameFilter ("li"); NodeList nodeList =myParser.parse(filter); System.out.println(nodeList); String result = nodeList.elementAt(0).toPlainTextString(); System.out.println(result); String address = result.split(":")[1]; return address; } public static void main(String[] args) { try { ip2addr("111.111.111.111"); } catch (Exception e) { System.out.println("网络异常"); } } }
程序执行结果:日本,可见111.111.111.111是小日本的ip。