java网络编程 url
package com.dasenlin.url;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
public class URLDemo {
public static void main(String[] args) {
try {
URL url = new URL("https://www.baidu.com");
InputStream input = url.openStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(input, "utf-8"));
while(true){
String s = reader.readLine();
if(s==null){
break;
}
System.out.println(s);
}
reader.close();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
package com.dasenlin.url;
import java.net.InetAddress;
import java.net.UnknownHostException;
public class InterAddressDemo {
public static void main(String[] args) {
try {
InetAddress ip = InetAddress.getLocalHost();
showIpInfo(ip);
System.out.println("-----");
ip=InetAddress.getByName("www.baidu.com");
showIpInfo(ip);
System.out.println();
InetAddress [] ips = InetAddress.getAllByName("www.baidu.com");
for(InetAddress i:ips){
System.out.println(i.getHostAddress());
}
} catch (UnknownHostException e) {
e.printStackTrace();
}
}
private static void showIpInfo(InetAddress ip){
System.out.println(ip.getHostName());
System.out.println(ip.getHostAddress());
}
}