String urlPost="http://127.0.0.1:9999"+"/auth/oauth/token?username=" + userName+"&password=" + AppEncrypt.encryptAES("123456") + "&grant_type=password&scope=server"; HttpHeaders headers = new HttpHeaders(); headers.set("Content-Type","application/x-www-form-urlencoded"); headers.set("Authorization","Basic dGVzdDp0ZXN0"); headers.set("Connection","keep-alive"); headers.set("Accept-Encoding","gzip, deflate, br"); headers.set("Accept","*/*"); String s = WeChatUtils.clientSetHeaders(urlPost, HttpMethod.POST, null, headers); retJson.put("token",String.valueOf(new Gson().fromJson(s, HashMap.class).get("access_token")));
public static String clientSetHeaders(String url, HttpMethod method, Object params, HttpHeaders headers){ RestTemplate client = new RestTemplate(); MappingJackson2HttpMessageConverter converter = new MappingJackson2HttpMessageConverter(); List<MediaType> mediaTypes = new ArrayList<>(); mediaTypes.add(MediaType.APPLICATION_FORM_URLENCODED); converter.setSupportedMediaTypes(mediaTypes); client.getMessageConverters().add(converter); org.springframework.http.HttpEntity<Object> requestEntity = new org.springframework.http.HttpEntity<>(params, headers); // 执行HTTP请求 ResponseEntity<String> response = client.exchange(url, method, requestEntity, String.class); return response.getBody(); }