设为首页 加入收藏

TOP

httpclient抓取网页内容
2014-11-23 17:39:06 】 浏览:851
Tags:httpclient 网页 内容

  1.想下载远程URL地址的内容。可以使用httpclient现在整理一下相关的代码:


  方法一:流转码


  public String convertStreamToString(InputStream is) throws UnsupportedEncodingException {


  BufferedReader reader = new BufferedReader(new InputStreamReader(is,"gbk"));


  StringBuilder sb = new StringBuilder();


  String line = null;


  try {


  while ((line = reader.readLine()) != null) { sb.append(line + "\n");


  }


  } catch (IOException e) {


  e.printStackTrace();


  } finally {


  try {


  is.close();


  } catch (IOException e) {


  e.printStackTrace();


  }


  }


  return sb.toString();


  }


  //下载内容


  private String urlContent(String urlString) throws HttpException, IOException {


  HttpClient client = new HttpClient();


  GetMethod get = new GetMethod("http://www.tianya.cn/publicforum/articleslist/0/no20.shtml"); client.executeMethod(get); System.out.print(get.getResponseCharSet()); InputStream iStream = get.getResponseBodyAsStream();


  String contentString = convertStreamToString(iStream);


  get.releaseConnection();


  return contentString;


  }


  通过GET方法能够实现下载网页内容出来的


  编辑特别推荐:


】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇HTMLParser学习整理常用的解析代码 下一篇jsp+beans实现读取数据库的例子

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目