com.beginner.core.utils.SmsUtil.java Source code

Java tutorial

Introduction

Here is the source code for com.beginner.core.utils.SmsUtil.java

Source

/*
 * Copyright 2015-9999 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.beginner.core.utils;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.HttpUriRequest;

import com.beginner.core.plugin.PageData;

/**
 * ???
 */
public class SmsUtil {

    public static void main(String[] args) {

        sendSms2("415646565", "??1111????");
        //sendSmsAll(List<PageData> list)

        //sendSms1();
    }

    //   http://www.dxton.com/ =====================================================================================
    /**
     * ????
     * @param mobile ?
     * @param code  
     */
    public static void sendSms1(String mobile, String code) {
        String account = StringUtils.EMPTY, password = StringUtils.EMPTY, strSMS1 = StringUtils.EMPTY;
        if (null != strSMS1 && !"".equals(strSMS1)) {
            String strS1[] = strSMS1.split(",beginner,");
            if (strS1.length == 2) {
                account = strS1[0];
                password = strS1[1];
            }
        }
        String PostData = "";
        try {
            PostData = "account=" + account + "&password=" + password + "&mobile=" + mobile + "&content="
                    + URLEncoder.encode(code, "utf-8");
        } catch (UnsupportedEncodingException e) {
            System.out.println("??");
        }
        //System.out.println(PostData);
        String ret = SMS(PostData, "http://sms.106jiekou.com/utf8/sms.aspx");
        System.out.println(ret);
        /*  
        100         ???
        101         ?
        102         ????
        103         ?
        104         
        105         
        106         ??
        107         Ip??
        108         ?????????
        109         ???
        110         ????
        111         ????
        120         ?
        */

    }

    public static String SMS(String postData, String postUrl) {
        try {
            //??POST
            URL url = new URL(postUrl);
            HttpURLConnection conn = (HttpURLConnection) url.openConnection();
            conn.setRequestMethod("POST");
            conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
            conn.setRequestProperty("Connection", "Keep-Alive");
            conn.setUseCaches(false);
            conn.setDoOutput(true);

            conn.setRequestProperty("Content-Length", "" + postData.length());
            OutputStreamWriter out = new OutputStreamWriter(conn.getOutputStream(), "UTF-8");
            out.write(postData);
            out.flush();
            out.close();

            //???
            if (conn.getResponseCode() != HttpURLConnection.HTTP_OK) {
                System.out.println("connect failed!");
                return "";
            }
            //??
            String line, result = "";
            BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream(), "utf-8"));
            while ((line = in.readLine()) != null) {
                result += line + "\n";
            }
            in.close();
            return result;
        } catch (IOException e) {
            e.printStackTrace(System.out);
        }
        return "";
    }

    //===================================================================================================================

    /**
     * 
     *    http://www.ihuyi.com/ =====================================================================================
     * 
     */
    private static String Url = "http://106.ihuyi.com/webservice/sms.php?method=Submit";

    /**
     * ????
     * @param mobile ?
     * @param code  
     */
    public static void sendSms2(String mobile, String code) {
        HttpUriRequest method = new HttpPost(Url);

        method.setHeader("ContentType", "application/x-www-form-urlencoded;charset=UTF-8");

        String strSMS2 = "";//2015/08/20 Tools.readTxtFile(Const.SMS2);         //?2?
        if (null != strSMS2 && !"".equals(strSMS2)) {
            String strS2[] = strSMS2.split(",beginner,");
            if (strS2.length == 2) {
            }
        }

        //      NameValuePair[] data = {//??
        //      new NameValuePair("account", account), new NameValuePair("password", password), //???32?MD5
        //            new NameValuePair("mobile", mobile), new NameValuePair("content", content), };
        //
        //      method.setRequestBody(data);

        //try {
        //client.executeMethod(method);

        //String SubmitResult = method.getResponseBodyAsString();

        //         Document doc = DocumentHelper.parseText(SubmitResult);
        //         Element root = doc.getRootElement();
        //
        //         code = root.elementText("code");
        //         String msg = root.elementText("msg");
        //         String smsid = root.elementText("smsid");
        //
        //         System.out.println(code);
        //         System.out.println(msg);
        //         System.out.println(smsid);
        //
        //         if (code == "2") {
        //            System.out.println("???");
        //         }

        //      } catch (HttpException e) {
        //         e.printStackTrace();
        //      } catch (IOException e) {
        //         e.printStackTrace();
        //      } catch (DocumentException e) {
        //         e.printStackTrace();
        //      }

    }

    /**
     * ????
     * @param list ???
     */
    public static void sendSmsAll(List<PageData> list) {
        String code;
        String mobile;
        for (int i = 0; i < list.size(); i++) {
            code = list.get(i).get("code").toString();
            mobile = list.get(i).get("mobile").toString();
            sendSms2(mobile, code);
        }
    }
    // =================================================================================================

}