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 = "84";
        String PASSWORD = "sWFHz3JnS2xqKtm/4uIzeh9O3EbsotoMVC6Z9Fk9PjY8Zbeya8bexQ==";

        //发送手机短信
        String reqXML = buildRequestXMLString(MAS_ID, PASSWORD, "", "13970413084", "知道谁给你发信息了吗?嘻嘻,你猜!");
        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>13970413084</phone>
            <content>知道谁给你发信息了吗?嘻嘻,你猜!</content>
        </sms_info>
    </sms>
</svc_init>
  1. 发送成功后返回的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>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值