package ce.cell.message.pool; import java.io.IOException; import java.net.URLEncoder; 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; import org.dom4j.Document; import org.dom4j.DocumentHelper; import org.dom4j.Element; /** * 短信发送 * @author needytwh * */ public class MessageUtils { private MessageUtils(){}; private static final String sendUrl="http://112.74.139.4:8002/sms3_api/xmlapi/send.jsp"; private static final String statusUrl="http://112.74.139.4:8002/sms3_api/xmlapi/report.jsp"; private static final String upUrl="http://112.74.139.4:8002/sms3_api/xmlapi/upmsg.jsp"; public static String sendMsg(String userid,String password,String phone,String content) throws HttpException, IOException{ Document createDocument = DocumentHelper.createDocument(); Element rootElement = DocumentHelper.createElement("root"); createDocument.setRootElement(rootElement); rootElement.addAttribute("userid", userid); 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); rootElement.addAttribute("seqid", seqid); rootElement.addAttribute("sign", sign); Element submitElement = rootElement.addElement("submit"); submitElement.addAttribute("phone", phone); submitElement.addAttribute("content", content); String result = submitUrl(createDocument,sendUrl); return result; } private static String submitUrl(Document createDocument,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(createDocument.asXML()); method.setRequestEntity(requestEntity); httpClient.executeMethod(method); String result = method.getResponseBodyAsString(); return result; } public static String getMsgStatus(String userid,String password) throws HttpException, IOException{ Document createDocument = DocumentHelper.createDocument(); Element rootElement = DocumentHelper.createElement("root"); createDocument.setRootElement(rootElement); rootElement.addAttribute("userid", userid); 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); rootElement.addAttribute("seqid", seqid); rootElement.addAttribute("sign", sign); String result = submitUrl(createDocument,statusUrl); return result; } public static String getMsgUp(String userid,String password) throws HttpException, IOException{ Document createDocument = DocumentHelper.createDocument(); Element rootElement = DocumentHelper.createElement("root"); createDocument.setRootElement(rootElement); rootElement.addAttribute("userid", userid); 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); rootElement.addAttribute("seqid", seqid); rootElement.addAttribute("sign", sign); String result = submitUrl(createDocument,upUrl); return result; } public static MessageUtils getInstance() { return new MessageUtils(); } 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; } } }