华为云xxxx

    <dependencies>
        <dependency>
            <groupId>org.apache.httpcomponents</groupId>
            <artifactId>httpclient</artifactId>
            <version>4.5.13</version>
        </dependency>
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>fastjson</artifactId>
            <version>1.2.71</version>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter</artifactId>
            <version>RELEASE</version>
            <scope>compile</scope>
        </dependency>
    </dependencies>
package com.ping;

import com.alibaba.fastjson.JSONObject;
import org.apache.http.Header;
import org.apache.http.HttpEntity;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.HttpRequestBase;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;

public class HttpClientUtils {
    public static String sendPostString(String url, String json) {
        CloseableHttpClient httpClient = null;
        HttpPost httpPost = null;
        CloseableHttpResponse response = null;
        String result = "";
        try {
            httpClient = HttpClientBuilder.create().build();
            httpPost = new HttpPost(url);
            // 设置参数
            if (null != json && json.length() > 0) {
                StringEntity stringEntity = new StringEntity(json, "utf-8");
                stringEntity.setContentType("application/json;charset=utf-8");
                httpPost.setEntity(stringEntity);
            }
            response = httpClient.execute(httpPost);

            //get all headers
            Header[] headers = response.getAllHeaders();
            for (Header header : headers) {
                if("X-Subject-Token".equals(header.getName())){
                    result = header.getValue();
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            close(response, httpPost, httpClient);
        }
        return result;
    }

    public static String sendGet(String token,String url, Map<String,String> params){
        CloseableHttpClient httpClient = null;
        HttpGet httpGet = null;
        CloseableHttpResponse response = null;
        String result = "";
        try {
            httpClient = HttpClientBuilder.create().build();
            httpGet = new HttpGet(url);
            // 设置参数
            if (null != params && params.size() > 0){
                System.out.println(token);
                List<BasicNameValuePair> pairList = new ArrayList<BasicNameValuePair>();
                params.forEach((x,y) -> pairList.add(new BasicNameValuePair(x,y)));
                UrlEncodedFormEntity urlEncodedFormEntity = new UrlEncodedFormEntity(pairList,"utf-8");
                // 将参数转成page=1&limit=5格式
                String param = EntityUtils.toString(urlEncodedFormEntity, "utf-8");
                httpGet = new HttpGet(url+"?"+param);
                httpGet.setHeader("Content-Type","application/json; charset=UTF-8");
                httpGet.setHeader("X-Auth-Token",token);
                System.out.println(httpGet);
            }
            response = httpClient.execute(httpGet);
            result = EntityUtils.toString(response.getEntity());
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            close(response,httpGet,httpClient);
        }
        return result;
    }

    public static String sendPostJson(String url, Map<String,String> params) {
        CloseableHttpClient httpClient = null;
        HttpPost httpPost = null;
        CloseableHttpResponse response = null;
        String result = "";
        try {
            httpClient = HttpClientBuilder.create().build();
            httpPost = new HttpPost(url);
            // 设置参数
            if (null != params && params.size() > 0) {
                String paramJson = JSONObject.toJSONString(params);
                StringEntity stringEntity = new StringEntity(paramJson, "utf-8");
                stringEntity.setContentType("application/json;charset=utf-8");
                httpPost.setEntity(stringEntity);
            }
            response = httpClient.execute(httpPost);
            result = EntityUtils.toString(response.getEntity());
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            close(response, httpPost, httpClient);
        }
        return result;
    }

    public static void close(CloseableHttpResponse response, HttpRequestBase httpRequestBase, CloseableHttpClient httpClient){
        try {
            if (response != null) {
                response.close();
            }

            if (httpRequestBase!= null){
                httpRequestBase.releaseConnection();
            }

            if (httpClient != null){
                httpClient.close();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
package com.ping;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.ping.pojo.*;
import org.junit.jupiter.api.Test;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

public class TestClient {
    @Test
    void sendGetUtil(){
        JsonRootBean jsonRootBean = new JsonRootBean();

        Domain domain = new Domain();
        domain.setName("mingtu");

        User user = new User();
        user.setName("mingtu");
        user.setPassword("yanglin1997");
        user.setDomain(domain);

        Password password = new Password();
        password.setUser(user);

        Project project = new Project();
        project.setName("");
        Scope scope = new Scope();
        scope.setProject(project);

        ArrayList<String> list = new ArrayList<String>();
        list.add("password");
        Identity identity = new Identity();
        identity.setMethods(list);
        identity.setPassword(password);

        Auth auth = new Auth();
        auth.setIdentity(identity);
        auth.setScope(scope);

        jsonRootBean.setAuth(auth);

        Object obj = JSONArray.toJSON(jsonRootBean);
        String json = obj.toString();

        System.out.println("将Person对象转成json:" + json);

        String str = "{\n" +
                " \"auth\": {\n" +
                "  \"identity\": {\n" +
                "   \"methods\": [\n" +
                "    \"password\"\n" +
                "   ],\n" +
                "   \"password\": {\n" +
                "    \"user\": {\n" +
                "     \"domain\": {\n" +
                "      \"name\": \"mingtu\"\n" +
                "     },\n" +
                "     \"name\": \"mingtu\",\n" +
                "     \"password\": \"yanglin1997\"\n" +
                "    }\n" +
                "   }\n" +
                "  }\n" +
                " }\n" +
                "}";
        String str2 = "{\n" +
                "    \"auth\": {\n" +
                "        \"identity\": {\n" +
                "            \"methods\": [\n" +
                "                \"password\"\n" +
                "            ],\n" +
                "            \"password\": {\n" +
                "                \"user\": {\n" +
                "                    \"domain\": {\n" +
                "                        \"name\": \"mingtu\"        //IAM用户所属帐号名\n" +
                "                    },\n" +
                "                    \"name\": \"mingtu\",             //IAM用户名\n" +
                "                    \"password\": \"yanglin1997\"      //IAM用户密码\n" +
                "                }\n" +
                "            }\n" +
                "        },\n" +
                "        \"scope\": {\n" +
                "            \"domain\": {\n" +
                "                \"name\": \"mingtu\"        //IAM用户所属帐号名\n" +
                "            }\n" +
                "        }\n" +
                "    }\n" +
                "}";
        String str3 = "{\n" +
                "    \"auth\": {\n" +
                "        \"identity\": {\n" +
                "            \"methods\": [\n" +
                "                \"password\"\n" +
                "            ],\n" +
                "            \"password\": {\n" +
                "                \"user\": {\n" +
                "                    \"domain\": {\n" +
                "                        \"name\": \"mingtu\"        //IAM用户所属帐号名\n" +
                "                    },\n" +
                "                    \"name\": \"mingtu\",             //IAM用户名\n" +
                "                    \"password\": \"yanglin1997\"      //IAM用户密码\n" +
                "                }\n" +
                "            }\n" +
                "        },\n" +
                "        \"scope\": {\n" +
                "            \"project\": {\n" +
                "                \"name\": \"cn-north-1\"               //项目名称\n" +
                "            }\n" +
                "        }\n" +
                "    }\n" +
                "}";

        //String token = HttpClientUtils.sendPostString("https://iam.cn-north-1.myhuaweicloud.com/v3/auth/tokens", str3);
        String token = HttpClientUtils.sendPostString("https://iam.myhuaweicloud.com/v3/auth/tokens?nocatalog=true", str);

        System.out.println(token);
        Map<String,String> map = new HashMap<>();
        map.put("project_id","5e6e83881aca4888a70dff6a0282509f");
        String url = "https://ces.cn-north-1.myhuaweicloud.com/V1.0/5e6e83881aca4888a70dff6a0282509f/metrics";
        String sendGet = HttpClientUtils.sendGet(token, url, map);
        System.out.println(sendGet);

    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值