SSH Frame Online Mall Item 26th Battle Order payment send SMS Reminder _java

Source: Internet
Author: User
Tags ssh

In the previous section we used Java mail to complete the function of sending a message to a buyer, and left a function to send a message to the buyer telling him that the order had been generated. This section describes how to automatically send text messages to users after payment is completed.

1. Request SMS Send function

Obviously, we can not send text messages, nor do we have the function of texting, so we need to use third-party operators. But like China Mobile, unicom, and telecoms do not directly provide us with text messaging services, but first SMS services to some operators, and then these operators to provide us.
There is an operator called "China Network to build SMS Messaging" (official website address). This operator can provide us with SMS services, access to the official website can look at the charging standards, of course, the first registration free to send 5 text messages, but the free may not be too stable, or send a longer time, it depends on the character, but the pay is basically very good. His profit model is to use a lower price from mobile, unicom or telecom to get text messages, and then sold to users, of course, there are preferential packages, this is generally large companies or have to send text messages to users of the business will buy.
First we go to register an account (in fact, to get the 5 free short interest to test our program), in order to make the process clear, I try to write more detail, please see the following interface:


  

Like these names Ah, email ah can be casually filled, mobile phone fill their own line, because you will receive the login username and password. After landing we can see that there are 5 free text messages that can be sent as follows:


  

Tips:5 free text message The first day do not use up, the next day will send 5 again, and then will not send ~
Then we click on the image above to send a text message, will pop up a message to send the interface, we are not to send text messages, but to set some of the suffix of the message, that is, after the user received a message, the last side has a suffix to explain from where, as follows:
  


  

After saving, we clicked on the "SMS API Interface" in the left column, will jump out of the API for sending text messages (which also gives the address of that connection), we'll need to refer to the API they provide to us, just like the previous payment function, using Yeepay to provide us with the official interface. Let's take a quick look at:

The above is only part of the content, the details can be clicked on the link above. We click on the image of the key immediately modify, mainly to see what the key is, and then copy the key down, save, and then write the program to use.
OK, now the preparation is done, the API has, now we can start to develop the text message function.

2. Test the third party server is normal or not

Before the development of SMS function, we first test the Chinese network built the server is normal, according to the SMS API page provided by the data, we can use UTF-8 code to send the address of the interface:

http://utf8.sms.webchinese.cn/?Uid= Local user name &key= interface security key &smsmob= mobile number &smstext= verification code: 8888
To test whether the utf8.sms.webchines.cn is normal. Below, write a test.jsp page in the project:

<%@ page language= "java" import= "java.util.*" pageencoding= "UTF-8"%> <!
DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" >
 
 

(Friendship tip: In the test time not many tests, once is enough, mainly to see whether the server is normal, but also have to leave a few times behind the test.) You can also write a parameter incorrectly, if you return a negative number, the server is normal, you can according to the parameters provided by the API page to see the corresponding negative number represents what went wrong. This is only used for testing, in practice can not be so to send, because the link also has my username and password, certainly not. )

3. The development of SMS send function

###**3.1 Send SMS Process Test * * Using SMS send function, need the following three jar packages: (Free download Address: http://download.csdn.net/detail/eson_15/9528009)-' Commons-codec-1.4.jar '-' commons-httpclient-3.1.jar '-' commons-logging-1.1.1.jar ' let's write a common Java class to complete the SMS delivery function, and test correctness:

 public class Sendmessagedemo {public static void main (string[] args) throws Excepti
    On {//1. Open browser HttpClient client = new HttpClient (); 2. How to create the request: Get/post Postmethod post = new Postmethod ("http://utf8.sms.webchinese.cn/");//The server address that we just applied for is//3. Sets the parameter information for the request Post.setrequestheader ("Content-type", "application/x-www-form-urlencoded;charset=utf-8");//Set transcoding post.se
    Tparameter ("Uid", "xxxx");/all parameters are the same as above Post.setparameter ("Key", "xxxx");
    Post.setparameter ("Smsmob", "xxxx"); Post.setparameter ("Smstext", "Test!")
    "); 4.
    Submits the request and returns the status code int code = Client.executemethod (POST); System.out.println ("Status Code of HTTP return:" + code);//If printing out 200 indicates normal//5.
    Gets the data information returned by the server end String result = Post.getresponsebodyasstring (); SYSTEM.OUT.PRINTLN ("SMS Send result:" + results);//If return 1 instructions to send normal}} 

You can then run this Java program to send yourself a text test. My test results are as follows:! [Test results] (http://img.blog.csdn.net/20160523122802066) ###**3.2 encapsulated into Messageutilimpl * * Next we'll use this feature in our own projects, We encapsulate the above process into the Messageutilimpl tool class and extract the interface as follows:

The extracted Messageutil interface is public interface Messageutil {public abstract void SendMessage (string phonenum, string id);  Messageutilimpl Implementation Class @Component ("Messageutil") public class Messageutilimpl implements Messageutil {@Override public
    void SendMessage (String phonenum, string id) {//1. Open browser HttpClient client = new HttpClient (); 2.
    How to create the request: Get/post Postmethod post = new Postmethod ("http://utf8.sms.webchinese.cn/"); 3.
    Sets the parameter information for the request Post.setrequestheader ("Content-type", "application/x-www-form-urlencoded;charset=utf-8");
    Post.setparameter ("Uid", "eson_15");
    Post.setparameter ("Key", "0537729d0b59a7e7358b");
    Post.setparameter ("Smsmob", "15216771570"); Post.setparameter ("Smstext", "Test!")
    "); 4.
      Submit the request and return the status code try {int code = 0;
      Code = Client.executemethod (POST);
      System.out.println ("HTTP Return status code:" + code); 5.
      Gets the data information returned by the server end String result = Post.getresponsebodyasstring (); SYSTEM.OUT.PRINTLN ("SMS sent the result:" + resULT);
      catch (Exception e) {e.printstacktrace ();
    throw new RuntimeException (e);
    finally {post.releaseconnection ();

 }
  }
}

4. Perfect Payaction

After completing the encapsulation of the tool class, we then put the tool class into the baseaction and inject it through the @resource annotation for action, and we refine the Backbank () method in the previous payaction, as follows:

 @Controller ("Payaction") @Scope ("prototype") public class Payaction extends Baseaction <Object> implements Parameteraware {//Omit extraneous code ... public void Backbank () {backdata backdata = (backdata) mod
    El
    SYSTEM.OUT.PRINTLN (model);
    Boolean IsOK = Payservice.checkbackdata (backdata); if (IsOK) {//1. Update order status, parameters are passed in according to the situation in the database to test the Forderservice.updatestatusbyid (Integer.parseint backdata.getr6_
      Order ()), 2); 2.
      According to the user's mailbox address, send message String EmailAddress = BACKDATA.GETR8_MP (). Split (",") [0];
      Emailutil.sendemail (EmailAddress, Backdata.getr6_order ()); 3.
      Send Mobile SMS String phonenum = BACKDATA.GETR8_MP (). Split (",") [1];
      Messageutil.sendmessage (Phonenum, Backdata.getr6_order ()); SYSTEM.OUT.PRINTLN ("----success!!
    ----"); else {System.out.println ("----false!!!
    ----"); }
  }
}

From the information returned from the PO r6_order parameter in the store is the number of orders, R8_MP parameter is the user's mailbox and telephone, the first is the second mailbox is a mobile phone number, separated by commas, so we first need to obtain the user's mobile phone number, and then to send text messages. OK, the function of sending SMS to the user after payment is completed.

Original link: http://blog.csdn.net/eson_15/article/details/51475431

The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.

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.