Java 网络编程
java.net 包中提供了两种常见的网络协议的支持:
1.TCP:TCP 是传输控制协议的缩写,它保障了两个应用程序之间的可靠通信。通常用于互联网协议,被称 TCP / IP。
2.UDP:UDP 是用户数据报协议的缩写,一个无连接的协议。提供了应用程序之间要发送的数据的数据包。
本教程主要讲解以下两个主题。
Socket 编程:这是使用最广泛的网络概念,它已被解释地非常详细。
URL 处理:这部分会在另外的篇幅里讲,点击这里更详细地了解在 Java 语言中的 URL 处理。
Java URL处理
URL(Uniform Resource Locator)中文名为统一资源定位符,有时也被俗称为网页地址。表示为互联网上的资源,如网页或者FTP地址。
URL可以分为如下几个部分。
protocol://host:port/path?query#fragment
protocol(协议)可以是 HTTP、HTTPS、FTP 和 File,port 为端口号,path为文件路径及文件名。
HTTP 协议的 URL 实例如下:http://www.runoob.com/index.html?language=cn#j2se
URL 解析:
协议为(protocol):http
主机为(host:port):www.runoob.com
端口号为(port): 80 ,以上URL实例并未指定端口,因为 HTTP 协议默认的端口号为 80。
文件路径为(path):/index.html
请求参数(query):language=cn
定位位置(fragment):j2se,定位到网页中 id 属性为 j2se 的 HTML 元素位置 。
try {
URL url = new URL("http://www.runoob.com/index.html?language=cn#j2se"); System.out.println("URL 为:" + url.toString());
System.out.println("协议为:" + url.getProtocol());
System.out.println("验证信息:" + url.getAuthority());
System.out.println("文件名及请求参数:" + url.getFile());
System.out.println("主机名:" + url.getHost());
System.out.println("路径:" + url.getPath());
System.out.println("端口:" + url.getPort());
System.out.println("默认端口:" + url.getDefaultPort());
System.out.println("请求参数:" + url.getQuery());
System.out.println("定位位置:" + url.getRef());
}catch(IOException e) {
e.printStackTrace();
}
URLConnections 类方法
openConnection() 返回一个 java.net.URLConnection。
例如:
如果你连接HTTP协议的URL, openConnection() 方法返回 HttpURLConnection 对象。
如果你连接的URL为一个 JAR 文件, openConnection() 方法将返回 JarURLConnection 对象。
等等...
URLConnection 方法列表如下:
序号 | 方法描述 |
|
1 | Object getContent() | 检索URL链接内容 |
2 | Object getContent(Class[] classes) | 检索URL链接内容 |
3 | String getContentEncoding() | 返回头部 content-encoding 字段值。 |
4 | int getContentLength() | 返回头部 content-length字段值 |
5 | String getContentType() | 返回头部 content-type 字段值 |
6 | int getLastModified() | 返回头部 last-modified 字段值。 |
7 | long getExpiration() | 返回头部 expires 字段值。 |
8 | long getIfModifiedSince() | 返回对象的 ifModifiedSince 字段值。 |
9 | public void setDoInput(boolean input) | URL 连接可用于输入和/或输出。如果打算使用 URL 连接进行输入,则将 DoInput 标志设置为 true;如果不打算使用,则设置为 false。默认值为 true。 |
10 | public void setDoOutput(boolean output) | URL 连接可用于输入和/或输出。如果打算使用 URL 连接进行输出,则将 DoOutput 标志设置为 true;如果不打算使用,则设置为 false。默认值为 false。 |
11 | public InputStream getInputStream() throws IOException | 返回URL的输入流,用于读取资源 |
12 | public OutputStream getOutputStream() throws IOExcepti | 返回URL的输出流, 用于写入资源。 |
13 | public URL getURL() | 返回 URLConnection 对象连接的URL |
实例
以下实例中URL采用了HTTP 协议。 openConnection 返回HttpURLConnection对象。
try {
URL url = new URL("http://www.runoob.com");
URLConnection urlConnection = url.openConnection();
HttpURLConnection connection = null;
if(urlConnection instanceof HttpURLConnection) {
connection = (HttpURLConnection) urlConnection;
} else {
System.out.println("请输入 URL 地址"); return;
}
BufferedReader in = new BufferedReader( new InputStreamReader(connection.getInputStream()));
String urlString = "";
String current;
while((current = in.readLine()) != null) {
urlString += current;
}
System.out.println(urlString);
}catch(IOException e) { e.printStackTrace(); }