```java
public class HuaWeiSMSUtil {
private static final String WSSE_HEADER_FORMAT = "UsernameToken Username=\"%s\",PasswordDigest=\"%s\",Nonce=\"%s\",Created=\"%s\"";
private static final String AUTH_HEADER_VALUE = "WSSE realm=\"SDP\",profile=\"UsernameToken\",type=\"Appkey\"";
public static void main(String[] args) throws Exception{
sendSms("[\""+1235+"\"]","xxxxxxxx","8822082404260","40b248aa34a7466287b76307572ad557");
}
public static void sendSms(String code,String phone,String sender,String templateId) throws Exception{
String url = "https://smsapi.cn-north-4.myhuaweicloud.com:443/sms/batchSendSms/v1";
String appKey = "xxxxxxxx";
String appSecret = "xxxxxx";
String signature = "xxxxxx";
String receiver = "+86"+phone;
String statusCallBack = "";
String templateParas = code;
String body = buildRequestBody(sender, receiver, templateId, templateParas, statusCallBack, signature);
if (null == body || body.isEmpty()) {
System.out.println("body is null.");
return;
}
String wsseHeader = buildWsseHeader(appKey, appSecret);
if (null == wsseHeader || wsseHeader.isEmpty()) {
System.out.println("wsse header is null.");
return;
}
CloseableHttpClient client = HttpClients.custom()
.setSSLContext(new SSLContextBuilder().loadTrustMaterial(null,
(x509CertChain, authType) -> true).build())
.setSSLHostnameVerifier(NoopHostnameVerifier.INSTANCE)
.build();
HttpResponse response = client.execute(RequestBuilder.create("POST")
.setUri(url)
.addHeader(HttpHeaders.CONTENT_TYPE, "application/x-www-form-urlencoded")
.addHeader(HttpHeaders.AUTHORIZATION, AUTH_HEADER_VALUE)
.addHeader("X-WSSE", wsseHeader)
.setEntity(new StringEntity(body)).build());
System.out.println(response.toString());
System.out.println(EntityUtils.toString(response.getEntity()));
}
static String buildRequestBody(String sender, String receiver, String templateId, String templateParas,
String statusCallbackUrl, String signature) {
if (null == sender || null == receiver || null == templateId || sender.isEmpty() || receiver.isEmpty()
|| templateId.isEmpty()) {
System.out.println("buildRequestBody(): sender, receiver or templateId is null.");
return null;
}
List<NameValuePair> keyValues = new ArrayList<NameValuePair>();
keyValues.add(new BasicNameValuePair("from", sender));
keyValues.add(new BasicNameValuePair("to", receiver));
keyValues.add(new BasicNameValuePair("templateId", templateId));
if (null != templateParas && !templateParas.isEmpty()) {
keyValues.add(new BasicNameValuePair("templateParas", templateParas));
}
if (null != statusCallbackUrl && !statusCallbackUrl.isEmpty()) {
keyValues.add(new BasicNameValuePair("statusCallback", statusCallbackUrl));
}
if (null != signature && !signature.isEmpty()) {
keyValues.add(new BasicNameValuePair("signature", signature));
}
return URLEncodedUtils.format(keyValues, Charset.forName("UTF-8"));
}
static String buildWsseHeader(String appKey, String appSecret) {
if (null == appKey || null == appSecret || appKey.isEmpty() || appSecret.isEmpty()) {
System.out.println("buildWsseHeader(): appKey or appSecret is null.");
return null;
}
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'");
String time = sdf.format(new Date());
String nonce = UUID.randomUUID().toString().replace("-", "");
byte[] passwordDigest = DigestUtils.sha256(nonce + time + appSecret);
String hexDigest = Hex.encodeHexString(passwordDigest);
String passwordDigestBase64Str = Base64.getEncoder().encodeToString(hexDigest.getBytes());
return String.format(WSSE_HEADER_FORMAT, appKey, passwordDigestBase64Str, nonce, time);
}
}