容联七陌短信/验证码

1.容联七陌云客服登录地址:https://kf.7moor.com ,需要注册账号,接口参考容联七陌开发者中心:http://developer.7moor.com/v2docs/sms/

package com.gsafety.operationmanagement.utils;

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

import org.apache.http.HttpEntity;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.util.EntityUtils;

import com.alibaba.fastjson.JSONObject;

public class MoorMessageUtil {

	/**
	 * 获取短信发送模板
	 * @return
	 */
	public static String getSmsTemplet() {
		String smsURL = MoorConfig.SMSTEMPLET_URL;
		String sig = MD5Util.getMd5(MoorConfig.accountID+MoorConfig.APISecret+DateUtil.getFullDateStr(new Date()));
		String authorization = MD5Util.encryptBASE64(MoorConfig.accountID+":"+DateUtil.getFullDateStr(new Date()));
		HttpPost httpPost = new HttpPost(smsURL.replace("accountID", MoorConfig.accountID).replace("SIG", sig));
		httpPost.addHeader("Accept","application/json;");
		httpPost.addHeader("Content-Type","application/json;charset=utf-8;");
		//httpPost.addHeader("Content-Length","256");
		httpPost.addHeader("Authorization",authorization);
		HttpClientBuilder builder = HttpClientBuilder.create();
		HttpClient client = builder.build();
		StringEntity requestEntity = null;
		HttpEntity entity = null;
		String requestModel = "{}";
		CloseableHttpResponse response = null;
		String rstMsg = "";
		try {
			requestEntity = new StringEntity(requestModel,"UTF-8");
			httpPost.setEntity(requestEntity);
			response = (CloseableHttpResponse) client.execute(httpPost);
			entity = response.getEntity();
			 rstMsg = EntityUtils.toString(entity,"UTF-8");
			System.out.println(rstMsg);
		}  catch (IOException e) {
			e.printStackTrace();
			return null;
		}
		return rstMsg;
	}
	
	/**
	 * 发送短信接口
	 * @return
	 */
	public static String SendMassage(JSONObject jsonObject) {
		String smsURL = MoorConfig.SENDSMS_URL;
		String sig = MD5Util.getMd5(MoorConfig.accountID+MoorConfig.APISecret+DateUtil.getFullDateStr(new Date()));
		String authorization = MD5Util.encryptBASE64(MoorConfig.accountID+":"+DateUtil.getFullDateStr(new Date()));
		HttpPost httpPost = new HttpPost(smsURL.replace("accountID", MoorConfig.accountID).replace("SIG", sig));
		httpPost.addHeader("Accept","application/json;");
		httpPost.addHeader("Content-Type","application/json;charset=utf-8;");
		httpPost.addHeader("Authorization",authorization);
		HttpClientBuilder builder = 	HttpClientBuilder.create();
		HttpClient client = builder.build();
		StringEntity requestEntity = null;
		HttpEntity entity = null;
		String requestModel = jsonObject.toJSONString();
		CloseableHttpResponse response = null;
		String rstMsg = "";
		try {
			requestEntity = new StringEntity(requestModel,"UTF-8");
			httpPost.setEntity(requestEntity);
			response = (CloseableHttpResponse) client.execute(httpPost);
			entity = response.getEntity();
			rstMsg = EntityUtils.toString(entity,"UTF-8");
			System.out.println(rstMsg);
		}  catch (IOException e) {
			e.printStackTrace();
		}
		return rstMsg;
	}
	
	
	public static void main(String[] args) {
		//getSmsTemplet();
		SendMassage(new JSONObject());
	}
	
}

  

原文地址:https://www.cnblogs.com/skj0330insn/p/10287816.html