- Preface
Short message transmission is now an indispensable part of the project. How can we push information to other people's mobile phones through web pages? Simple, simple coding! After reading this article, we only need to know what to send in order to realize the sending of short messages in the future. To whom? OK, the code is as follows, is it simple?^^
String result= "";//Return status
Note note = new Note();
String Tel = "17089490559";//Receiver's mobile phone number
String message = "Hello!";//Short Message Content
result = note.sendNote(Tel,message);//Information transmission status
It is a headache for B/S structure project to send information to the other party by inputting some information on the page or console and specifying a telephone number. But can we send the information and the telephone number to a server responsible for sending short messages to help us complete the information? What about the delivery? The answer is yes, only the question of money... (After all, there is no free lunch in the world). Understand this, then we can do it. We just need to know where the server is and how to contact the server. Fortunately, Apache has provided us with a subclass of HttpClient to help us connect to the server that sends short messages. Where is the server that sends short messages? There are all kinds of short message service platforms on the internet. Next, let's take the following steps China Network Construction For example, to explain the sending of short message information.
First of all, you need to register an account. After registration, the system will provide you with a user name and a key (found in the modified SMS key) and provide 5 free SMS messages. That's enough for the test. What we need is id and key. Let's start!
1. Import related jar packages
jar package download: http://pan.baidu.com/s/1hrJLBwg Password: 05fo
2. Making Short Message Tools
What we hope is once and for all, it's very hard to change all kinds of parameters when we do a project. Next, let's use the singleton mode to provide information modification for our project. With information change, we just need to change the configuration file, and the code part doesn't need to change at all. In this way, we use a note.properties to put the configuration file, a class of ConfigNoteInfo.java calling configuration file parameters, and a class of Note.java sending short messages to achieve the production of gadgets. Next, we will teach you how to configure the three files in turn.
In the first step, we create a new file called note.properties in the project, which has two parameters (in the src root directory).
id=xxx (xxx is your registered user name) Key=******** (SMS key provided by platform after successful registration)
Second, ConfigNoteInfo.java gets the id and key in the configuration file
public class ConfigNoteInfo {
private static ConfigNoteInfo configNoteInfo;
private static Properties properties;
private ConfigNoteInfo(){
//note.properties is your new configuration file in the root directory
String configFile="note.properties";
properties=new Properties();
InputStream in=ConfigNoteInfo.class.getClassLoader().getResourceAsStream(configFile);
try {
properties.load(in);
in.close();
} catch (IOException e) {
e.printStackTrace();
}
}
public static ConfigNoteInfo getInstance(){
if(configNoteInfo==null){
configNoteInfo = new ConfigNoteInfo();
}
return configNoteInfo;
}
public String getString(String key){
return properties.getProperty(key);
}
}
Third, Note.java class implements the sending of short message information. Based on the idea of OOP, we put forward the sending of short message separately as a method, so that only one mobile phone number and a message string can be sent in the future work. (If it's an array or a List collection of multiple cell phone numbers that can be transmitted)
package cn.hs.tools;
import java.io.IOException;
import org.apache.commons.httpclient.*;
import org.apache.commons.httpclient.methods.PostMethod;
/***
* @author HeShuang
*@Create_date:2017-5-4
*/
public class Note {
//Short message interface, referencing (mobile phone number, short message content) can send short messages.
public String sendNode(String Tel,String message) throws HttpException, IOException{
HttpClient client = new HttpClient();
PostMethod post = new PostMethod("http://utf8.sms.webchinese.cn");
post.addRequestHeader("Content-Type",
"application/x-www-form-urlencoded;charset=UTF-8");// Set up transcoding in the header file, here UTF-8
NameValuePair[] data = {
new NameValuePair("Uid", ConfigNoteInfo.getInstance().getString("id")), // Registered username, we read the username from the configuration file id through the singleton mode.
new NameValuePair("Key",ConfigNoteInfo.getInstance().getString("key")), // After successful registration, the key obtained after login to the website is also read from the configuration file.
new NameValuePair("smsMob", Tel), // Pass in the phone number
new NameValuePair("smsText", message) // Text Message Content
};
post.setRequestBody(data);
client.executeMethod(post);
Header[] headers = post.getResponseHeaders();
int flag = post.getStatusCode(); //Return status, refer to API
System.out.println("statusCode:" + flag);
for (Header h : headers) {
System.out.println("---" + h.toString());
}
String result = new String(post.getResponseBodyAsString().getBytes(
"UTF-8"));
System.out.println(result);
String info="";
if(Integer.parseInt(result.trim())>0){
info="Successful delivery!";
}
switch (result.trim()) {
case "-1":
info="User account does not exist!";
break;
case "-2":
info="The interface key is incorrect";
break;
case "-3":
info="Respected users, selling blood and kidneys, please pay the cost of SMS first!";
break;
case "-4":
info="The format of mobile phone number is incorrect!";
break;
case "-6":
info="IP There are limits!";
break;
case "-11":
info="This user has been disabled!";
break;
case "-14":
info="There are illegal characters in the text message content!";
break;
default:
break;
}
return info;
}
}
The fourth step is to call our tool class elsewhere to send short messages.
String result= "";//Return status
Note note = new Note();
String Tel = "17089490559";//Mobile number to send
String message = "Hello!";//Short Message to Send
result = note.sendNote();//Get the status of information transmission
In this regard, java to achieve the sending function of short message information is completed, and we also made it into a small tool class, information changes, just need to change the configuration file, write once, copy everywhere...