1 public class SendMessageUtil {
2 // 短信发送相关数据
3 private static String strUserName = "";
4 private static String strPassword = "";
5 public static DynamicClientFactory dcf = null;
6 public static org.apache.cxf.endpoint.Client client = null;
7 static {
8 dcf = DynamicClientFactory.newInstance();
9 client = dcf.createClient("");
10 }
11
12
13 private static int post(String path, String json) throws Exception {
14 HttpClient client = new HttpClient();
15 PostMethod postMethod = new PostMethod(path);
16 postMethod.setRequestHeader("Content-Type", "application/json");
17 byte[] requestBytes = json.getBytes("utf-8"); // 将参数转为二进制流
18 InputStream inputStream = new ByteArrayInputStream(requestBytes, 0, requestBytes.length);
19 RequestEntity requestEntity = new InputStreamRequestEntity(inputStream,
20 requestBytes.length, "application/json; charset=utf-8"); // 请求体
21 postMethod.setRequestEntity(requestEntity);
22 int state = client.executeMethod(postMethod);
23 return state;
24 }
25
26 /**
27 * 异步发送短信
28 *
29 * @param strPhoneNos ,手机号码,多个时须采用;隔开。
30 * @param strMsg 发送的短信内容。
31 * @author 012169 2013-9-12 下午2:32:04
32 */
33 public static void sendMsg(String strPhoneNos, String strMsg) {
34 ClientCallback callback = new ClientCallback();
35 int intReply = 0;
36
37 try {
38
39 client.invoke(callback, "sendMsg", new Object[] { strPhoneNos, strMsg, strUserName, strPassword });
40 Object[] replys = callback.get();
41 for (Object o : replys) {
42 intReply = NumUtils.objectConvertToInt(o);
43 }
44 // intReply,0:用户名不存在,-1:用户密码错误,02:发送条数超本月短信上限,<0:发送失败,>0:发送成功
45 } catch (Exception e) {
46 log.error("发送错误:" + e.getMessage() + ";返回值:" + intReply + strPhoneNos + strMsg);
47 }
48 }
49
50 /**
51 * 同步发送短信
52 *
53 * @param strPhoneNos ,手机号码,多个时须采用;隔开。
54 * @param strMsg 发送的短信内容。
55 * @author 012169 2013-9-12 下午2:32:04
56 */
57 public static void sendMsgBySynchronously(String strPhoneNos, String strMsg) {
58 Object[] replys = null;
59 int intReply = 0;
60
61 try {
62 replys = client.invoke("sendMsg", new Object[] { strPhoneNos, strMsg, strUserName, strPassword });
63
64 for (Object o : replys) {
65 intReply = NumUtils.objectConvertToInt(o);
66 }
67
68 // intReply,0:用户名不存在,-1:用户密码错误,02:发送条数超本月短信上限,<0:发送失败,>0:发送成功
69
70 } catch (Exception e) {
71 log.error("发送错误:" + e.getMessage() + ";返回值:" + intReply + strPhoneNos + strMsg);
72 }
73
74 }
75 }