调用网易短信接口(发短信模板通知)

5 篇文章 0 订阅
4 篇文章 0 订阅

调用短信接口出现{"code":414,"msg":"miss param"}错误的解决办法,所需jar包 如下截图

import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.utils.URLEncodedUtils;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;


public class WangYiMessage {
	//请求路径
	private static final String URLS = "";
	//appKey
	private static final String APP_KEY ="";
	//
	private static final String APP_SECRET ="";
	//短信模板id
	private static final String TEMPLATEID ="";//"3108039"s;
	//随机参数
	private static final String NONCE = "";
	
	public void wangYiMSM() throws ClientProtocolException, IOException{
		
		DefaultHttpClient httpCline = new DefaultHttpClient();
		
		HttpPost httpPost = new HttpPost(URLS);
		
		String curTime  = String.valueOf(new Date().getTime()/1000L);
		
		String  checkSum = CheckSumBuilder.getCheckSum(APP_SECRET, NONCE, curTime);	
		
        httpPost.addHeader("AppKey", APP_KEY);
        httpPost.addHeader("Nonce", NONCE);
        httpPost.addHeader("CurTime", curTime);
        httpPost.addHeader("CheckSum", checkSum);
        httpPost.addHeader("Content-Type", "application/x-www-form-urlencoded;charset=utf-8");

        //电话号码
		JSONArray json = new JSONArray();
		json.add("17**********");
		
		//短信内容(此处的短信内容是你在网易短信模板申请中的变量【%s】这种,
		//值得注意的是你申请了多少个%S变量就必须填写多少个但是每个变量不能大于30个字:比如我申请了11个那么我就要传11个变量)
		//如果参数少了会提示{"code":414,"msg":"miss param"}大概意思是说缺少参数
		JSONArray json1 = new JSONArray();

		//json1.add("222");
		//json1.add("https:www.dingteam.com");
		
		
		System.out.println(json1.toString());
		
		//json.add(value)
		List<NameValuePair> nvp = new ArrayList<NameValuePair>();
		nvp.add(new BasicNameValuePair("templateid",TEMPLATEID));
		
		nvp.add(new BasicNameValuePair("mobiles","[\"187709***\"]"));
		
		nvp.add(new BasicNameValuePair("params",json1.toString()));
		
	
		
		httpPost.setEntity(new UrlEncodedFormEntity(nvp,"utf-8"));
		
		HttpResponse response = httpCline.execute(httpPost);
		 
		System.out.println(EntityUtils.toString(response.getEntity(), "utf-8"));

	}
	
	public static void  main(String... args) throws ClientProtocolException, IOException{
	
			WangYiMessage   d= new WangYiMessage();
			d.wangYiMSM();

		
	}
	
	
	
	

	
	
	
	
	
	
	
	
	
	
	
	
	
	


}

import java.security.MessageDigest;


public class CheckSumBuilder {
    // 计算并获取CheckSum
    public static String getCheckSum(String appSecret, String nonce, String curTime) {
        return encode("sha1", appSecret + nonce + curTime);
    }

    // 计算并获取md5值
    public static String getMD5(String requestBody) {
        return encode("md5", requestBody);
    }

    private static String encode(String algorithm, String value) {
        if (value == null) {
            return null;
        }
        try {
            MessageDigest messageDigest
                    = MessageDigest.getInstance(algorithm);
            messageDigest.update(value.getBytes());
            return getFormattedText(messageDigest.digest());
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
    private static String getFormattedText(byte[] bytes) {
        int len = bytes.length;
        StringBuilder buf = new StringBuilder(len * 2);
        for (int j = 0; j < len; j++) {
            buf.append(HEX_DIGITS[(bytes[j] >> 4) & 0x0f]);
            buf.append(HEX_DIGITS[bytes[j] & 0x0f]);
        }
        return buf.toString();
    }
    private static final char[] HEX_DIGITS = { '0', '1', '2', '3', '4', '5',
            '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' };
}

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值