Java 实例 - 获取 URL 响应头信息
以下实例演示了如何获取指定 URL 的响应头信息:
import java.io.IOException; import java.net.URL; import java.net.URLConnection; import java.util.Map; import java.util.Set; public class Main { public static void main(String[] args) throws IOException{ URL url = new URL("http://www.nowcoder.com"); URLConnection conn = url.openConnection(); Map headers = conn.getHeaderFields(); Set<String> keys = headers.keySet(); for( String key : keys ){ String val = conn.getHeaderField(key); System.out.println(key+" "+val); } System.out.println( conn.getLastModified() ); } }
以上代码运行输出结果为:
Transfer-Encoding chunked null HTTP/1.1 200 OK Server Tengine/1.3.0 Connection keep-alive Vary Cookie Date Mon, 04 May 2015 03:54:05 GMT X-Pingback http://www.nowcoder.com/xmlrpc.php X-Powered-By PHP/5.3.15 Content-Type text/html; charset=UTF-8
Java 实例 - 解析 URL
以下实例演示了如何使用 net.URL 类的 url.getProtocol() ,url.getFile() 等方法来解析 URL 地址:
import java.net.URL; public class Main { public static void main(String[] args) throws Exception { URL url = new URL("http://www.nowcoder.com/html/html-tutorial.html"); System.out.println("URL 是 " + url.toString()); System.out.println("协议是 " + url.getProtocol()); 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()); } }
以上代码运行输出结果为:
URL 是 http://www.nowcoder.com/html/html-tutorial.html 协议是 http 文件名是 /html/html-tutorial.html 主机是 www.nowcoder.com 路径是 /html/html-tutorial.html 端口号是 -1 默认端口号是 80
Java 实例 - ServerSocket 和 Socket 通信实例
以下实例演示了如何实现客户端发送消息到服务器,服务器接收到消息并读取输出,然后写出到客户端客户端接收到输出。
1、建立服务器端
服务器建立通信ServerSocket
服务器建立Socket接收客户端连接
建立IO输入流读取客户端发送的数据
建立IO输出流向客户端发送数据消息
服务器端代码:
import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.IOException; import java.io.InputStreamReader; import java.io.OutputStreamWriter; import java.net.ServerSocket; import java.net.Socket; public class Server { public static void main(String[] args) { try { ServerSocket ss = new ServerSocket(8888); System.out.println("启动服务器...."); Socket s = ss.accept(); System.out.println("客户端:"+s.getInetAddress().getLocalHost()+"已连接到服务器"); BufferedReader br = new BufferedReader(new InputStreamReader(s.getInputStream())); //读取客户端发送来的消息 String mess = br.readLine(); System.out.println("客户端:"+mess); BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(s.getOutputStream())); bw.write(mess+"\n"); bw.flush(); } catch (IOException e) { e.printStackTrace(); } } }
以上代码运行输出结果为:
启动服务器....
2、建立客户端
创建Socket通信,设置通信服务器的IP和Port
建立IO输出流向服务器发送数据消息
建立IO输入流读取服务器发送来的数据消息
客户端代码:
import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.OutputStream; import java.io.OutputStreamWriter; import java.net.Socket; import java.net.UnknownHostException; public class Client { public static void main(String[] args) { try { Socket s = new Socket("127.0.0.1",8888); //构建IO InputStream is = s.getInputStream(); OutputStream os = s.getOutputStream(); BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(os)); //向服务器端发送一条消息 bw.write("测试客户端和服务器通信,服务器接收到消息返回到客户端\n"); bw.flush(); //读取服务器返回的消息 BufferedReader br = new BufferedReader(new InputStreamReader(is)); String mess = br.readLine(); System.out.println("服务器:"+mess); } catch (UnknownHostException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } }
以上代码运行输出结果为:
服务器:测试客户端和服务器通信,服务器接收到消息返回到客户端