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 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118
| import java.nio.charset.StandardCharsets; import java.util.Date; import java.util.Map; import java.util.TreeMap; import java.util.UUID;
import javax.crypto.Mac; import javax.crypto.spec.SecretKeySpec;
import org.apache.commons.codec.binary.Base64; import org.apache.commons.codec.digest.DigestUtils; import org.apache.commons.io.IOUtils; import org.apache.http.HttpResponse; import org.apache.http.client.HttpClient; import org.apache.http.client.methods.HttpPost; import org.apache.http.entity.StringEntity; import org.apache.http.impl.client.HttpClients;
public class TestHttpClient { private static final String HTTP_METHOD = "POST"; private static final String ACCEPT_VALUE = "application/json;charset=utf-8"; private static final String CONTENT_TYPE_VALUE = "application/json;charset=utf-8"; private static final String HmacSHA256 = "HmacSHA256";
public static void main(String[] args) throws Exception { test(); }
private static void test() throws Exception { String appKey = ""; String appSecret = ""; String host = ""; String url = ""; String body = "";
Date date = new Date(); String uuid = UUID.randomUUID().toString(); String sign = getSign(appKey, appSecret, date, url, body, uuid);
HttpClient httpClient = HttpClients.createDefault(); HttpPost post = new HttpPost(host + url); post.setHeader("date", String.valueOf(date.getTime())); post.setHeader("accept", ACCEPT_VALUE); post.setHeader("content-type", CONTENT_TYPE_VALUE); post.setHeader("x-ca-key", appKey); post.setHeader("x-ca-signature", sign); post.setHeader("x-ca-timestamp", String.valueOf(date.getTime())); post.setHeader("x-ca-nonce", uuid); post.setHeader("content-md5", getBaseMD5(body)); post.setHeader("x-ca-signature-headers", "x-ca-nonce,x-ca-timestamp,x-ca-key");
StringEntity entity = new StringEntity(body, StandardCharsets.UTF_8); post.setEntity(entity);
System.out.println("entity:" + entity);
HttpResponse response = httpClient.execute(post);
System.out.println("response:" + response); System.out.println("Content: " + IOUtils.toString(response.getEntity().getContent())); }
private static String getSign(String appKey, String appSecret, Date date, String url, String body, String uuid) throws Exception { String contentMD5 = getBaseMD5(body); String formatDate = String.valueOf(date.getTime()); String headers = getSignHeaders(appKey, date, uuid);
StringBuilder builder = new StringBuilder(); builder.append(HTTP_METHOD).append("\n"); builder.append(ACCEPT_VALUE).append("\n"); builder.append(contentMD5).append("\n"); builder.append(CONTENT_TYPE_VALUE).append("\n"); builder.append(formatDate).append("\n"); builder.append(headers); builder.append(url);
String stringToSign = builder.toString();
Mac hmacSha256 = Mac.getInstance(HmacSHA256); byte[] keyBytes = appSecret.getBytes(StandardCharsets.UTF_8); hmacSha256.init(new SecretKeySpec(keyBytes, 0, keyBytes.length, HmacSHA256)); byte[] value = hmacSha256.doFinal(stringToSign.getBytes(StandardCharsets.UTF_8)); return Base64.encodeBase64String(value); }
private static String getBaseMD5(String body) { byte[] md5Bytes = DigestUtils.md5(body.getBytes(StandardCharsets.UTF_8)); final Base64 base64 = new Base64(); return new String(base64.encode(md5Bytes)); }
private static String getSignHeaders(String appKey, Date date, String uuid) { long timestamp = date.getTime(); Map<String, String> map = new TreeMap<>(); map.put("x-ca-key", appKey); map.put("x-ca-nonce", uuid); map.put("x-ca-timestamp", String.valueOf(timestamp));
StringBuilder builder = new StringBuilder(); for (String key : map.keySet()) { String value = map.getOrDefault(key, ""); String header = String.format("%s:%s\n", key, value); builder.append(header); } return builder.toString(); } }
|