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

调用短信接口出现{"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 nvp = new ArrayList();
		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' };
}

调用网易短信接口(发短信模板通知)_第1张图片

你可能感兴趣的:(java,Java,短信,接口)