java翼讯通短信对接

package test;


import com.alibaba.fastjson.JSON;
import message.ContainFilter;
import message.MessageContext;
import message.MwRuleSelectParam;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.net.URLEncoder;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;


public class test11 {
    public static void main(String[] args) throws ParseException, IOException, NoSuchAlgorithmException {
        String url = "https://sms.189ek.com/yktsms/send?appid=APPID&mobile=MOBILE&msg=MSG&sign=SIGN";
        String appid = "appid";
        //签名:【写bug】;内容:测试
        String msg = "【写bug】测试";
        String phone = "1******9,1******8";
        String appKey = "appkey";
        String sign = appid + phone + msg + appKey;
        sign = MD5(sign);
        System.out.println(sign);
        String msgEncode = URLEncoder.encode(msg,"utf-8");
        System.out.println(msgEncode);
        url = url.replaceAll("APPID", appid).replaceAll("MOBILE",phone)
                .replaceAll("MSG",msgEncode).replaceAll("SIGN",sign);

        String result = doPost(url,null,null);
        System.out.println(result);
    }

    public static String doPost(String url, Map<String, Object> param, Map<String, String> header) throws IOException {
        // 创建Httpclient对象
        HttpClient httpclient = HttpClients.createDefault();
        HttpPost httppost = new HttpPost(url);
        httppost.addHeader("Content-Type", "application/json; charset=utf-8");
        if (header != null) {
            for (Map.Entry<String, String> entry : header.entrySet()) {
                httppost.addHeader(entry.getKey(), entry.getValue());
            }
        }
        if(param != null){
            StringEntity se = new StringEntity(JSON.toJSONString(param), "utf-8");
            httppost.setEntity(se);
        }
        HttpResponse response = httpclient.execute(httppost);
        String result = null;
        if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
            result = EntityUtils.toString(response.getEntity());
        }
        return result;
    }



    public static String MD5(String strSrc) {
        try {
            MessageDigest md5 = MessageDigest.getInstance("MD5");
            byte[] b = strSrc.getBytes("utf-8");
            md5.update(b);
            String result = "";
            byte[] temp = md5.digest();
            String s = "";
            for (byte bb : temp) {
                s += (bb + " ");
            }

            for (int i = 0; i < temp.length; i++) {
                result += Integer.toHexString((0x000000ff & temp[i]) | 0xffffff00).substring(6);
            }
            return result;

        } catch (NoSuchAlgorithmException e) {


        } catch (Exception e) {

        }
        return "";
    }

    public static String getSendTouer(HashSet<String> sendPhones) {
        StringBuffer touser = new StringBuffer();
        if (sendPhones != null && sendPhones.size() > 0 && sendPhones.size() <= 1000) {
            Iterator iterator = sendPhones.iterator();
            while (iterator.hasNext()) {
                touser.append(",").append(iterator.next());
            }
        }else {
            return "";
        }
        touser.replace(0, 1, "");
        return touser.toString();
    }

    public static String getHostMac(InetAddress ia) throws SocketException {
        NetworkInterface byInetAddress = NetworkInterface.getByInetAddress(ia);
        byte[] mac = byInetAddress.getHardwareAddress();
        StringBuffer sb = new StringBuffer("");
        for (int i = 0; i <mac.length ; i++) {
            if(i!=0){
                sb.append("—");
            }
            int temp=mac[i]&0xff;
            String s = Integer.toHexString(temp);
            if(s.length()==1){
                sb.append("0"+s);
            }else{
                sb.append(s);
            }
        }
        return sb.toString().toUpperCase();
    }




}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值