The program calls the Apsara API to send free text messages (Java example, used in the same way as other languages)

Source: Internet
Author: User

On the internet to see the web version of the fetion, http://fetionlib.appspot.com/can add friends, send messages to the group and timed send SMS to fetion friends, but also open the API interface for the program to call, it can be used to monitor whether the machine is working normally and regularly send text messages to administrators, or small-scale websites send text messages to members.

Important: Recently, https of all appspot has been reset. Please change the HTTPS in the call address to HTTP

A simple test: http://fetionlib.appspot.com/restlet/fetion/13812345678/Password/13912345678/Message

Among them, 13812345678 and 13912345678 are the sender and receiver's mobile phone numbers. Note that these two numbers must be mutual friends. We need to replace them with our own mobile phone numbers and send them to ourselves, change password to your password, enter the link in the browser, and wait a moment. If it succeeds, it will display OK. At this time, your mobile phone should be able to receive your own message.

Well, the above is the simplest test. I will not talk much about it. Here is an example of a Java call to implement a series of actions such as sending text messages, sending and canceling scheduled text messages, and adding friends. Other languages should call HTTP connection using get or post. The following example is text messaging, other examples such as getting friends list, sending and canceling scheduled text messages, adding friends please view the http://fetionlib.appspot.com/api.html directly

Package com. test;

Import java. Io. bufferedreader;
Import java. Io. dataoutputstream;
Import java. Io. inputstreamreader;
Import java.net. httpurlconnection;
Import java.net. url;
Import java.net. urlencoder;
Import java. util. UUID;

Import org. Apache. commons. Logging. log;
Import org. Apache. commons. Logging. logfactory;

Import org. JSON. jsonarray;
Import org. JSON. jsonobject;

/**
* @ Author Terry Email: yaoxinghuo at 126 dot com
* @ Version create: Aug 5, 2009 11:17:23 AM
* @ Version update: Oct 15,200 9 00:11:00 AM
* @ Description fetionlib provides an example of simple HTTP call and a restlet method. Home: http://fetionlib.appspot.com/
* Tips: free activation of Apsara Stack: If your mobile phone number does not activate Apsara stack, you can go to China Mobile's Apsara stack website to view the activation status, or directly edit the SMS ktfx and send it to 10086 for activation.
* Change the flying Mail Password: the new password (6 to 16 characters, not Chinese characters or full-width characters) is sent to 12520050
* Enter the following address in your browser (change your mobile phone number and password. do not include/in the password, or enter your mobile phone number in your friend's number ):
* Http://fetionlib.appspot.com/restlet/fetion/13812345678/Password/13912345678/Message
* Replace the four bold areas separated by "/" with your mobile phone number, password, and other mobile phone number (you can write your own mobile phone number and send it to yourself), text message content (no more than 180 words ),
* If the password is incorrect, the system returns the message not sent if the recipient is not your friend or not, and returns OK only if the recipient is successful.
* If you want to send Chinese characters, it is best to use urlencode (UTF-8 encoding, such as "hello" after encode is % E4 % BD % A0 % E5 % a5 % BD, now supported) or the following example (POST method, note that the called URL is slightly different)
* If you can receive a text message sent by yourself, congratulations, the test is successful. You can call the message through post or get in a language you are familiar with. For the call format, see the following Java example, similar to other languages
* If you have any questions or have any better suggestions on the API call method, Please provide your valuable suggestions.
*
*You can obtain a list of friends, send a group (with eight or fewer friends) in the post mode, and send a group at a scheduled time (with a maximum of 30 friends in the group). See the updated example.
*
*Updates recently found that some people use this program to send bombing text messages to others, causing serious harassment to others, and also consume a lot of resources on this site. The following restrictions have been imposed:
* The same mobile phone number sends a text message API to the same friend and other APIs (such as adding a friend or getting a friend list) with a request interval of 30 seconds, similar requests within 30 seconds cannot be completed.
* Note: For actual needs, there will be no 30-second interval for API requests that send text messages (the mobile phone number is the same as the other friend number or the Group of Friends contains the mobile phone number) to themselves!
*
*This API program is strongly driven by Google and hosted for free. It will be retained for a long time. For the JSON package used by the sample program, please download the jar package at www.json.org or here.
*/
Public class test {
Private Static log = logfactory. getlog (test. Class );

Public static void main (string [] ARGs ){
// Test text message
Boolean B = fetchtosendsms ("13812345678", "12345678", new string [] {"13812345678 "},
"Testmessage ");
System. Out. println ("Send message Result:" + B );

// Test to obtain a friend list
// Jsonarray friends = fetchtogetfriends ("13812345678", "12345678 ");
// System. Out. println ("Friends:/R/N" + (Friends = NULL? "Null": Friends. tostring ()));

// Test adding a friend
// Int result = fetchtoaddfriend ("13812345678", "12345678", "13812345678", "testmyname", "testfriendname ");
// System. Out. println ("add friend Result:" + result );

// Test the timed message sending(Note that it is the Pacific time, so is the departure time at Beijing Time)
// String SID = fetchtosendschedulemsg ("13812345678", "12345678", new string [] {"13912345678"}, "testschedulemessage", "2009-10-09 ");
// System. Out. println ("Sid:" + Sid );

// Test the deletion of scheduled SMS messages
// Boolean b2 = fetchtodeleteschedulemsg ("13812345678", "12345678", "123456 ");
// System. Out. println ("schedule message Delete Result:" + b2 );
}

Private Static final int try_times = 3;
Private Static final int time_out = 30000;

/**
* A simpler get Method for Sending Short Messages (Group Sending is not supported. For example, if you want to send short messages in the following post mode and have been updated), enter the following address in your browser, change the mobile phone number and password:
* If the http://fetionlib.appspot.com/restlet/fetion/13812345678/password/13912345678/message is successful, OK is returned.
* Otherwise, the message not sent is returned. If you want to send a group message or your password contains/or you need to submit a Chinese message to avoid possible garbled characters, use the following Program (POST method)
*Note that the array in the string [] Friends parameter can be the mobile phone number of a friend or the URI of a friend obtained by the program,For details, refer to the descriptions for getting a friend list.
*Such as fetchtosendsms ("13812345678", "password", new string [] {"SIP: 12345678@fetion.com.cn; P = 5065", "13916416465", "Tel: 15912345678 "}, "test ");
* The number of friends cannot exceed 8. If necessary, use the program to separate multiple calls.

*Note: The call interval for requests with the same phone number and friends is more than 30 seconds. Otherwise, the call fails (responsecode: 406 ), however, you are not limited to 30 seconds to accept requests that contain your own mobile phone number!
*/
Public static Boolean fetchtosendsms (string mobile, string password,
String [] Friends, string message ){
// The UUID is added to prevent messages from being sent successfully on the server, but an error is returned,
// As a result, the client will continue to try the request. In this case, the server will tell you that the request has been sent based on the uuid to avoid sending text messages again.
String UUID = UUID. randomuuid (). tostring ();
For (INT I = 0; I <try_times; I ++ ){
Int responsecode = 0;
Try {
URL posturl = new URL (
Http://fetionlib.appspot.com/restlet/fetion ");
Httpurlconnection connection = (httpurlconnection) posturl
. Openconnection ();
Connection. setconnecttimeout (time_out );
Connection. setreadtimeout (time_out );
Connection. setdooutput (true );
Connection. setrequestmethod ("Post ");
Connection. setusecaches (false );
Connection. setinstancefollowredirects (true );
Connection. setrequestproperty ("Content-Type ",
"Application/X-WWW-form-urlencoded ");
Connection. Connect ();
Dataoutputstream out = new dataoutputstream (connection
. Getoutputstream ());
String content = "Mobile =" + mobile + "& UUID =" + UUID
+ "& Password =" + password + "& friend =" + convertarraytojsonstring (Friends)
+ "& Message =" + urlencoder. encode (message, "UTF-8 ");
Out. writebytes (content );

Out. Flush ();
Out. Close ();

Responsecode = connection. getresponsecode ();
Connection. Disconnect ();
If (responsecode = 202)
Return true;
Else
Return false;
} Catch (exception e ){
Log. Warn ("error fetchtosendsms, exception:" + E. getmessage ()
+ ". Tried" + I + "times ");
}
}
Return false;
}

 

// Convert the array to jsonstring
Private Static string convertarraytojsonstring (string [] ARR) throws exception {
Jsonarray ja = new jsonarray ();
For (string a: ARR)
Ja. Put (a); // Ja. Add ();//?
Return urlencoder. encode (JA. tostring (), "UTF-8 ");
}

}

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.