创建URL对象需对中文参数用URLEncoder.encode()编码,URLConnection发POST须先setDoOutput(true)再写入;读响应前必查getResponseCode(),非2xx用getErrorStream();须设超时并关闭流。

URL对象创建时要注意协议和编码
直接用 new URL("https://example.com/path?name=张三") 会抛 MalformedURLException,因为中文参数没编码。必须先用 URLEncoder.encode("张三", "UTF-8") 处理查询参数,再拼接字符串。常见错误是只对值编码却漏掉键名(如 name=... 中的 name 不需要),或混用 encode() 和 encodeComponent() —— Java 里只有 URLEncoder.encode(String, String),没有后者。
URLConnection默认禁用输出,POST请求要手动打开
调用 url.openConnection() 返回的是 URLConnection,不是 HttpURLConnection,但大多数场景下可安全强转。关键点:即使你想发 POST,setDoOutput(true) 必须在获取连接后、写入前调用;否则会报 IllegalStateException: Already connected。同时记得设请求方法:
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("POST");
conn.setDoOutput(true);
conn.setRequestProperty("Content-Type", "application/json; charset=UTF-8");
读响应体前必须检查响应码,否则可能读到重定向页面
conn.getResponseCode() 返回 302 或 401 时,conn.getInputStream() 仍可能返回内容(比如登录页 HTML),而不是你期望的 JSON。正确流程是:
- 先调
getResponseCode() - 若非
2xx,改用getErrorStream()读错因 - 别依赖
getContentType()判断是否成功——它可能返回text/html即使状态码是500
超时和连接复用容易被忽略
默认连接和读取无限等待,生产环境必须显式设置:
conn.setConnectTimeout(5000); conn.setReadTimeout(10000);另外,
URLConnection 默认启用 HTTP Keep-Alive,但不会自动管理连接池。频繁请求时,不关流会导致文件描述符耗尽;务必在 finally 块中关闭 InputStream 或 OutputStream。JDK 9+ 虽支持 try-with-resources,但 URLConnection 本身不实现 AutoCloseable,只能关它的流。










