目录

Life in Flow

知不知,尚矣;不知知,病矣。
不知不知,殆矣。

X

URL、URLConnection

URL

 URL 类 java.net 包中定义了 URL 类

 1@Test
 2    public void urlTesting() throws IOException {
 3        URL url = new URL("http://api.xdclass.net:8081/pub/api/v1/web/find_ad_by_id?id=1");
 4        //主机地址
 5        System.out.println("getHost="+ url.getHost());//api.xdclass.net
 6        //协议
 7        System.out.println("getProtocol="+ url.getProtocol());//http
 8        //端口
 9        System.out.println("getPort="+ url.getPort());//8081
10        //路径(接口)
11        System.out.println("url.getPath="+ url.getPath());// /pub/api/v1/web/find_ad_by_id
12        //请求参数
13        System.out.println("url.getQuery="+ url.getQuery());//id=1
14        //获取getPath+getQuery的组合
15        System.out.println("url.getFile="+ url.getFile());//url.getFile=/pub/api/v1/web/find_ad_by_id?id=1
16    }

HttpURLConnection 类

 java.net 包中提供了访问 HTTP 协议的类,继承 ⾃ URLConnection。

 1@Test
 2    public void urlTesting() throws IOException {
 3        URL url = new URL("http://api.xdclass.net:8081/pub/api/v1/web/find_ad_by_id?id=1");
 4        //主机地址
 5        System.out.println("getHost=" + url.getHost());//api.xdclass.net
 6        //协议
 7        System.out.println("getProtocol=" + url.getProtocol());//http
 8        //端口
 9        System.out.println("getPort=" + url.getPort());//8081
10        //路径(接口)
11        System.out.println("url.getPath=" + url.getPath());// /pub/api/v1/web/find_ad_by_id
12        //请求参数
13        System.out.println("url.getQuery=" + url.getQuery());//id=1
14        //获取getPath+getQuery的组合
15        System.out.println("url.getFile=" + url.getFile());//url.getFile=/pub/api/v1/web/find_ad_by_id?id=1
16
17        //获取对应的连接对象(发起HTTP请求)
18        HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
19        //设置请求方式
20        //httpURLConnection.setRequestMethod();
21        //获取服务器的响应代码
22        int responseCode = httpURLConnection.getResponseCode();
23
24        if (200 <= responseCode && responseCode <= 299) {
25            //返回URL输入流,用于读取资源
26            try (InputStream inputStream = httpURLConnection.getInputStream();
27                 //使用BufferedReader读取(缓冲区提高性能)
28                 BufferedReader input = new BufferedReader(new InputStreamReader(inputStream))
29            ) {
30                //字符串拼接(String 拼接字符串需要不断的开辟内存空间)
31                StringBuilder response = new StringBuilder();
32
33                //临时变量,用于读取input
34                String currentLine;
35                while ((currentLine = input.readLine()) != null) {
36                    response.append(currentLine);
37                }
38                System.out.println(response.toString());//{"code":0,"data":{"id":1,"url":"https://www.aliyun.com/minisite/goods?userCode=r5saexap&share_source=copy_link","img":"https://xd-video-pc-img.oss-cn-beijing.aliyuncs.com/xdclass_pro/bannner/1911/1212.png","createTime":"2018-11-18T16:00:00.000+0000","remark":"竖图"},"msg":null}
39
40            }catch (Exception e){
41                e.printStackTrace();
42            }
43        }
44    }

作者:Soulboy