public class urlDemo {
@Test//URL
public void t1() throws IOException{
try {
URL url = new URL("http://www.hncu.net");
InputStream in = url.openStream();//读出所有的内容
BufferedReader br = new BufferedReader( new InputStreamReader(in));
String str="";
while( (str=br.readLine())!=null){
byte buf[]=str.getBytes("gbk");//转换一下编码类型
String str2=new String(buf,0,buf.length,"utf-8");
System.out.println(str2);
}
br.close();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
@Test//URLConnection---相应头信息
public void t2(){
try {
URL url = new URL("http://www.hncu.net");
URLConnection con = url.openConnection();//获得连接的权限
String code = con.getContentEncoding();//获得编码类型
System.out.println("code:"+code);
int len = con.getContentLength();//获得content的字符长度
System.out.println("len:"+len);
Date d = new Date(con.getLastModified());//最后一次修改的时间
System.out.println(d.toString());
//
con.setDoOutput(true);
//
OutputStream out = con.getOutputStream();
//
for(int i=0;i<10000;i++){
//
out.write(i);
//
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
@Test//IP地址的封装类
public void t3() throws IOException{
InetAddress ip = InetAddress.getByName("www.hncu.net");//不用输入http://
System.out.println(ip.getHostName());//主机名
System.out.println(ip.getHostAddress());//IP地址
}
}