java调用Harbor v2.0接口

Harbor是一个用于存储和分发Docker镜像的企业级Registry服务器。
有时候需要Java自动获取docker 镜像的相关信息,Harbor提供了很多的api,提供调用。但是需要Authorization Basic认证。最近做了代码实现,这里做下备忘。

CloseableHttpClient httpClient = HttpClientBuilder.create().build();
//获取镜像的版本信息
        String uri = "/api/v2.0/projects/projectname/repositories/reposiname/artifacts/";
        HttpGet httpGet = new HttpGet(“harborip:port”+uri);
        httpGet.setHeader("Content-Type", "application/json");
        //关键语句
        httpGet.setHeader("Authorization", "Basic "+ Base64.getUrlEncoder().encodeToString(("admin" + ":" + "Harbor12345").getBytes()));
        // 响应模型
        CloseableHttpResponse response = null;
        HashMap hashMap = new HashMap();
        try {
            // 由客户端执行(发送)Get请求
            response = httpClient.execute(httpGet);
            // 从响应模型中获取响应实体
            HttpEntity responseEntity = response.getEntity();
            System.out.println("响应状态为:" + response.getStatusLine());
            if (responseEntity != null) {
                System.out.println("响应内容长度为:" + responseEntity.getContentLength());
                String  STR_INFO= EntityUtils.toString(responseEntity);
                System.out.println("响应内容为:" + STR_INFO);
                JSONArray array = JSONArray.parseArray(STR_INFO);
                String newTags = ((JSONObject) array.get(0)).get("tags").toString();
                JSONArray newTagArr = JSONArray.parseArray(newTags);
                String newtag = ((JSONObject) newTagArr.get(0)).get("name").toString();
                
            }
        } catch (Exception e) {
            log.error("error", e);
        }  finally {
            try {
                // 释放资源
                if (httpClient != null) {
                    httpClient.close();
                }
                if (response != null) {
                    response.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

你可能感兴趣的:(javaweb,java,docker,开发语言)