首页 电脑 电脑学堂 查看内容

httpclient抓取网页内容

2010-9-10 15:45 863 0

摘要:     1.想下载远程URL地址的内容。可以使用httpclient现在整理一下相关的代码:     而且解决中文乱码问题  ...
关键词: nbsp String IOException httpclient convertStreamToS get new printStackTrace BufferedReader contentString

    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 + " ");     }     } 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;     }
声明:文章版权归原作者所有 部分文章转自互联网 如有侵权请联系 [邮箱地址] 删除

路过

雷人

握手

鲜花

鸡蛋

最新评论

返回顶部