mitao
2025-02-21 31573d6180d15ef65ed0df9c2732495f40b12663
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
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<String,Object> 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<String> httpEntity = new HttpEntity(body, requestHeaders);
        ResponseEntity<String> response = new RestTemplate().exchange(url, HttpMethod.POST,httpEntity,String.class);
        System.out.println(JSONObject.toJSONString(JSONObject.parseObject(response.getBody()),true));
    }
 
}