package cn.util;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.List;
import cn.ccb.yn.acms.system.vo.ShortReceiveMsgVo;
import cn.ccb.yn.acms.system.vo.ShortSendMsgVo;
public class ShortMsgUtil {
/**
* 短信的发送和接受接口
* @param getURL
* @return
*/
private Constant c = null;
private static final String reqForGet(String getURL) {
try {
URL url = new URL(getURL);
URLConnection urlConn = url.openConnection();
HttpURLConnection httpUrlConn = (HttpURLConnection) urlConn;
// 默认情况下是false;
httpUrlConn.setDoOutput(false);
// 设置是否从httpUrlConnection读入,默认情况下是true
httpUrlConn.setDoInput(true);
// Get 请求不能使用缓存
httpUrlConn.setUseCaches(false);
// 设定请求的方法为"GET",默认是GET
httpUrlConn.setRequestMethod("GET");
BufferedReader in = new BufferedReader(new InputStreamReader(httpUrlConn
.getInputStream(),"utf-8"));
String line;
StringBuffer sb = new StringBuffer();
while ((line = in.readLine()) != null) {
sb.append(line);
}
in.close();
return (sb.toString());
} catch (Exception e) {
e.printStackTrace();
}
return "";
}
/**
* 短信的发送
* @param args
*/
public String sendMsg(ShortSendMsgVo Vo){
if(null!= Vo && !"".equals(Vo)){
c = new Constant();
StringBuffer bf = new StringBuffer();
bf.append("USERNAME="+c.getFilePath("USERNAME")+"&PASSWORD="+c.getFilePath("PASSWORD")+"&SCODE='"+Vo.getScode()+"'");
bf.append("&MOBILE=");
List<String> mobile = Vo.getMobile();
String mobileStr = "";
for(int i=0,len=mobile.size();i<len;i++){
mobileStr+= mobile.get(i).toString()+",";
}
String bi = mobileStr.substring(0, mobileStr.lastIndexOf(","));
bf.append(bi).append("&CONTENT='"+Vo.getContent()+"'");
String url = c.getFilePath("SENDMSGIP")+"?"+bf.toString();
String result = reqForGet(url);
return result;
}
return "fail";
}
/**
* 短信的接收
* @param args
*/
public List<ShortReceiveMsgVo> receIveMsg(String scode){
String url = c.getFilePath("SENDMSGIP")+"?"+c.getFilePath("USERNAME")+"&PASSWORD="+c.getFilePath("PASSWORD")+"&SCODE='"+scode+"'";
String result = reqForGet(url);
List<ShortReceiveMsgVo> vo = null;
if(!"NOMESSAGE".equals(result)){
vo = new ArrayList<ShortReceiveMsgVo>();
String msg[] = result.split(";");
vo = factoryData(msg);
}
return vo;
}
private List<ShortReceiveMsgVo> factoryData(String a[]){
List<ShortReceiveMsgVo> vo = new ArrayList<ShortReceiveMsgVo>();
ShortReceiveMsgVo v = null;
for(int i=0,len=a.length;i<len;i++){
v = new ShortReceiveMsgVo();
String m[] = a[i].split("\\");
v.setMobile(m[0]);
v.setContent(m[1]);
v.setReceiveTime(m[2]);
vo.add(v);
}
return vo;
}
public static void main(String args[]){
reqForGet("http://wgyblog.com");
}
}