JavaDemo


    import java.io.IOException;
    import java.io.InputStream;
    import java.io.UnsupportedEncodingException;
    import java.text.SimpleDateFormat;
    import java.util.Date;
    import java.util.HashMap;
    import java.util.Map;

    import javax.ws.rs.core.Response;

    import org.apache.commons.codec.digest.DigestUtils;
    import org.apache.cxf.helpers.IOUtils;
    import org.apache.cxf.jaxrs.client.WebClient;
    import org.apache.log4j.Logger;

    import com.fasterxml.jackson.databind.ObjectMapper;

    public class NewApiDemo {
        private static final Logger log = Logger.getLogger(NewApiDemo.class);

        private static ObjectMapper objectMapper = new ObjectMapper();

        //private static String url = http://localhost:8080/DXBPortal/sms/submit;
        private static String url = http://192.168.20.153:8080/sms/submit;
        private static String appid = 100045;
        private static String appkey = 0D6F8869A9A53B4EBDA2E19AC006F461;

        public static void main(String[] args) throws IOException, InterruptedException {
            WebClient client = createClient(url);
            submit(client, 13777802386, 【摩迪纳箱包旗舰店】亲爱的 李榕枝,您购买的宝贝已发出,收到后有疑问欢迎来咨询在线客服,如对我们商品满意请给予我们五星好评。退订回TD);

        }

        private static WebClient createClient(String uri) {
            WebClient client = WebClient.create(uri);
            // 非常重要
            client.type(application/json;charset=UTF-8);
            return client;
        }

        /**
         * 短信提交请求
         * 
         * @return
         * @throws IOException
         */
        public static Map submit(WebClient client, String mobile, String content) throws IOException {
            String tradeNo = getTradeNo();
            Map params = new HashMap();
            params.put(tradeNo, tradeNo);// 必填参数
            params.put(appid, appid);// 必填参数
            params.put(mobile, mobile);
            params.put(content, content);
            params.put(extno, );// 扩展端口号
            params.put(xid, );
            String sign = MD5(mobile + content + appkey);
            params.put(sign, sign);
            String req = objectMapper.writeValueAsString(params);
            log.info(请求消息: + req);
            Response response = client.post(req);
            InputStream stream = (InputStream) response.getEntity();
            String rsp = IOUtils.toString(stream);
            log.info(响应消息: + rsp);
            @SuppressWarnings(rawtypes)
            Map resultMap = (Map) objectMapper.readValue(rsp, Map.class);
            log.info(resultMap: + resultMap);
            return resultMap;
        }

        public static String MD5(String text, String encoding) throws UnsupportedEncodingException {
            return DigestUtils.md5Hex(text.getBytes(encoding));
        }

        public static String MD5(String text) {
            try {
                return MD5(text, UTF-8);
            } catch (UnsupportedEncodingException e) {
                return ;
            }
        }

        public static String getTradeNo() {
            return new SimpleDateFormat(yyyyMMddHHmmssSSS).format(new Date())
                    + (int) (Math.random() * (1000 - 100) + 100);
        }

    }