package cn.bl.net;

import java.io.InputStream;
import java.net.URL;

import org.junit.Test;

public class Demo1_URL {
@Test
public void test() {
URL url;
try {
url = new URL("https:/
System.out.println(url.toString());//也就是这个网址
System.out.println(url.getPort());//没设置就返回-1
System.out.println(url.getDefaultPort());//返回默认端口 -- 443
System.out.println(url.getProtocol());//https - 协议
System.out.println(url.getHost());/

InputStream in = url.openStream();//将网页的html读取出来了
byte[]b = new byte[512];
int len = -1;
while((len = in.read(b)) !=-1) {
System.out.println(new String(b, 0, len));
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
package cn.bl.net;

import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;

import org.junit.Test;

public class Demo2_URLConnection {
@Test
public void test() {
try {
URL url = new URL
URLConnection con = url.openConnection();
System.out.println("返回资源文件的长度:"+con.getContentLength());
System.out.println("返回资源文件的类型:"+con.getContentType());
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
package cn.bl.net;

import java.net.InetAddress;

import org.junit.Test;

public class Deom3_InetAddress {
@Test
public void test() throws Exception {
InetAddress address = InetAddress.getByName
System.out.println(address.getHostAddress());//39.96.132.69

System.out.println(address.isSiteLocalAddress());//false
System.out.println(address.getHostName());
}
}