1.httpclient采用的maven依赖html
<dependency> <groupId>commons-httpclient</groupId> <artifactId>commons-httpclient</artifactId> <version>3.1</version> </dependency>
2.使用步骤maven
新建一个 HttpClient对象,做为客户端post
新建请求方法对象,好比GetMethod 或 PostMethodurl
客户端执行所建立的GetMethod或PostMethodcode
获取返回的数据htm
释放链接对象
3.代码展现get
public static void main(String[] args) { HttpClient httpClient = new HttpClient(); //建立客户端 String url ="http://news.163.com/16/0602/10/BOI4LUB400014PRF.html"; GetMethod getMethod = new GetMethod(url); try { int statusCode = httpClient.executeMethod(getMethod); if (statusCode != HttpStatus.SC_OK) { //执行成功的标示状态 System.err.println("Method failed: " + getMethod.getStatusLine()); } // 读取内容 byte[] responseBody = getMethod.getResponseBody(); // String res = getMethod.getResponseBodyAsString(); /* //Post PostMethod postMethod = new PostMethod(url); httpClient.executeMethod(postMethod); System.out.println(postMethod.getResponseBodyAsString());*/ 处理内容 String html = new String(responseBody); System.out.printlin(html); } catch (Exception e) { System.err.println("页面没法访问"); }finally{ //不管成功与否都要释放链接 getMethod.releaseConnection(); } }