• HttpClient远程调用基本使用(详解)


    通过HttpClient实现远程调用

    如果在不同的服务要实现服务与服务之间的调用,有众多选择,如HttpclientOkhttpHttpURLConnectionRestTemplate这几种,当然还有那些封装好的框架,如feign等。这里主要讲解一下这个HttpClient的使用。

    1,通过get方式实现

    public static String get(String url,String tokenName,String tokenValue,Map<String,String> map) {
    	//构建客户端对象
    	CloseableHttpClient httpClient = HttpClientBuilder.create().build();
    	//设置参数
    	if (CollectionUtils.isNotEmpty(map)) {
    		//添加参数
    		String paramData = addParamByGet(map);
    		//拼接参数
    		url = url + "?" + paramData ;
    	}
    	//String url = ""http://127.0.0.1:8080/user"
    	HttpGet httpGet = new HttpGet(url);
    	//响应体
    	CloseableHttpResponse response;
    	try {
    		//添加请求头,添加token
    		//httpGet.addHeader("token", "eyJ0eXAiOiJKc29uV2ViVG9rZW4iLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOi");
    		httpGet.addHeader(tokenName, tokenValue);
    		//添加请求配置,设置响应时间等
    		RequestConfig requestConfig = RequestConfig.custom()
    			.setSocketTimeout(2000) //服务器响应超时时间
    			.setConnectionRequestTimeout(2000) //连接服务器超时时间
    			.build();
    
    		//将配置信息添加到 httpGet 中
    		httpGet.setConfig(requestConfig);
    		//调用接口,获取响应
    		response = httpClient.execute(httpGet);
    		//将响应转化为一个结果集
    		HttpEntity entity = response.getEntity();
    		//将内容转化为字符串,并且设定指定字符集
    		String result= EntityUtils.toString(entity, "UTF-8");
    		//最终将结果集返回
    		return result;
    	} catch (IOException e) {
    		e.printStackTrace();
    	} finally {
    		//资源释放
    		if (httpGet != null) {
    			try {
    				httpGet.clone();
    			} catch (CloneNotSupportedException e) {
    				e.printStackTrace();
    			}
    		}
    		if (httpClient != null) {
    			try {
    				httpClient.close();
    			} catch (IOException e) {
    				e.printStackTrace();
    			}
    		}
    	}
    }
    
    • 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
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54

    get请求添加参数

        /**
         * get添加参数
         */
        public static StringBuilder addParamByGet(Map<String,String> map){
            StringBuilder param = new StringBuilder();
            //遍历参数
            for (Map.Entry<String, String> m :map.entrySet()) {
                param.append(m.getKey()).append("=").append(m.getValue()).append("&");
            }
            return param;
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    2,通过post的方式实现

    里面有一些token,参数之类的,如果不用,直接将代码注释即可

    public static String post(String url, String tokenName,String tokenValue, Map<String,String> map) throws IOException {
    	//构建客户端对象
    	CloseableHttpClient httpClient = HttpClientBuilder.create().build();
    	CloseableHttpResponse response = null;
    	try {
    		//创建http post请求对象 这个url可以手动封装一个
    		HttpPost post = new HttpPost("http://127.0.0.1:8080/user/addUser");
    		//设置请求头
    		//post.setHeader("token", "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJzdWIiOiJ1c2VyIiwidXN" +
    		//	"lckluZm8iOnsidXNlcklkIjoxNTg5ODIxMzMwNTY1NjQ0Mjg5fSwiZXhwIjoxNjY4MDU5MzQ3LCJpYXQiOjE2N" +
    		//	"jc5NzI5NDd9.mcmgzc0yEyTyfeo-2D5vpaYhdyD9LFBwCwP8s30r0Hs");
    		post.addHeader(tokenName, tokenValue);
    		//设置文本类型
    		post.setHeader("Content-Type", "application/json");
    
            //如果只有一两个参数的话,也可以通过这个反转义字符手动传参变成json格式,反正最终是要转成json的形式的
    		//post.setEntity(new StringEntity("{\"type\":\"1\"}", "UTF-8"));
           
    		//构建参数
            if (CollectionUtils.isNotEmpty(map)){
                JSONObject jo = new JSONObject();
                //增加参数
                addParam(map,jo);
    			post.setEntity(new StringEntity(jo.toString(), ContentType.APPLICATION_JSON));
            }else {
            	post.setEntity(new StringEntity(null, ContentType.APPLICATION_JSON));
            }
    		
            //创建响应对象
    		response = httpClient.execute(post);
    		/**
    		 * 由于响应接口返回的是一个字符串,因此需要转换
    		 * 响应体数据封装在HttpEntity对象中
    		 */
    		String result = EntityUtils.toString(response.getEntity(), "utf-8");
    		return result;
    	} catch (Exception e) {
    		e.printStackTrace();
    	} finally {
    		//关闭资源
    		response.close();
    		httpClient.close();
    	}
    }
    
    • 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
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44

    post请求添加参数

    /**
     * post添加参数
     */
    public static JSONObject addParam(Map < String, String > map, JSONObject jsonObject) {
    	for (Map.Entry < String, String > entry: map.entrySet()) {
    		jsonObject.put(entry.getKey(), entry.getValue());
    	}
    	return jsonObject;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

    3,解析返回数据

    上面两种方式都是返回一个json格式的字符串,因此可以将这个字符串解析,或者直接在方法里面解析,我这里选择在外面解析

    public static vois mian(String Args[]) {
    	//调用get请求,可以将里面的url,token等作为参数传入
    	String url = "localhost:8080/user/message";
    	String tokenName = "token";
    	String tokenValue = "xxx";
    	String message = get(url,tokenName,tokenValue,null);
    	//如果里面有多条数据,那么可以用数组方式解析,即使一条数据,也是可以通过这个解析的
    	JSONArray jsonArray = JSONArray.parseArray(message);
    	//遍历
        List<User> list = new ArrayList<>();
    	jsonArray.stream().forEach(json - > {
    		JSONObject jsonObject = JSONArray.parseObject(json.toString());
            User user = JSON.toJavaObject(jsonObject, User.class);
            list.add(user);
    	});
        //遍历全部的用户
        list.stream.forEach(e->{System.out.println(e)});
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
  • 相关阅读:
    2023年【电工(中级)】考试资料及电工(中级)找解析
    Python全栈开发【基础-07】与用户交互
    unity属性之UnityEditor
    TCP四次挥手
    [笔记] Windows内核课程:保护模式《一》保护模式
    国货之光——jdchain1.6.5测试网络部署
    Qt 项目实战 | 多界面文本编辑器
    介绍一款小巧的Excel比对工具-DiffExcel
    软件测试常见术语和名词解释
    linux使用less排查日志文件
  • 原文地址:https://blog.csdn.net/zhenghuishengq/article/details/127772840