public String getCode(String jsCode) { try { String url= "http://www.baidu.com" ResponseEntity responseEntity = restTemplate.getForEntity(url, WeChatSessionVo.class); if (responseEntity.getStatusCode().equals(HttpStatus.OK)) { log.info("返回body为:{}", JacksonTool.toJson(responseEntity.getBody())); WeChatSessionVo vo= (WeChatSessionVo) responseEntity.getBody(); return vo.getOpenid(); } } catch (Exception e) { log.error("信息出错,jsCode:{}", jsCode, e); } return null; }
restTemplate get
String wholeUrl = URLDecoder.decode("www.baidu.com", "utf8"); ResponseEntity<byte[]> responseEntity; try { responseEntity = restTemplate.exchange( wholeUrl, HttpMethod.GET, this.createEntity(), new ParameterizedTypeReference<byte[]>() { } ); } catch (Exception e) { log.error("http时出错, 请求地址:{}", wholeUrl, e); throw new RuntimeException(500+""); } if (responseEntity.getBody() == null || responseEntity.getBody().length == 0) { if (log.isInfoEnabled()) { log.info("请求搜索中台失败.中台返回内容为空. httpUrl:{}", wholeUrl); } throw new RuntimeException(500+""); } byte[] resultByte = responseEntity.getBody(); String resultStr = new String(resultByte, StandardCharsets.UTF_8); if (log.isInfoEnabled()) { log.info("请求搜索中台返回数据:{}", resultStr); }
private <T> HttpEntity<T> createEntity() { HttpHeaders headers = new HttpHeaders(); MediaType type = MediaType.parseMediaType("application/json; charset=UTF-8"); headers.setContentType(type); headers.add("Accept", MediaType.APPLICATION_JSON.toString()); return new HttpEntity<>(headers); }
public String sendPost(Integer platform, String jsCode) { try { String requestURI = "www.baidu.com"; log.info("通过code转换微信小程序凭证信息,构建URL地址为:{}", requestURI); ResponseEntity responseEntity = restTemplate.getForEntity(requestURI, WeChatSessionVo.class); if (responseEntity.getStatusCode().equals(HttpStatus.OK)) { log.info("通过code转换微信小程序凭证信息,返回body为:{}", JacksonTool.toJson(responseEntity.getBody())); WeChatSessionVo vo= (WeChatSessionVo) responseEntity.getBody(); return vo.getUnionid(); } } catch (Exception e) { log.error("通过code转换微信小程序凭证信息出错,jsCode:{}", jsCode, e); } return null; }