package tea.ui.neea.chaxun;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
import org.apache.commons.codec.binary.Base64;
import org.apache.http.HttpEntity;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.config.RequestConfig;
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.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import org.json.JSONException;
import org.json.JSONObject;
public class SMSMessage {
private String projectId;//
private String smsType;
private String traceNo;
private String applicationId;
private String extCode;
private String receiverMobileNo;
private String smsContent;
private String status;
private String message;
/**
* 获取短信接口需要参数类
* @param mobile 手机号
* @param content 短信内容
* @return
*/
private SMSMessage getSMSMessage(String mobile,String content){
SMSMessage smsMessage = new SMSMessage();
smsMessage.setProjectId("XXX");
smsMessage.setSmsType("XXX");
UUID uuid = UUID.randomUUID();
smsMessage.setTraceNo(uuid.toString());
smsMessage.setApplicationId("XXX");
smsMessage.setExtCode("XX");
smsMessage.setReceiverMobileNo(mobile);
//base64加密短信内容
smsMessage.setSmsContent(Base64.encodeBase64String(content.getBytes()));
return smsMessage;
}
private List<NameValuePair> getParmList(SMSMessage smsMessage) {
List<NameValuePair> parmList = new ArrayList<NameValuePair>();
parmList.add(new BasicNameValuePair("projectId", smsMessage.getProjectId()));
parmList.add(new BasicNameValuePair("smsType", smsMessage.getSmsType()));
parmList.add(new BasicNameValuePair("traceNo", smsMessage.getTraceNo()));
parmList.add(new BasicNameValuePair("applicationId", smsMessage.getApplicationId()));
parmList.add(new BasicNameValuePair("extCode", smsMessage.getExtCode()));
parmList.add(new BasicNameValuePair("receiverMobileNo", smsMessage.getReceiverMobileNo()));
parmList.add(new BasicNameValuePair("smsContent", smsMessage.getSmsContent()));
return parmList;
}
private String getParms(SMSMessage smsMessage){
JSONObject obj = new JSONObject();
try {
obj.put("projectId", smsMessage.getProjectId());
obj.put("smsType", smsMessage.getSmsType());
obj.put("traceNo", smsMessage.getTraceNo());
obj.put("applicationId", smsMessage.getApplicationId());
obj.put("extCode", smsMessage.getExtCode());
obj.put("receiverMobileNo", smsMessage.getReceiverMobileNo());
obj.put("smsContent", smsMessage.getSmsContent());
} catch (JSONException e) {
e.printStackTrace();
}
return obj.toString();
}
private void sendMessage(String mobile,String content) {
String uri = "http://ip:99/soa/sms";
CloseableHttpClient httpClient = HttpClients.createDefault();
HttpPost post = new HttpPost(uri);//创建HttpPost对象
RequestConfig config = RequestConfig.custom()
.setConnectionRequestTimeout(10000).setConnectTimeout(10000)
.setSocketTimeout(10000).build();//设置请求和传输超时时间
CloseableHttpResponse response = null;
try {
SMSMessage smsMessage = getSMSMessage(mobile,content);
/*List<NameValuePair> parmList = getParmList(smsMessage);
post.setEntity(new UrlEncodedFormEntity(parmList, "UTF-8"));
普通表单,纯参数了,键值对,服务端直接获取request.getParameter
*/
//http post的json数据格式
String parms = getParms(smsMessage);
post.setEntity(new StringEntity(parms));
/*
Enumeration en = request.getParameterNames();
while (en.hasMoreElements()) {
String paramName = (String) en.nextElement();
String paramValue = request.getParameter(paramName);
}
*/
post.setConfig(config);
post.addHeader("Content-Type", "application/json");
post.addHeader("UserName", "XXX");
post.addHeader("Password", "XXX");
response = httpClient.execute(post);//执行请求
HttpEntity entity = response.getEntity();
if(response.getStatusLine().getStatusCode() == 200){
if (entity != null) {//如果没有错误返回码
String json_str = EntityUtils.toString(entity);
System.out.println("content:" + content);
String info = "";
if(json_str!=null){
JSONObject obj = new JSONObject(json_str);
String status = (String)obj.get("status");
String message = (String)obj.get("message");
info = "status:"+status+",message:"+message;
System.out.println(info);
}
}
}
EntityUtils.consume(entity);//关闭HttpEntity的流
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (JSONException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
response.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
public static void main(String[] args) {
SMSMessage smsMessage = new SMSMessage();
smsMessage.setReceiverMobileNo("13012345678");
smsMessage.setSmsContent("测试短信");
smsMessage.sendMessage("13012345678", "测试短信");
}
public String getProjectId() {
return projectId;
}
public void setProjectId(String projectId) {
this.projectId = projectId;
}
public String getSmsType() {
return smsType;
}
public void setSmsType(String smsType) {
this.smsType = smsType;
}
public String getTraceNo() {
return traceNo;
}
public void setTraceNo(String traceNo) {
this.traceNo = traceNo;
}
public String getApplicationId() {
return applicationId;
}
public void setApplicationId(String applicationId) {
this.applicationId = applicationId;
}
public String getExtCode() {
return extCode;
}
public void setExtCode(String extCode) {
this.extCode = extCode;
}
public String getReceiverMobileNo() {
return receiverMobileNo;
}
public void setReceiverMobileNo(String receiverMobileNo) {
this.receiverMobileNo = receiverMobileNo;
}
public String getSmsContent() {
return smsContent;
}
public void setSmsContent(String smsContent) {
this.smsContent = smsContent;
}
public String getStatus() {
return status;
}
public void setStatus(String status) {
this.status = status;
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
}