IP地址
每台计算机都有一个唯一表示自己的标记,这就是IP地址。
IP地址的表示方法
–
分类:
A类到B类中 缺少127.开始的地址,实际可以用127.0.0.1表示本机地址,或直接使用localhost表示本机地址
InetAddress
32位二进制的IP地址称为IPV4,由于IP地址不够使用了,所以才有了IPV6

使用例子:

URL and URLConnection
URL
直接用例子来理解,此例读取网站源代码
package 网络;
import java.net.*;
import java.util.Scanner;
import java.io.*;
public class URLDemo {
public static void main(String[] args) throws Exception
{
URL url = new URL("http","www.baidu.com",80,"/?tn=94112622_hao_pg");
InputStream input = url.openStream();
Scanner scan = new Scanner(input);
scan.useDelimiter("\n");
while(scan.hasNext())
{
System.out.println(scan.next());
}
}
}
URLConnection
例子胜于一切:
package 网络;
import java.net.*;
import java.util.Scanner;
import java.io.*;
public class URLDemo {
public static void main(String[] args) throws Exception
{
URL url = new URL("http://www.baidu.com");
URLConnection con = url.openConnection();
System.out.println("内容大小"+con.getContentLength());
System.out.println("内容大小"+con.getContentType());
}
}
URLEncoder and URLDecoder
例子:
package 网络;
import java.net.*;
import java.util.Scanner;
import java.io.*;
public class URLDemo {
public static void main(String[] args) throws Exception
{
String name ="杨";
String name1="yang";
String en= URLEncoder.encode(name, "UTF-8");
String en1= URLEncoder.encode(name1, "UTF-8");
System.out.println(en);
System.out.println(en1);
String de = URLDecoder.decode(en,"UTF-8");
String de2 = URLDecoder.decode(en,"UTF-8");
System.out.println(de+" \n"+de2);
}
/*{
URL url = new URL("http://www.baidu.com");
URLConnection con = url.openConnection();
System.out.println("内容大小"+con.getContentLength());
System.out.println("内容大小"+con.getContentType());
}*/
}
运行效果:
其实就是对输入的进行编码和解码。