由於項目上要調用中國移動的接口,用到了X-WSSE頭部驗證,通過百度找到了寫法,寫了個Demo。
package com.epoint.test;
import java.io.IOException;
import java.net.URLDecoder;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.TimeZone;
import java.util.UUID;
import org.apache.commons.codec.binary.Base64;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
import net.sf.json.JSONObject;
public class TestSms
{
public static void main(String[] args) throws NoSuchAlgorithmException {
String httpResponse = post();
System.out.println(httpResponse);
}
/**
* 發送 post請求
* @param url 訪問的接口地址
* @param appKey app名
* @param appSecret 密碼
* @param jsonParam 查詢參數
* @return HttpResponse 該類包含請求方法的態碼及返回的數據
* @throws NoSuchAlgorithmException
*/
public static String post() throws NoSuchAlgorithmException {
String appKey = "e9774d93e1e7491cbfba31ecbcbed1be";
String appSecret = "853ea5b5f4e98387";
String jsonParam = "{\"from\":\"106575261108138\",\"to\":\"+8613862114469\", \"smsTemplateId\":\"f285dc66-4eec-44ee-82d5-17ae726658c0\", \"paramValue\":{\"name\":\"aa\",\"taskname\":\"aa\",\"time\":\"11\"}}";
String url = "http://aep.api.cmccopen.cn/entireSms/sendTemplateSms/v1";
//post請求返回結果
DefaultHttpClient httpClient = new DefaultHttpClient();
JSONObject jsonResult = null;
HttpPost method = new HttpPost(url);
method.addHeader("Authorization", "WSSE realm=\"SDP\", profile=\"UsernameToken\", type=\"Appkey\"");
String s = getXWSSE(appKey, appSecret);
method.addHeader("X-WSSE", s);
System.out.println(s);
try {
if (null != jsonParam) {
//解決中文亂碼問題
StringEntity entity = new StringEntity(jsonParam, "utf-8");
entity.setContentEncoding("UTF-8");
entity.setContentType("application/json");
method.setEntity(entity);
}
HttpResponse result = httpClient.execute(method);
url = URLDecoder.decode(url, "UTF-8");
/**請求發送成功,並得到響應**/
String str = "";
try {
/**讀取服務器返回過來的json字符串數據**/
str = EntityUtils.toString(result.getEntity());
/**把json字符串轉換成json對象**/
jsonResult = JSONObject.fromObject(str);
}
catch (Exception e) {
// logger.error("post請求提交失敗:" + url, e);
}
// }
}
catch (IOException e) {
// logger.error("post請求提交失敗:" + url, e);
}
return jsonResult.toString();
}
private static String getXWSSE(String appKey, String appSecret) throws NoSuchAlgorithmException {
//取值爲:UsernameToken Username="App Key的值",PasswordDigest="PasswordDigest的值", Nonce="隨機數", Created="隨機數生成時間"。
//Created
Calendar calendar = Calendar.getInstance();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'");
sdf.setTimeZone(TimeZone.getTimeZone("UTC"));
String stTime = sdf.format(calendar.getTime());
System.out.println(stTime);
// user
String stUserName = appKey;
// nonce
String stNonce = UUID.randomUUID().toString().replace("-", "").toUpperCase();
String stCombine = stNonce + stTime + appSecret;
String stPassWordDigiest = SHA256Encrypt(stCombine);
String stXsse = "UsernameToken Username=" + "\"" + stUserName + "\"" + "," + "PasswordDigest=" + "\""
+ stPassWordDigiest + "\"" + "," + "Nonce=" + "\"" + stNonce + "\"" + "," + "Created=" + "\"" + stTime
+ "\"";
return stXsse;
}
public static String SHA256Encrypt(String strSrc) throws NoSuchAlgorithmException {
//SHA-256加密
MessageDigest msgDigest = MessageDigest.getInstance("SHA-256");
//加密字符串
msgDigest.update(strSrc.getBytes());
String string = Base64.encodeBase64String(msgDigest.digest());
System.out.println(string);
return string;
}
}