package com.qiwuai.garden.common.utils; import com.alibaba.fastjson.JSONObject; import com.qiwuai.garden.dto.request.PhraseRequest; import com.qiwuai.garden.dto.response.PhraseResponse; import org.apache.commons.codec.digest.DigestUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.http.HttpEntity; import org.springframework.http.HttpHeaders; import org.springframework.http.HttpMethod; import org.springframework.http.ResponseEntity; import org.springframework.web.client.RestTemplate; public class HttpUtil { static Logger log = LoggerFactory.getLogger(HttpUtil.class); //动态词组更新秘钥 public static String appSecret = YamlUtil.getValueByKey("appSecret"); //动态词组更新秘钥 public static String updatePhraseUrl = YamlUtil.getValueByKey("updatePhraseUrl"); public static void doPut(String phraseName, String phraseContent) { RestTemplate template = new RestTemplate(); //获取时间戳 long timestamp = System.currentTimeMillis(); //签名 String authorization = DigestUtils.md5Hex(appSecret + ":" + timestamp); //设置请求体 PhraseRequest phraseRequest = new PhraseRequest(); phraseRequest.setWorkName("秘密花园"); phraseRequest.setPhraseName(phraseName); phraseRequest.setPhraseContent(phraseContent); JSONObject object = new JSONObject(); object.put("workName", "秘密花园"); object.put("phraseName", phraseName); object.put("phraseContent", phraseContent); //设置请求头 HttpHeaders httpHeaders = new HttpHeaders(); httpHeaders.set("Content-Type", "application/json;charset=utf-8"); httpHeaders.set("Timestamp", String.valueOf(timestamp)); httpHeaders.set("Authorization", authorization); HttpEntity request = new HttpEntity<>(object, httpHeaders); ResponseEntity forObject = template.exchange(updatePhraseUrl, HttpMethod.PUT, request, PhraseResponse.class); System.out.println("动态词组更新后的返回结果" + forObject); log.info(phraseName + "的动态词组更新结果:" + forObject.getBody()); } }