Java中使用URL(Uniform Resource Locator)可以访问不同协议的资源。常见的协议有HTTP、FTP、Telnet等,在这里我们着重讲解HTTP协议。
一、URL的构成
URL由三部分组成:协议、主机和路径。例如:http://www.example.com/index.html。其中,http是协议,www.example.com是主机,index.html是路径。除此之外,URL还可以有查询参数和锚点。
下面我们通过代码实现获取URL的各个部分:
import java.net.URL; public class URLDemo { public static void main(String[] args) throws Exception { URL url = new URL("http://www.example.com/index.html?name=yiibai#example"); System.out.println("协议:" + url.getProtocol()); System.out.println("主机:" + url.getHost()); System.out.println("路径:" + url.getPath()); System.out.println("查询参数:" + url.getQuery()); System.out.println("锚点:" + url.getRef()); } }
二、URL的解析
在实际应用中,我们常常需要对URL进行解析,获取其中的参数。URL的解析可以使用java.net.URLDecoder类。下面通过代码演示解析URL:
import java.net.URLDecoder; import java.util.HashMap; import java.util.Map; public class URLParser { public static Mapparse(String url) throws Exception { Map params = new HashMap<>(); int index = url.indexOf('?'); if (index != -1) { String query = url.substring(index + 1); String[] pairs = query.split("&"); for (String pair : pairs) { int pos = pair.indexOf('='); if (pos != -1) { params.put(URLDecoder.decode(pair.substring(0, pos), "UTF-8"), URLDecoder.decode(pair.substring(pos + 1), "UTF-8")); } } } return params; } public static void main(String[] args) throws Exception { String url = "http://www.example.com/login?username=yiibai&password=123456"; Map params = URLParser.parse(url); System.out.println("用户名:" + params.get("username")); System.out.println("密码:" + params.get("password")); } }
三、URL的请求
使用Java可以方便地实现对URL的请求,可以使用HttpURLConnection类。下面通过代码演示对URL进行GET请求:
import java.io.IOException; import java.io.InputStream; import java.net.HttpURLConnection; import java.net.URL; import java.util.Scanner; public class HttpDemo { public static String getRequest(String urlString) throws IOException { URL url = new URL(urlString); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); conn.setRequestMethod("GET"); conn.setConnectTimeout(5000); InputStream is = conn.getInputStream(); Scanner scanner = new Scanner(is, "UTF-8"); StringBuilder sb = new StringBuilder(); while (scanner.hasNextLine()) { sb.append(scanner.nextLine()); } scanner.close(); conn.disconnect(); return sb.toString(); } public static void main(String[] args) throws IOException { String url = "http://www.example.com/index.html"; String content = getRequest(url); System.out.println(content); } }
如果需要发送POST请求,可以调用HttpURLConnection的setRequestMethod方法,并且需要设置Content-Type和Content-Length:
import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.net.HttpURLConnection; import java.net.URL; import java.nio.charset.StandardCharsets; import java.util.Scanner; public class HttpDemo { public static String postRequest(String urlString, String body) throws IOException { URL url = new URL(urlString); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); conn.setRequestMethod("POST"); conn.setConnectTimeout(5000); conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded"); conn.setRequestProperty("Content-Length", Integer.toString(body.getBytes(StandardCharsets.UTF_8).length)); conn.setDoOutput(true); OutputStream os = conn.getOutputStream(); os.write(body.getBytes(StandardCharsets.UTF_8)); os.close(); InputStream is = conn.getInputStream(); Scanner scanner = new Scanner(is, "UTF-8"); StringBuilder sb = new StringBuilder(); while (scanner.hasNextLine()) { sb.append(scanner.nextLine()); } scanner.close(); conn.disconnect(); return sb.toString(); } public static void main(String[] args) throws IOException { String url = "http://www.example.com/login"; String body = "username=yiibai&password=123456"; String content = postRequest(url, body); System.out.println(content); } }
四、URL的拼接
有时候我们需要动态生成URL,可以使用StringBuilder来拼接URL。下面通过代码演示:
public class URLBuilder { public static String build(String baseUrl, Mapparams) { StringBuilder sb = new StringBuilder(); sb.append(baseUrl); sb.append('?'); for (Map.Entry entry : params.entrySet()) { sb.append(entry.getKey()); sb.append('='); sb.append(entry.getValue()); sb.append('&'); } sb.deleteCharAt(sb.length() - 1); return sb.toString(); } public static void main(String[] args) { String baseUrl = "http://www.example.com/search"; Map params = new HashMap<>(); params.put("q", "Java"); params.put("page", "1"); String url = URLBuilder.build(baseUrl, params); System.out.println(url); } }
五、URL的编码
URL中的参数需要进行编码,避免出现特殊字符。Java内置了URLEncoder和URLDecoder类,可以用于URL的编码和解码。下面通过代码演示:
import java.io.UnsupportedEncodingException; import java.net.URLEncoder; public class URLEncodeDemo { public static void main(String[] args) throws UnsupportedEncodingException { String url = "http://www.example.com/search?q=Java 编程"; String encodedUrl = URLEncoder.encode(url, "UTF-8"); System.out.println(encodedUrl); } }
需要注意的是,对于URL中的路径部分,只需要对特殊字符进行编码,而对于查询参数部分,需要对整个参数字符串进行编码。