Java发送手机短信

一、需求

通过MAS: 移动代理服务器,向用户发送手机短信。
由移动提供WebService接口,在代码中调用该接口即可。

二、实现代码

package com.wbf.test;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.URL;

public class Test {
	public static String buildRequestXMLString(String id, String pwd, String serviceid, String phone, String content) {
		StringBuffer sb = new StringBuffer();

		sb.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>").append(
				"<svc_init ver=\"2.0.0\">").append("<sms ver=\"2.0.0\">")
				.append("<client>").append("<id>").append(id).append("</id>")
				.append("<pwd>").append(pwd).append("</pwd>").append(
						"<serviceid>").append(serviceid).append("</serviceid>")
				.append("</client>").append("<sms_info>").append("<phone>")
				.append(phone).append("</phone>").append("<content>").append(
						content).append("</content>").append("</sms_info>")
				.append("</sms>").append(" </svc_init>");

		System.out.println(sb.toString());
		return sb.toString();
	}

	public static String buildRequestXMLString2Query(String id, String pwd) {
		StringBuffer sb = new StringBuffer();

		sb.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>").append(
				"<svc_init ver=\"2.0.0\">")
					.append("<sms ver=\"2.0.0\">")
						.append("<client>")
							.append("<id>").append(id).append("</id>")
							.append("<pwd>").append(pwd).append("</pwd>")
						.append("</client>")
					.append("</sms>")
				.append(" </svc_init>");

		System.out.println(sb.toString());
		return sb.toString();
	}
	
	public static String postXMLSendSMSRequest(String servletUrl, String content) {
		String result = null;

		BufferedReader br = null;
		OutputStreamWriter out = null;
		HttpURLConnection con = null;

		try {
			URL url = new URL(servletUrl);

			con = (HttpURLConnection) url.openConnection();
			con.setDoOutput(true);
			con.setRequestMethod("POST");

			out = new OutputStreamWriter(con.getOutputStream(), "UTF-8");

			out.write(content);

			out.flush();

			br = new BufferedReader(new InputStreamReader(con.getInputStream(),
					"UTF-8"));

			String line = null;

			StringBuilder sb = new StringBuilder();

			while ((line = br.readLine()) != null) {
				sb.append(line);
			}

			result = sb.toString();

			System.out.println(result);
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			if (br != null) {
				try {
					br.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}

			if (out != null) {
				try {
					out.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}

			if (con != null) {
				con.disconnect();
				con = null;
			}
		}

		return result;
	}

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// 下面的MAS_ID、PASSWORD仅供测试使用,正式使用由移动公司分配
		String MAS_ID = "88";
		String PASSWORD = "sWFHzxJnS2xqKtm/4uIzeh9O3EbsotoMVC6Z9Fk9PjY8Zbeya8bexQ==";

		//发送手机短信
		String reqXML = buildRequestXMLString(MAS_ID, PASSWORD, "", "13988888888", "知道谁给你发信息了吗?嘻嘻,你猜!");
		postXMLSendSMSRequest("http://218.204.149.110:18080/sjb/HttpSendSMSService", reqXML);
		
		//查询短信发送记录
		//String reqXML2Query = buildRequestXMLString2Query(MAS_ID, PASSWORD);
		//postXMLSendSMSRequest("http://218.204.149.110:18080/sjb/HttpDeliverySMSService", reqXML2Query);
	}

}

三、运行结果

1) 发送到服务器的XML字符串

<?xml version="1.0" encoding="UTF-8"?>
<svc_init ver="2.0.0">
    <sms ver="2.0.0">
        <client>
            <id>84</id>
            <pwd>sWFHz3JnS2xqKtm/4uIzeh9O3EbsotoMVC6Z9Fk9PjY8Zbeya8bexQ==</pwd>
            <serviceid/>
        </client>
        <sms_info>
            <phone>1398888888</phone>
            <content>知道谁给你发信息了吗?嘻嘻,你猜!</content>
        </sms_info>
    </sms>
</svc_init>

2) 发送成功服务器返回的XML字符串

<?xml version="1.0" encoding="UTF-8"?>
<svc_result ver="2.0.0">
    <response_info>
        <gwid>679d49be-8d44-4355-af3b-ed6fc54142d8</gwid>
        <retcode>00</retcode>
        <retmesg>OK</retmesg>
    </response_info>
</svc_result>

四、注意问题

移动代理服务器做了如下限定:

1) 向移动提供部署程序的服务器ip地址,移动会由ip地址生成MAS_ID和PASSWORD,只有此ip所对应的pc可以成功访问移动的服务器,其他ip对应的pc是无法连接到移动服务器的。

2) 预先向移动提供接收短信的手机号码,其他没有提供的手机号码是不会发短信的。

猜你喜欢

转载自javawangbaofeng.iteye.com/blog/2209223