吉信通工具類

package com.kayo.bos.utils;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.net.URLEncoder;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import org.apache.commons.lang3.RandomStringUtils;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

public class SmsUtils {
    private static String userid = "seawind";
    private static String pass = "itcast123456";

    /**
     * 調用HTTP 協議方式發送短信
     * 
     * @param mobile
     * @param content
     * @return
     * @throws UnsupportedEncodingException
     */
    public static String sendSmsByHTTP(String mobile, String content)
            throws UnsupportedEncodingException {
        HttpURLConnection httpconn = null;
        String result = "Error";
        StringBuilder sb = new StringBuilder();
        sb.append("http://service.winic.org:8009/sys_port/gateway/index.asp?");

        // 以下是參數
        sb.append("id=").append(URLEncoder.encode(userid, "gb2312"));
        sb.append("&pwd=").append(pass);
        sb.append("&to=").append(mobile);
        sb.append("&content=").append(URLEncoder.encode(content, "gb2312"));
        sb.append("&time=").append("");
        try {
            URL url = new URL(sb.toString());
            httpconn = (HttpURLConnection) url.openConnection();
            BufferedReader rd = new BufferedReader(new InputStreamReader(
                    httpconn.getInputStream()));
            result = rd.readLine();
            rd.close();
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (httpconn != null) {
                httpconn.disconnect();
                httpconn = null;
            }
        }
        return result;
    }

    /**
     * 調用 WebService 協議方式發送短信
     * 
     * @param mobiles
     * @param msg
     * @return
     */
    public static String sendSmsByWebService(String mobiles, String msg) {
        String result = "-12";
        try {
            Document doc;
            DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
            dbf.setNamespaceAware(true);
            DocumentBuilder db = dbf.newDocumentBuilder();
            InputStream is = getSoapInputStream(userid, pass, mobiles, msg, "");
            if (is != null) {
                doc = db.parse(is);
                NodeList nl = doc.getElementsByTagName("SendMessagesResult");
                Node n = nl.item(0);
                result = n.getFirstChild().getNodeValue();
                is.close();
            }
            return result;
        } catch (Exception e) {
            System.out.print("SmsSoap.sendSms error:" + e.getMessage());
            return "-12";
        }
    }

    private static String getSoapSmssend(String userid, String pass,
            String mobiles, String msg, String time) {
        try {
            String soap = "";
            soap = "<?xml version=\"1.0\" encoding=\"utf-8\"?>"
                    + "<soap:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\">"
                    + "<soap:Body>"
                    + "<SendMessages xmlns=\"http://tempuri.org/\">" + "<uid>"
                    + userid + "</uid>" + "<pwd>" + pass + "</pwd>" + "<tos>"
                    + mobiles + "</tos>" + "<msg>" + msg + "</msg>" + "<otime>"
                    + time + "</otime>" + "</SendMessages>" + "</soap:Body>"
                    + "</soap:Envelope>";
            return soap;
        } catch (Exception ex) {
            ex.printStackTrace();
            return null;
        }
    }

    private static InputStream getSoapInputStream(String userid, String pass,
            String mobiles, String msg, String time) throws Exception {
        URLConnection conn = null;
        InputStream is = null;
        try {
            String soap = getSoapSmssend(userid, pass, mobiles, msg, time);
            if (soap == null) {
                return null;
            }
            try {

                URL url = new URL("http://service2.winic.org:8003/Service.asmx");

                conn = url.openConnection();
                conn.setUseCaches(false);
                conn.setDoInput(true);
                conn.setDoOutput(true);
                conn.setRequestProperty("Content-Length",
                        Integer.toString(soap.length()));
                conn.setRequestProperty("Content-Type",
                        "text/xml; charset=utf-8");
                conn.setRequestProperty("HOST", "service2.winic.org");
                conn.setRequestProperty("SOAPAction",
                        "\"http://tempuri.org/SendMessages\"");

                OutputStream os = conn.getOutputStream();
                OutputStreamWriter osw = new OutputStreamWriter(os, "utf-8");
                osw.write(soap);
                osw.flush();
            } catch (Exception ex) {
                System.out.print("SmsSoap.openUrl error:" + ex.getMessage());
            }
            try {
                is = conn.getInputStream();
            } catch (Exception ex1) {
                System.out.print("SmsSoap.getUrl error:" + ex1.getMessage());
            }

            return is;
        } catch (Exception e) {
            System.out.print("SmsSoap.InputStream error:" + e.getMessage());
            return null;
        }
    }

    public static void main(String[] args) throws IOException {
        String randomCode = RandomStringUtils.randomNumeric(4);
        // System.out.println(sendSmsByHTTP("xxx", randomCode));
        // System.out.println(sendSmsByHTTP("xxx", "尊敬的用戶您好,本次獲取的驗證碼爲:"
        // + randomCode + ",服務電話:4006184000"));
        System.out.println(sendSmsByWebService("xxx", "尊敬的用戶您好,本次獲取的驗證碼爲:"
                + randomCode + ",服務電話:4006184000"));
    }
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章