评论

V3版SDK请求封装

V3版SDK签名、POST,GET请求封装,

JAVA 版本经过测试 请求签名这块没有问题

private String getRandomString(int length) {
    //length表示生成字符串的长度
    String base = "abcdefghijklmnopqrstuvwxyz0123456789";
    Random random = new Random();
    StringBuffer sb = new StringBuffer();
    for (int i = 0; i < length; i++) {
        int number = random.nextInt(base.length());
        sb.append(base.charAt(number));
    }
    return sb.toString().toUpperCase();
}
private long getTimestamp(){
    return System.currentTimeMillis() / 1000;
}
private String getSignature(String method, String url, String body,String nonceStr,long timestamp) throws NoSuchAlgorithmException, SignatureException, UnsupportedEncodingException, InvalidKeyException, KeyStoreException, FileNotFoundException {
    PrivateKey privateKey=WxUtil.loadPrivateKey(new ByteArrayInputStream(wxMchServerConfig.getApiclientKey().getBytes("utf-8")));
    String message=method + "\n"
            + url + "\n"
            + timestamp + "\n"
            + nonceStr + "\n"
            + body + "\n";
    Signature sign = Signature.getInstance("SHA256withRSA");
    sign.initSign(privateKey);
    sign.update(message.getBytes("utf-8"));
    return Base64.getEncoder().encodeToString(sign.sign());
}
public String request(String method, String domainUrl, String jsonData) throws MalformedURLException, FileNotFoundException, NoSuchAlgorithmException, KeyStoreException, UnsupportedEncodingException, SignatureException, InvalidKeyException {
    String nonceStr=getRandomString(32);
    long timestamp=getTimestamp();
    URL uri = new URL(domainUrl);
    String canonicalUrl = uri.getPath();
    if (uri.getQuery() != null) {
        canonicalUrl += "?" + uri.getQuery();
    }
    String signature=getSignature(method,canonicalUrl,jsonData,nonceStr,timestamp);
    String authorization="mchid=\"" + wxMchServerConfig.getMchId() + "\","
            + "nonce_str=\"" + nonceStr + "\","
            + "timestamp=\"" + timestamp + "\","
            + "serial_no=\"" + wxMchServerConfig.getSerialNo() + "\","
            + "signature=\"" + signature + "\"";
    Map<String,String> headers=new HashMap<>();
    headers.put("Authorization","WECHATPAY2-SHA256-RSA2048"+" "+authorization);
    headers.put("Content-Type" ,"application/json");
    headers.put("User-Agent","WechatPay-TEST-HttpClient");

    if(method.equals("POST")) {
        HttpResponse response = HttpRequest.post(domainUrl)
                .body(jsonData)
                .addHeaders(headers)
                .execute();
        String respbody=response.body();
        return respbody;
    }
    else {
        HttpResponse response = HttpRequest.get(domainUrl)
                .body(jsonData)
                .addHeaders(headers)
                .execute();
        String respbody=response.body();
        return respbody;
    }
}
最后一次编辑于  2020-06-29  
点赞 1
收藏
评论

1 个评论

  • 淡定
    淡定
    2021-04-01
    HttpResponse response = HttpRequest.post(domainUr
    

    这里的两个类是哪个版本?

    2021-04-01
    赞同
    回复
登录 后发表内容