Java Send Short Message function tool class and thinking detailed

Source: Internet
Author: User

//Send SMS features the following is the tool class
exception return 0 Send SMS failure can be result==0 to judge then return failure information
If successful, return the validation code information can save session
and set session time SESSION.S Etmaxinactiveinterval (60*5);
Set session time post-validation can get session if the session is empty
The validation code has expired or equals session, set session null and return success

Package com.kero99.ygc.sms;
Import org.apache.commons.httpclient.HttpClient;
Import Org.apache.commons.httpclient.NameValuePair;
Import Org.apache.commons.httpclient.methods.PostMethod;
Import org.dom4j.Document;
Import Org.dom4j.DocumentHelper;
Import org.dom4j.Element;
 /** * Interface type: Mutual billion wireless trigger SMS interface, to support the sending of authentication code text messages, order notification messages and so on. * Account Registration: Please open the account through the address http://sms.ihuyi.com/register.html * NOTE: * (1) during debugging, please use the default template for testing, the default template for details of the interface document; * (2) Please use APIID (View API ID Please login to the User Center-> authentication Code, notification SMS-> account and signature settings->apiid) and Apikey to invoke the interface, Apikey in the Member Center can be obtained; * (3) The code is only for access to the mutual billion wireless SMS Interface reference use, Customers can write their own according to the actual needs;/public class Sendutils {private static String Url = "Http://106.ihuyi.cn/webservice/sms.php?method
    =submit ";
        public static int sendsms (String phone) {httpclient client = new HttpClient ();

        Postmethod method = new Postmethod (URL);
        Client.getparams (). Setcontentcharset ("GBK");

        Method.setrequestheader ("ContentType", "APPLICATION/X-WWW-FORM-URLENCODED;CHARSET=GBK"); int mobile_code = (int) (MAth.random () *9+1) *100000); String content = new String ("Your verification code is: + Mobile_code +".) Please do not divulge the verification code to other people.

        "); namevaluepair[] data = {//Submit SMS New Namevaluepair ("Account", "c72562***"),//view user name please login to User Center-> authentication Code, notification SMS-> Account and Signature settings->apiid new Namevaluepair ("Password", "ad95c26915ec51bd68d87a72***"),//view password Please login User Center-> authentication Code, short notice Letter-> Account and signature settings->apikey//new namevaluepair ("password", util. Stringutil.md5encode ("password"), New Namevaluepair ("mobile", phone),//Mobile number new Namevaluepair (
        Content),//contents};

        Method.setrequestbody (data);

            try {Client.executemethod (method);

            String Submitresult =method.getresponsebodyasstring ();

            System.out.println (Submitresult);
            Document doc = Documenthelper.parsetext (submitresult);

            Element root = Doc.getrootelement ();
            String 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 ("2". Equals (code)) {System.out.println ("text message submitted successfully");    
            return mobile_code;

        }else return 0;
            catch (Exception e) {e.printstacktrace ();
        return 0;
    }//test public static void main (string[] args) {sendsms ("13521******");



 }
}
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.