okhttpClient 使用示例
1 @Autowired 2 private OkHttpClient okHttpClient; 3 4 @PostMapping("testOkHttp") 5 public Result testOkHttp(@RequestParam("code") String code) throws IOException { 6 Result result = new Result(Code.INTERNAL_SERVER_ERROR, null, "fail"); 7 8 //1. 获取参数 access_token string 接口调用凭证 9 Result getReqParam = getAccessToken(); 10 Map<String, String> map = (Map<String, String>) getReqParam.getData(); 11 12 if (null == map) { 13 String phoneURL = "https://api.weixin.qq.com/wxa/business/getuserphonenumber?access_token=" + map.get("access_token"); 14 15 JSONObject code1 = new JSONObject(); 16 code1.put("code", code); 17 String content = JSONObject.toJSONString(code1); 18 RequestBody body = RequestBody.create(content, okhttp3.MediaType.parse("application/json")); 19 Request request = new Request.Builder() 20 .url(phoneURL) 21 .method("POST", body) 22 .addHeader("Accept", "application/json") 23 .addHeader("Content-type", "application/json") 24 .build(); 25 Response response = okHttpClient.newCall(request).execute(); 26 System.out.println("response = " + response); 27 } 28 return result; 29 }