package com.mchuan.mcloud.client.test.ext; import java.io.IOException; import net.sf.json.JSONArray; import net.sf.json.JSONObject; import java.security.MessageDigest; import java.text.SimpleDateFormat; import java.util.Random; import org.apache.commons.httpclient.HttpClient; import org.apache.commons.httpclient.HttpException; import org.apache.commons.httpclient.methods.PostMethod; import org.apache.commons.httpclient.methods.RequestEntity; import org.apache.commons.httpclient.methods.StringRequestEntity; import org.apache.commons.httpclient.params.HttpMethodParams; public class MessageUtilsJson { private MessageUtilsJson(){}; private static final String sendUrl="http://112.74.139.4:8002/sms3_api/jsonapi/jsonrpc2.jsp"; public static String sendMsg(String userid,String password,String phone,String content) throws HttpException, IOException{ JSONObject jsonObject = new JSONObject(); jsonObject.accumulate("id","1"); jsonObject.accumulate("method","send"); password = md5(password); String seqid = new SimpleDateFormat("yyMMddHHmmss").format(System.currentTimeMillis()); String tick = ("00000" + new Random().nextInt(1000000)); seqid = seqid + tick.substring(tick.length() - 6); String sign = md5(seqid + password); JSONObject jsonParams = new JSONObject(); jsonParams.accumulate("userid", userid); jsonParams.accumulate("seqid", seqid); jsonParams.accumulate("sign", sign); JSONObject jsonSubmits = new JSONObject(); jsonSubmits.accumulate("content", content); jsonSubmits.accumulate("phone", phone); JSONArray jsonArray = new JSONArray(); jsonArray.add(jsonSubmits); jsonParams.accumulate("submit",jsonArray); jsonObject.accumulate("params", jsonParams); System.out.println(jsonObject.toString()); String result = submitUrl(jsonObject, sendUrl); return result; } // 获取短信状态 public static String getMsgStatus(String userid,String password) throws HttpException, IOException{ JSONObject jsonObject = new JSONObject(); jsonObject.accumulate("id","1"); jsonObject.accumulate("method","report"); password = md5(password); String seqid = new SimpleDateFormat("yyMMddHHmmss").format(System.currentTimeMillis()); String tick = ("00000" + new Random().nextInt(1000000)); seqid = seqid + tick.substring(tick.length() - 6); String sign = md5(seqid + password); JSONObject jsonParams = new JSONObject(); jsonParams.accumulate("userid", userid); jsonParams.accumulate("seqid", seqid); jsonParams.accumulate("sign", sign); jsonObject.accumulate("params", jsonParams); System.out.println(jsonObject.toString()); String result = submitUrl(jsonObject, sendUrl); return result; } public static String getMsgUp(String userid,String password) throws HttpException, IOException{ JSONObject jsonObject = new JSONObject(); jsonObject.accumulate("id","1"); jsonObject.accumulate("method","upmsg"); password = md5(password); String seqid = new SimpleDateFormat("yyMMddHHmmss").format(System.currentTimeMillis()); String tick = ("00000" + new Random().nextInt(1000000)); seqid = seqid + tick.substring(tick.length() - 6); String sign = md5(seqid + password); JSONObject jsonParams = new JSONObject(); jsonParams.accumulate("userid", userid); jsonParams.accumulate("seqid", seqid); jsonParams.accumulate("sign", sign); jsonObject.accumulate("params", jsonParams); System.out.println(jsonObject.toString()); String result = submitUrl(jsonObject, sendUrl); return result; } public static String submitUrl(JSONObject jsonobject,String url) throws IOException, HttpException { HttpClient httpClient = new HttpClient(); PostMethod method = new PostMethod(url); method.getParams().setParameter(HttpMethodParams.HTTP_CONTENT_CHARSET, "UTF-8"); httpClient.getHttpConnectionManager().getParams().setConnectionTimeout(5000); httpClient.getHttpConnectionManager().getParams().setSoTimeout(300000); RequestEntity requestEntity=new StringRequestEntity(jsonobject.toString()); method.setRequestEntity(requestEntity); httpClient.executeMethod(method); String result = method.getResponseBodyAsString(); return result; } public static String md5(String s) { char hexDigits[] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' }; try { byte[] btInput = s.getBytes(); // 获得MD5摘要算法的 MessageDigest 对象 MessageDigest mdInst = MessageDigest.getInstance("MD5"); // 使用指定的字节更新摘要 mdInst.update(btInput); // 获得密文 byte[] md = mdInst.digest(); // 把密文转换成十六进制的字符串形式 int j = md.length; char str[] = new char[j * 2]; int k = 0; for (int i = 0; i < j; i++) { byte byte0 = md[i]; str[k++] = hexDigits[byte0 >>> 4 & 0xf]; str[k++] = hexDigits[byte0 & 0xf]; } return new String(str); } catch (Exception e) { e.printStackTrace(); return null; } } }