阅读更多
=======================================URL常用方法
//创建一个URL实例
URL imooc = new URL("http://www.imooc.com");
//?后面表示参数,#后面表示锚点
URL url = new URL(imooc, "/search/course?words=java");
System.out.println("协议:"+url.getProtocol());
System.out.println("主机:"+url.getHost());
System.out.println("端口:"+url.getPort());//没有指定端口号,默认使用80端口号,但是该方法返回-1
System.out.println("文件路径:"+url.getPath());
System.out.println("文件名:"+url.getFile());
System.out.println("相对路径:"+url.getRef());
System.out.println("查询字符串:"+url.getQuery());
=======================================获取网页数据实例
package com.kai;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
/**
* 使用URL读取网页内容
* */
public class ReadHtmlByURL {
private static String getHtmlByURl(String urlTemp){
try {
URL url = new URL(urlTemp);
//通过url.openStream()获取URL对象的资源字节输入流
InputStream is = url.openStream();
//将字节输入流转换为字符输入流
InputStreamReader isr = new InputStreamReader(is,"utf-8");
//为字符输入流添加缓冲
StringBuffer sb = new StringBuffer();
BufferedReader bfRead = new BufferedReader(isr);
//读取数据
String data = bfRead.readLine();
while(data!=null){
sb.append(data);
//输出数据,继续读取下一行数据
System.out.println(data);
data = bfRead.readLine();
}
//关闭资源
bfRead.close();
isr.close();
is.close();
return sb.toString();
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
public static void main(String[] args) {
String html=getHtmlByURl("http://www.imooc.com/search/course?words=java");
System.out.println(html);
}
}