Java中提供了专门的网络开发程序包java.net,提供了两种通信协议:TCP和UDP(Java-网络编程TCP和UDP)。这里主要讲解一下常用的网络编程类InetAddr
和URL
。
InetAddress类
InetAddress
:封装我们上篇博客中数字式的IP地址和该地址的域名。对于InetAddress,三个方法:getLocalHost()、getByName()以及getAllByName()可以用来创建InetAddress的实例。
1、getLocalHost():返回象征本地主机的InetAddress对象。
2、getByName():返回一个传给它的主机名的InetAddress,无法解析引发UnknownHostException
3、getAllByName():工厂方法返回代表由一个特殊名称分解的所有地址的InetAddresses类数组。不能分解时引发UnknownHostException异常。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| InetAddress ip = InetAddress.getByName("www.baidu.com");
System.out.println("baidu是否可达:" + ip.isReachable(2000));
System.out.println(ip.getHostAddress());
InetAddress local = InetAddress.getByAddress(new byte[]{127,0,0,1});
System.out.println(local.getCanonicalHostName());
|
URL
URL
(Uniform Resource Locator)统一资源定位符,可以直接使用此类找到互联网上的资源
。目前使用最为广泛的TCP/IP中对于URL中主机名的解析也是协议的一个标准,即所谓的域名解析服务
。
URL和URLConnection
URL
获取资源,比如URL的InputStream对象资源的信息,以及一个到URL所引用远程对象的连接URLConnection。 URLConnection对象可以向所代表的URL发送请求和读取URL的资源,每次调用URL 的openConnection方法都打开一个新的连接。创建一个URL的连接,需要如下几个步骤:
创建URL对象
,并通过openConnection方法获得URLConnection
对象;
设置URLConnection参数和普通请求属性;
向远程资源发送请求;
访问远程资源的头字段和通过输入流来读取远程资源返回的信息。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75
| import java.io.*; import java.net.URL; import java.net.URLConnection;
public class URLDemo01 {
static final String URL_ = "http://blog.xiaoxiaomo.com" ; static final String param = "" ; public static void main(String[] args) throws IOException { myGet(); }
public static void myGet() throws IOException { URL url = new URL(URL_);
URLConnection conn1 = url.openConnection();
conn1.setRequestProperty("accept", "*/*"); conn1.setRequestProperty("connection", "Keep-Alive"); conn1.setRequestProperty("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1)");
conn1.connect();
BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream())); String line; while ((line = in.readLine()) != null) System.out.println(line);
in.close(); }
public static void myPOST() throws IOException { URL url2 = new URL(URL_);
URLConnection conn2 = url2.openConnection();
conn2.setRequestProperty("accept", "*/*"); conn2.setRequestProperty("connection", "Keep-Alive"); conn2.setRequestProperty("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1)");
conn2.setDoOutput(true); conn2.setDoInput(true);
PrintWriter out = new PrintWriter(conn2.getOutputStream());
InputStream is = conn2.getInputStream();
byte[] bytes = new byte[1024*10] ; int len ; while ( ( len = is.read(bytes) ) != -1 ){ System.out.println(new String(bytes , 0 ,len )); } } }
|
URLDecoder和URLEncoder
URLDecoder
:HTML格式编码的实用工具类。该类包含了将String转换为application/x-www-form-urlencoded MIME
格式的静态方法。
String转换格式:
1.字符”a”-“z”,”A”-“Z”,”0”-“9”,”.”,”-“,”*”,和”_” 都不会被编码;
2.将空格转换为加号 (+) ;
3.将非文本内容转换成”%xy”的形式,xy是两位16进制的数值;
4.在每个 name=value 对之间放置 & 符号。
URLEncoder
:该类包含了将String从application/x-www-form-urlencoded MIME
格式解码的静态方法。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
| import java.io.UnsupportedEncodingException; import java.net.URLDecoder; import java.net.URLEncoder;
public class URLDEENcoder { public static void main(String[] args) { try {
String encodeStr = URLEncoder.encode("小小默", "utf-8"); System.out.println("编码:" + encodeStr);
String decodeStr = URLDecoder.decode(encodeStr, "utf-8"); System.out.println("解码:" + decodeStr);
} catch (UnsupportedEncodingException e) { e.printStackTrace(); } } }
|
- 运行结果
1 2
| 编码:%E5%B0%8F%E5%B0%8F%E9%BB%98 解码:小小默
|
上一篇:Java-NIO之概述
下一篇:Java--网络编程TCP和UDP