Java 獲取網站源碼
2016-03-23 21:34
495 查看
複製可用。
/** * Get a page's source code * @param strUrl Url of this page * @param charset Charset of this page, like "utf-8","gbk" * @return * @throws IOException */ public String fetchHtml(String strUrl, String charset) throws Exception { if (!strUrl.startsWith("http://")) strUrl = "http://" + strUrl; URL url = new URL(strUrl); HttpURLConnection con = (HttpURLConnection) url.openConnection(); InputStream is = con.getInputStream(); InputStreamReader isr = new InputStreamReader(is, charset); String html = ""; int read; for (; (read = isr.read()) != -1; ) html += (char) read; isr.close(); return html; }
相关文章推荐
- 学习Java画板
- 蓝桥杯比赛题目集锦:JAVA组
- java大数(BigInteger)
- Java异常处理总结
- 《java中局部变量和成员变量的区别》
- java.io.InvalidClassException: javax.management.MBeanAttributeInfo; local class incompatible:
- JAVA面向对象程序设计中多态特性的概述
- 深入理解java socket编程
- 小鸦-spring AOP的小模拟
- Java四舍五入
- java Dom 解析xml文档,实现xml文档结点的增删改查,遍历
- 使用Idea创建ssm项目,SpringMVC+Spring+MyBatis+Maven整合
- Spring3.1 Cache注解
- Java中的TreeMap、Comparable、Comparator
- kettle-step插件结构
- hibernate级联删除
- hdu2041java
- spring-mybatis整合(配置文件)
- spring学习笔记(15)趣谈spring 事件:实现业务逻辑解耦,异步调用提升用户体验
- javaEE SSH框架不使用注解的方式与使用注解的方式