Java 读取网页简易程序
2016-05-12 09:17
375 查看
package com.ibm.downloadtool;
import java.io.BufferedInputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Scanner;
public class DownLoadTool {
public static void main(String[] args) {
// TODO Auto-generated method stub
DownLoadTool d = new DownLoadTool();
d.downLoadUrl("http://finance.sina.com.cn/");
}
//download
public String downLoadUrl(String addr){
StringBuffer sb=new StringBuffer();
try {
URL url=new URL(addr);
HttpURLConnection con= (HttpURLConnection) url.openConnection();
con.setConnectTimeout(1000);
con.connect();
System.out.println(con.getResponseCode());
if(con.getResponseCode()==200){
BufferedInputStream bis=new BufferedInputStream(con.getInputStream());
Scanner sc=new Scanner(bis,"GBK");
while(sc.hasNextLine()){
sb.append(sc.nextLine());
}
sc.close();
}
} catch (Exception e) {
e.printStackTrace();
}
System.out.println(sb.toString());
return sb.toString();
}
}
import java.io.BufferedInputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Scanner;
public class DownLoadTool {
public static void main(String[] args) {
// TODO Auto-generated method stub
DownLoadTool d = new DownLoadTool();
d.downLoadUrl("http://finance.sina.com.cn/");
}
//download
public String downLoadUrl(String addr){
StringBuffer sb=new StringBuffer();
try {
URL url=new URL(addr);
HttpURLConnection con= (HttpURLConnection) url.openConnection();
con.setConnectTimeout(1000);
con.connect();
System.out.println(con.getResponseCode());
if(con.getResponseCode()==200){
BufferedInputStream bis=new BufferedInputStream(con.getInputStream());
Scanner sc=new Scanner(bis,"GBK");
while(sc.hasNextLine()){
sb.append(sc.nextLine());
}
sc.close();
}
} catch (Exception e) {
e.printStackTrace();
}
System.out.println(sb.toString());
return sb.toString();
}
}
相关文章推荐
- java解析xml文件(三种方式-(dom ,jdom ,dom4j)
- java 对象序列化
- 【JDK】:Java容器框架
- Eclipse 启动提示java虚拟机启动失败,“Failed to create the Java Virtual Machine”-------解决方法
- JAVA代码发送邮件示例和解释
- struts2 的简单配置总结
- javar日志
- Java之使用json-lib进行对象与json字符串之间的转换
- java 读文件中文乱码
- java jxl导出excel
- Spring对ORM的支持之集成Hibernate3
- Spring MVC的RequestParam注解
- Myeclipse中SVN版本与客户端版本出现冲突问题与解决方法
- java字符串的操作
- Spring 中的 FactoryBean
- Java实现序列化与反序列化的简单示例
- Java中最高效的读取字节文件的方法
- Java web系列----JDBC
- JAVA中如何用shuffle打乱列表并生成乱序序列
- 2015-2016-2 《Java程序设计》 游戏化