签名校验失败 返回{"successful":false,"redirect":false}
请求抓包如下图:
代码如下: private static String APPID = ""; private static String APPKEY = ""; private static String DOMAIN = "https://solution.wps.cn";
public static void main(String[] args) throws NoSuchAlgorithmException, IOException {
System.setProperty("http.proxyHost", "127.0.0.1");
System.setProperty("http.proxyPort", "8866");
System.setProperty("https.proxyHost", "127.0.0.1");
System.setProperty("https.proxyPort", "8866");
String url = "/api/developer/v1/office/pdf/convert/to/xlsx";
SimpleDateFormat dateFormat = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss 'GMT'", Locale.US);
//Wed, 23 Jan 2013 06:43:08 GMT
dateFormat.setTimeZone(TimeZone.getTimeZone("GMT"));
String formattedDate = dateFormat.format(new Date());
System.out.println("====formattedDate==="+formattedDate);
String sign = signature(url,formattedDate);
System.out.println("====sign==="+sign);
String authorization = getAuthorization(sign);
System.out.println("====authorization==="+authorization);
createTask( authorization, url, formattedDate);
}
public static void createTask(String authorization,String url,String formattedDate) throws IOException, NoSuchAlgorithmException {
OkHttpClient client = new OkHttpClient.Builder()
.sslSocketFactory(CustomSSLSocketFactory.getSSLSocketFactory(),CustomSSLSocketFactory.getX509TrustManager())
.hostnameVerifier(new HostnameVerifier() {
@Override
public boolean verify(String hostname, SSLSession session) {
return true;
}
}).build();
MediaType mediaType = MediaType.parse("application/json");
String md5 = DigestUtils.md5Hex(url);
System.out.println("====md5==="+md5);
System.out.println("====request url==="+DOMAIN+url);
RequestBody body = RequestBody.create(mediaType, "{\"url\":\"https://appdownload.wingtatusa.com/test.pdf\",\"text_unify\":true,\"export_type\":\"xlsx\"}");
Request request = new Request.Builder()
.url(DOMAIN+url)
.post(body)
.addHeader("Date", formattedDate)
.addHeader("Content-Md5", md5)
.addHeader("Content-Type", "application/json")
.addHeader("Authorization", authorization)
.build();
Response response = client.newCall(request).execute();
System.out.println(getObjectMapper().writeValueAsString(response));
}
private static String getAuthorization(String signature) {
String Authorization = String.format("WPS-2:%s:%s", APPID, signature);
return Authorization;
}
private static String signature(String url,String formattedDate) throws NoSuchAlgorithmException, UnsupportedEncodingException {
// 将字符串转换为字节数组
byte[] appKeyBytes = APPKEY.getBytes(StandardCharsets.UTF_8);
byte[] contentMd5Bytes = url.getBytes(StandardCharsets.UTF_8);
byte[] contentTypeBytes = "application/json".getBytes(StandardCharsets.UTF_8);
byte[] dateBytes = formattedDate.getBytes(StandardCharsets.UTF_8);
// 合并字节数组
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
outputStream.write(appKeyBytes, 0, appKeyBytes.length);
outputStream.write(contentMd5Bytes, 0, contentMd5Bytes.length);
outputStream.write(contentTypeBytes, 0, contentTypeBytes.length);
outputStream.write(dateBytes, 0, dateBytes.length);
byte[] concatenated = outputStream.toByteArray();
// 计算SHA-1哈希
MessageDigest digest = MessageDigest.getInstance("SHA-1");
byte[] hashBytes = digest.digest(concatenated);
// 将字节数组转换为十六进制字符串
StringBuilder hexString = new StringBuilder();
for (byte b : hashBytes) {
String hex = String.format("%02x", b);
hexString.append(hex);
}
String signature = hexString.toString();
return signature;
}
private static ObjectMapper getObjectMapper() {
ObjectMapper mapper = new ObjectMapper();
mapper.setTimeZone(TimeZone.getTimeZone("Asia/Shanghai"));
mapper.setSerializationInclusion(Include.NON_NULL);
SimpleModule module = new SimpleModule();
mapper.registerModule(module);
return mapper;
}