介绍
写代码过程中发现自己对 Java 的一些 HTTP 请求方法不是很熟悉,也没有一个全局的概念框架,网上的一些博客使用的方法已经被 JDK 所抛弃。
于是简单整理下 Java 中常用的 HTTP 请求方法,以及它们的适用场景等,方便自己以后使用,本文基于 JDK 1.8。
这篇文章的作用很简单:
- 了解 Java 开发中常用的 HTTP 请求方法
- 了解这些方法的适用场景和注意事项
- 使用最新的方法,抛弃被添加 @Deprecated 注解的方法
1. 常用的 HTTP 请求方法
Socket:又称套接字,它工作在传输层,是所有应用层 HTTP 请求的底层实现。通过 ip 地址 + 端口号 即可实现网络通信
HttpURLConnection:继承了抽象类 URLConnection,源码中它定义了一些网络请求的返回码;也能够对 HTTP 请求做一些参数调整,例如使用 POST 请求、使用代理
HttpClient:是 Apache 下的子项目,使用时需要引入第三方 org.apache.httpcomponents.httpclient.jar 包,它对 HTTP 请求的一些方法封装的更多,造好的轮子,使用也更加方便
2. 各个 HTTP 请求方法示例
- Socket
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
| public static void SocketDemo(URL url,String content){
try { url = new URL("https://www.apiopen.top"); String path = "/weatherApi?city=%E6%AD%A6%E6%B1%89"; String host = url.getHost(); int port = 80; Socket socket = new Socket(host,port); OutputStreamWriter osw = new OutputStreamWriter(socket.getOutputStream(),"utf-8"); osw.write("GET " + path + " HTTP/1.1\r\n"); osw.write("Host: " + host + " \r\n"); osw.write("Connection: Keep-Alive\r\n"); osw.write("Content-Type: application/x-www-form-urlencoded; charset=utf-8 \r\n"); osw.write("\r\n"); BufferedReader reader = new BufferedReader(new InputStreamReader(socket.getInputStream())); String readLine; StringBuffer stringBuffer = new StringBuffer(); while ((readLine = reader.readLine()) != null) { stringBuffer.append(readLine); } System.out.println(stringBuffer.toString()); } catch (IOException e) { e.printStackTrace(); }
}
|
- HttpURLConnection
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
| public static void HttpURLConnection(URL url, String content) { try { HttpURLConnection connection = (HttpURLConnection) url.openConnection(); connection.setDoInput(true); connection.setDoOutput(true); connection.setUseCaches(false); connection.setRequestMethod("POST");
OutputStream outputStream = connection.getOutputStream(); outputStream.write(content.getBytes("UTF-8")); outputStream.close(); BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream())); String readLine; StringBuffer stringBuffer = new StringBuffer(); while ((readLine = reader.readLine()) != null) { stringBuffer.append(readLine); } System.out.println(stringBuffer.toString()); } catch (MalformedURLException e) { e.printStackTrace();
} catch (IOException e) { e.printStackTrace(); } }
|
- HttpClient
1 2 3 4 5 6 7 8 9 10
| public static void HTTPClient(URL url,String content){ try { HttpClient client = HttpClients.createDefault(); HttpPost post = new HttpPost(url.toString()); CloseableHttpResponse response = (CloseableHttpResponse) client.execute(post); System.out.println(EntityUtils.toString(response.getEntity())); } catch (IOException e) { e.printStackTrace(); } }
|
Main 方法
1 2 3 4 5 6 7 8 9 10 11 12
| public static void main(String[] args) { try { URL url = new URL("https://www.apiopen.top/weatherApi?city=%E6%AD%A6%E6%B1%89"); String content = "123"; HttpURLConnection(url, content); HTTPClient(url,content); SocketDemo(url,content);
} catch (MalformedURLException e) { e.printStackTrace(); } }
|
3. 总结
上面各个方法适用场景