package com.panzhihua.service_community.util; import java.nio.charset.StandardCharsets; import java.security.MessageDigest; import java.security.NoSuchAlgorithmException; import java.time.LocalDateTime; import java.time.ZoneOffset; import java.util.HashMap; import java.util.Map; import org.springframework.http.HttpEntity; import org.springframework.http.HttpHeaders; import org.springframework.http.HttpMethod; import org.springframework.http.ResponseEntity; import org.springframework.stereotype.Component; import org.springframework.web.client.RestTemplate; import com.alibaba.fastjson.JSONObject; /** * @author zzj */ @Component public class JsoupUtil { public static String getSHA256StrJava(String str) { MessageDigest messageDigest; String encodeStr = ""; try { messageDigest = MessageDigest.getInstance("SHA-256"); messageDigest.update(str.getBytes(StandardCharsets.UTF_8)); byte[] bytes = messageDigest.digest(); StringBuffer stringBuffer = new StringBuffer(); String temp = null; for (int i = 0; i < bytes.length; i++) { temp = Integer.toHexString(bytes[i] & 0xFF); if (temp.length() == 1) { //1得到一位的进行补0操作 stringBuffer.append("0"); } stringBuffer.append(temp); } encodeStr = stringBuffer.toString(); } catch (NoSuchAlgorithmException e) { e.printStackTrace(); } return encodeStr; } public void test() { //获取时间戳 long timestamp = LocalDateTime.now().toEpochSecond(ZoneOffset.of("+8")); String url = "http://103.66.32.242:8005/zwfwMovePortal/interface/interfaceJson"; //以下常量值都是从网页源代码上获取到的------start final String STATE_COUNCIL_SIGNATURE_KEY = "fTN2pfuisxTavbTuYVSsNJHetwq5bJvCQkjjtiLM2dCratiA"; final String STATE_COUNCIL_X_WIF_NONCE = "QkjjtiLM2dCratiA"; final String STATE_COUNCIL_X_WIF_PAASID = "smt-application"; //国务院疫情防控查询 appId final String STATE_COUNCIL_APP_ID = "NcApplication"; //国务院疫情防控查询 PASSID final String STATE_COUNCIL_PASSID = "zdww"; //国务院疫情防控查询 秘钥 final String STATE_COUNCIL_NONCE = "123456789abcdefg"; //国务院疫情防控查询 token final String STATE_COUNCIL_TOEKN = "23y0ufFl5YxIyGrI8hWRUZmKkvtSjLQA"; //国务院疫情防控查询 key final String STATE_COUNCIL_KEY = "3C502C97ABDA40D0A60FBEE50FAAD1DA"; //以下常量值都是从网页源代码上获取到的------end HttpHeaders requestHeaders = new HttpHeaders(); Map body = new HashMap<>(10); //计算签名要用的字符串 String signatureStr = String.format("%d%s%d",timestamp, STATE_COUNCIL_SIGNATURE_KEY,timestamp); //计算签名 String signature = getSHA256StrJava(signatureStr).toUpperCase(); //请求头参数封装 requestHeaders.add( "x-wif-nonce",STATE_COUNCIL_X_WIF_NONCE); requestHeaders.add( "x-wif-paasid",STATE_COUNCIL_X_WIF_PAASID); requestHeaders.add( "x-wif-signature",signature); requestHeaders.add( "x-wif-timestamp",String.valueOf(timestamp)); //body参数封装 body.put("appId",STATE_COUNCIL_APP_ID); body.put("paasHeader",STATE_COUNCIL_PASSID); body.put("timestampHeader",timestamp); body.put("nonceHeader",STATE_COUNCIL_NONCE); //请求参数 signatureStr = String.format("%d%s%s%d",timestamp,STATE_COUNCIL_TOEKN,STATE_COUNCIL_NONCE,timestamp); String signatureHeader = getSHA256StrJava(signatureStr).toUpperCase(); body.put("signatureHeader",signatureHeader); body.put("key",STATE_COUNCIL_KEY); HttpEntity httpEntity = new HttpEntity(body, requestHeaders); ResponseEntity response = new RestTemplate().exchange(url, HttpMethod.POST,httpEntity,String.class); System.out.println(JSONObject.toJSONString(JSONObject.parseObject(response.getBody()),true)); } }