ThinkChat🤖让你学习和工作更高效,注册即送10W Token,即刻开启你的AI之旅 广告
### JAVA示例代码 ``` package com.ksxing; import io.jsonwebtoken.Jwts; import io.jsonwebtoken.SignatureAlgorithm; import java.io.UnsupportedEncodingException; import java.nio.charset.StandardCharsets; /** * Created by examstar on 2018/8/1. */ public class KSXJWTUtil { /** * 客户唯一标识符,由考试星提供,需替换成自己的appId */ private static final int appId = 123456; /** * 用于加密jwt,由考试星提供,需替换成自己的appKey */ private static final String appKey = "debae3071b7d48818fe51a1fbc1b1113"; /** * 生成jwtInfo * * @param actionId * @return */ private static String getJWTInfo(String actionId) { String jwtInfo = null; try { jwtInfo = Jwts.builder() .setHeaderParam("alg", "HS256") .setHeaderParam("typ", "JWT") .claim("exp", System.currentTimeMillis() + 1000 * 10) .claim("action_id", actionId) .signWith( SignatureAlgorithm.HS256, appKey.getBytes(StandardCharsets.UTF_8.toString()) ) .compact(); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } return jwtInfo; } /** * 获取post请求地址 * * @param actionId * @return */ public static String getPostUrl(String actionId) { /** * 考试星域名 */ String domainUrl = "https://api.kaoshixing.com"; String postUrl = String.format("%s/api/company/data/%d/?jwt=%s", domainUrl, appId, getJWTInfo(actionId)); return postUrl; } } ``` ``` package com.ksxing; import org.apache.http.HttpEntity; import org.apache.http.client.methods.CloseableHttpResponse; import org.apache.http.client.methods.HttpPost; import org.apache.http.entity.StringEntity; import org.apache.http.impl.client.CloseableHttpClient; import org.apache.http.impl.client.HttpClients; import org.apache.http.util.EntityUtils; import java.nio.charset.StandardCharsets; /** * Created by examstar on 2018/8/1. */ public class KSXPostUtil { public static String sendPost(String url, String body) { String responseContent = null; try { CloseableHttpClient httpClient = HttpClients.createDefault(); HttpPost httpPost = new HttpPost(url); httpPost.addHeader("Content-Type", "application/json"); httpPost.setEntity(new StringEntity(body, StandardCharsets.UTF_8.toString())); CloseableHttpResponse response = httpClient.execute(httpPost); HttpEntity entity = response.getEntity(); responseContent = EntityUtils.toString(entity, StandardCharsets.UTF_8.toString()); response.close(); httpClient.close(); } catch (Exception e) { e.printStackTrace(); } return responseContent; } } ``` ### ``` package com.ksxing; import net.sf.json.JSONObject; /** * Created by examstar on 2018/8/1. */ public class KSXPostUtilTest { public static void main(String[] args) { String postUrl = KSXJWTUtil.getPostUrl("201"); JSONObject json = new JSONObject(); json.put("user_id", "ksx001"); json.put("user_name", "考试星测试"); json.put("role", "staff"); json.put("department", "总部"); System.out.println(KSXPostUtil.sendPost(postUrl, json.toString())); } } ```