JSP学习笔记(七十五):使用HttpClient远程抓取网页内容
2013-11-22 09:48
633 查看
1.准备工作
需要下载两个jar包:commons-httpclient和commons-codes
commons-httpclient下载地址:http://hc.apache.org/downloads.cgi 4.0版的现在还没有正式版,我下的是3.1版的
commons-codes下载地址:http://commons.apache.org/downloads/download_codec.cgi
2.获取一个网页
⑴说明一
GetMethod getMethod = new GetMethod(http://www.baidu.com/);
这是一个完整的网页路径请求,也可以先设置Host,然后使用路径直接访问:
httpClient.getHostConfiguration().setHost("www.baidu.com", 80);
GetMethod getMethod = new GetMethod("/index.htm");
⑵说明二
// 读取内容
byte[] responseBody = getMethod.getResponseBody();
// 处理内容
html = new String(responseBody);
如果只是简单的获取页面html,可以直接这么使用:
html = getMethod.getResponseBodyAsString();
三.以Post的方式获取网页
上面的例子是以Get的方式获取网页,这个例子是以Post的方式
这个例子传递了两个Post参数:username为aaa,password为bbb,传递给页面http://127.0.0.1:8080/site/page.jsp
4.获取经过gzip的网页
5.获取图片或者其他二进制文件
6.解析获取的html
比较优秀的解析方式有htmlparser或者nekohtml,需要了解的请参考相关资料
7.模拟登录网站
很多站点都是需要身份验证的,通过简单的设置cookie可以达到这样的效果:
getMethod.addRequestHeader("Cookie","cookiename1=aa;cookiename2=bb");
当然也可以以Post登录系统的时候,获取cookie,动态的写上去,具体的就先不写了,这篇文章写的好累,先到这里吧。
参考资料:http://www.ibm.com/developerworks/cn/opensource/os-httpclient/
http://www.cnblogs.com/yesun/archive/2008/10/31/1323432.html
http://www.cnblogs.com/modou/articles/1325569.html
需要下载两个jar包:commons-httpclient和commons-codes
commons-httpclient下载地址:http://hc.apache.org/downloads.cgi 4.0版的现在还没有正式版,我下的是3.1版的
commons-codes下载地址:http://commons.apache.org/downloads/download_codec.cgi
2.获取一个网页
HttpClient httpClient = new HttpClient(); GetMethod getMethod = new GetMethod("http://www.baidu.com/"); try { int statusCode = httpClient.executeMethod(getMethod); if (statusCode != HttpStatus.SC_OK) { System.err.println("Method failed: " + getMethod.getStatusLine()); } // 读取内容 byte[] responseBody = getMethod.getResponseBody(); // 处理内容
String html = new String(responseBody);
System.out.println(html); } catch (Exception e) { System.err.println("页面无法访问"); } getMethod.releaseConnection();
⑴说明一
GetMethod getMethod = new GetMethod(http://www.baidu.com/);
这是一个完整的网页路径请求,也可以先设置Host,然后使用路径直接访问:
httpClient.getHostConfiguration().setHost("www.baidu.com", 80);
GetMethod getMethod = new GetMethod("/index.htm");
⑵说明二
// 读取内容
byte[] responseBody = getMethod.getResponseBody();
// 处理内容
html = new String(responseBody);
如果只是简单的获取页面html,可以直接这么使用:
html = getMethod.getResponseBodyAsString();
三.以Post的方式获取网页
上面的例子是以Get的方式获取网页,这个例子是以Post的方式
HttpClient httpClient = new HttpClient(); PostMethod postMethod = new PostMethod("http://127.0.0.1:8080/site/page.jsp"); postMethod.getParams().setParameter(HttpMethodParams.RETRY_HANDLER,new DefaultHttpMethodRetryHandler()); NameValuePair[] data = { new NameValuePair("username", "aaa"),new NameValuePair("password", "bbb") }; postMethod.setRequestBody(data); try { int statusCode = httpClient.executeMethod(postMethod); if (statusCode != HttpStatus.SC_OK) { System.err.println("Method failed: " + postMethod.getStatusLine()); } //读取内容 byte[] responseBody = postMethod.getResponseBody(); //处理内容 String html = new String(responseBody);
System.out.println(html); } catch (Exception e) { System.err.println("页面无法访问"); } postMethod.releaseConnection();
这个例子传递了两个Post参数:username为aaa,password为bbb,传递给页面http://127.0.0.1:8080/site/page.jsp
4.获取经过gzip的网页
HttpClient httpClient = new HttpClient(); httpClient.getHostConfiguration().setHost("127.0.0.1", 8080); GetMethod getMethod = new GetMethod("/page.jsp"); String acceptEncoding = ""; if (getMethod.getResponseHeader("Content-Encoding") != null) acceptEncoding = getMethod.getResponseHeader("Content-Encoding") .getValue(); if (acceptEncoding.toLowerCase().indexOf("gzip") > -1) { getMethod.setRequestHeader("Accept-Encoding", "gzip, deflate"); try { int statusCode = httpClient.executeMethod(getMethod); if (statusCode != HttpStatus.SC_OK) { System.err.println("Method failed: " + getMethod.getStatusLine()); } InputStream is = getMethod.getResponseBodyAsStream(); GZIPInputStream gzin = new GZIPInputStream(is); InputStreamReader isr = new InputStreamReader(gzin, "utf-8"); // 设置读取流的编码格式,自定义编码 java.io.BufferedReader br = new java.io.BufferedReader(isr); StringBuffer sb = new StringBuffer(); String tempbf; while ((tempbf = br.readLine()) != null) { sb.append(tempbf); sb.append("\r\n"); } isr.close(); gzin.close(); String html = sb.toString();
System.out.println(html); } catch (Exception e) { System.err.println("页面无法访问"); } getMethod.releaseConnection();
5.获取图片或者其他二进制文件
HttpClient httpClient = new HttpClient(); GetMethod getMethod = new GetMethod("http://127.0.0.1:8080/site/file.gif"); try { InputStream inputStream = getMethod.getResponseBodyAsStream(); // 这里处理 inputStream } catch (Exception e) { System.err.println("页面无法访问"); } getMethod.releaseConnection();
6.解析获取的html
比较优秀的解析方式有htmlparser或者nekohtml,需要了解的请参考相关资料
7.模拟登录网站
很多站点都是需要身份验证的,通过简单的设置cookie可以达到这样的效果:
getMethod.addRequestHeader("Cookie","cookiename1=aa;cookiename2=bb");
当然也可以以Post登录系统的时候,获取cookie,动态的写上去,具体的就先不写了,这篇文章写的好累,先到这里吧。
参考资料:http://www.ibm.com/developerworks/cn/opensource/os-httpclient/
http://www.cnblogs.com/yesun/archive/2008/10/31/1323432.html
http://www.cnblogs.com/modou/articles/1325569.html
相关文章推荐
- JSP学习笔记(七十五):使用HttpClient远程抓取网页内容
- 使用HttpClient远程抓取网页内容
- android 学习笔记 使用HttpClient
- 【JSP学习笔记】使用Servlet实现网页验证码
- 萌新的Python学习日记 - 爬虫无影 - 使用BeautifulSoup + css selector 抓取动态网页内容:Knewone
- Asp 使用 Microsoft.XMLHTTP 抓取网页内容无乱码处理,并过滤须要的内容
- 使用HttpClient+Jericho HTML Parser 实现网页抓取
- HttpClient抓取网页内容简单介绍
- HTTPCLIENT抓取网页内容
- python3.4学习笔记(十七) 网络爬虫使用Beautifulsoup4抓取内容
- 使用HttpClient远程抓取网页内容
- linux学习笔记-工程师技术:HTTP服务基础(Web服务基本搭建)、网页内容访问、安全Web服务、动态网站的部署
- 萌新的Python学习日记 - 爬虫无影 - 使用BeautifulSoup + css selector 抓取自己想要网页内容
- python3.4学习笔记(十三) 网络爬虫实例代码,使用pyspider抓取多牛投资吧里面的文章信息,抓取政府网新闻内容
- ThinkPHP Http工具类(用于远程采集 远程下载) phpSimpleHtmlDom采集类库_Jquery筛选方式 使用phpQuery轻松采集网页内容http://www.thinkphp.cn/extend/541.html
- Ajax学习笔记,原生Ajax,使用XMLHttpRequest读取xml内容
- HttpClient抓取网页内容简单介绍
- 使用HttpClient获得网页内容
- Asp 使用 Microsoft.XMLHTTP 抓取网页内容并过滤需要的