First, register and log in building a network user, newly registered users will receive the test message 5
Built-messaging network address: http://sms.webchinese.cn/default.shtml
Register an account here do not go into details, directly on the code
1. Import dependency in pom.xml file
<!--短信依赖--> <dependency> <groupId>commons-logging</groupId> <artifactId>commons-logging</artifactId> <version>1.1.1</version> </dependency> <dependency> <groupId>commons-codec</groupId> <artifactId>commons-codec</artifactId> <version>1.4</version> </dependency> <dependency> <groupId>commons-httpclient</groupId> <artifactId>commons-httpclient</artifactId> <version>3.0.1</version> </dependency>
2. Create a new SMS tool class SendMessageUtil
Import org.apache.commons.httpclient.Header; Import org.apache.commons.httpclient.HttpClient; Import org.apache.commons.httpclient.NameValuePair; Import org.apache.commons.httpclient.methods.PostMethod; Import Classes in java.util .Random; public class SendMessageUtil { Private static Final String SMS_Url = "http://sms.webchinese.cn/web_api/" ; / ** * @param Uid the SMS user id: SMS account * @param Key Interface keys: SMS Log in to be investigated (non-password) * @param sendPhoneNum SMS destination number * @paramdesc message content * @return Integer (. 1: success code, other failures, particularly see note) * / public static Integer Send (Uid String, String Key, sendPhoneNum String, String desc) { HttpClient Client = new new HttpClient (); a PostMethod POST = new new a PostMethod (SMS_Url); post.addRequestHeader ( "the Content-the Type", "file application / X-WWW-form-urlencoded; charset = GBK"); // set transcoding header file // set the parameters of NameValuePairs [] = Data { new new of NameValuePairs ( "Uid" , Uid), new new of NameValuePairs ( "Key", Key),//秘钥 new NameValuePair("smsMob", sendPhoneNum), new NameValuePair("smsText", desc) }; post.setRequestBody(data); try { client.executeMethod(post); } catch (Exception e) { e.printStackTrace(); } Header[] headers = post.getResponseHeaders(); int statusCode = post.getStatusCode(); System.out.println("statusCode:" + statusCode); for (Header h : headers) { System.out.println(h.toString()); } String result -6 IP restrictions= "" ; The try { Result = new new String (. Post.getResponseBodyAsString () the getBytes ( "GBK" )); } the catch (Exception E) {e.printStackTrace ();} post.releaseConnection (); return the Integer.parseInt ( Result); } / ** * this user account is not -1 -2 interface key is incorrect [View key] account login password than -21 MD5 interface key encryption incorrect -3 insufficient number of messages -11 user is disabled -14 message content appears illegal character -4 phone number format is incorrect -41 phone number is empty -42 message content is empty -51 SMS signature format is incorrect Interface signature format: [signature] content Send message number is greater than 0 More complement ;* / public static String the getMessage (Integer code) { String Message; IF (code> 0 ) { Message = "F the SMS-message has been sent successfully amount there!" + Code + "article" ; } the else IF (code -1 == ) { Message = "SMS- not the user account" ; } the else IF (code == -2 ) { Message = "SMS- interface key is incorrect" ; } the else IF (code == -21 ) { Message = "interface to the SMS-key encryption is incorrect the MD5" } the else IF (code == -3 ) { Message = "insufficient quantity SMS- message" ; } the else IF (code == -11 ) { Message = "SMS- the user is disabled" ; } the else IF (code == -14 ) { the message = "SMS-message content appears illegal character" ; } the else IF (code == -4 ) { the message = "SMS-phone number format is not correct" ; } the else IF (code == -41 ) { the message = "SMS- phone number is empty" ; } the else IF (code == -42 ) { Message = "SMS- message is empty" ; } the else IF (code == -51 ) { Message = "SMS- message incorrect signature format as an interface signature format: [Signature] Content " ; } the else IF (code == -6 ) { Message =" the IP-limits the SMS " ; } the else { Message =" other errors " ; } return Message; } / ** * 6 randomly generated codes * @return * / public static String getRandomCode(Integer code){ Random random = new Random(); StringBuffer result= new StringBuffer(); for (int i=0;i<code;i++){ result.append(random.nextInt(10)); } return result.toString(); } }
3. Controller call layer process
SendMessageUtil.send ( "network build-messaging account", "keys", "recipient mobile phone number", "Send Content");