Java example program for free sending text messages

Source: Internet
Author: User

Recently I found an interface for sending text messages. You only need to enter your mobile phone number and text message content, but the content is incomplete.

Interface Source: HiChina Alibaba Cloud recharge center,

Http://finance.aliyun.com/account/recharge.htm

If you select "offline recharge", you can enter your mobile phone number and send it for free.

This section of code needs to reference the commons-httpclient-3.1.jar commons-io.jar package, and the request part can also be used in other ways, as long as it is post access.

The preceding code is only for learning and cannot be used for other purposes.

The code is as follows: Copy code

Package com. google;

Import java. io. BufferedReader;
Import java. io. IOException;
Import java. io. InputStream;
Import java. io. InputStreamReader;

Import org. apache. commons. httpclient. HttpClient;
Import org. apache. commons. httpclient. methods. PostMethod;

Public class SendSMS {
Public static void main (String [] args ){
String url = "http://finance.aliyun.com/recharge/sendMessage.json ";
String token = "9ZuD6iN0aFDN2SzFjEMKuA ";
String mobile = "13800138000 ";
String sendStr = "Qi Ruifeng grass-roots Bank 8888 8888 8888 888 ";
SendSMS ss = new SendSMS ();
String result = ss. getPostPage (url, mobile, sendStr, token );
System. out. println (result );
 }

Public String getPostPage (String url, String mobile, String sendStr,
String _ csrf_token ){
HttpClient client = new HttpClient ();
PostMethod method = new PostMethod (url );
Method. getParams (). setContentCharset ("UTF-8 ");
Method. setRequestHeader ("Accept ",
"Text/html, application/xhtml + xml, application/xml; q = 0.9, */*; q = 0.8 ");
Method. setRequestHeader ("Accept-Encoding", "gzip, deflate, sdch ");
Method. setRequestHeader ("Accept-Language", "zh-CN, zh; q = 0.8 ");
Method. setRequestHeader ("Accept-Charset", "GBK, UTF-8; q = 0.7, *; q = 0.3 ");
Method. setRequestHeader ("Host", "finance.aliyun.com ");
Method. setRequestHeader ("Origin", "http://finance.aliyun.com ");
Method. setRequestHeader ("Connection", "Keep-Alive ");
Method. setRequestHeader (
"Referer ",
"Http://finance.aliyun.com/account/recharge.htm? Spm = 0.0.0.0.elRHXV _ 0.0.0.0.0.uwxmet ");
Method. setRequestHeader (
"User-Agent ",
"Mozilla/5.0 (Windows NT 5.1) AppleWebKit/535.7 (KHTML, like Gecko) Chrome/16.0.912.77 Safari/535.7 ");

Method. setRequestHeader (
"Cookie ",
"JSESSIONID = UM5663B1-YORLH533CMAAD3D7NVG72-IDQ1HNPH-HA8; www_aliyun = role; login_aliyunid =" admin@admin.com "; role = role * 0 * Cm58slMT1tJw3_l $ role; role = _ role; hssid = role; hsite = 6; hsts = 1388030291344; cfp0 = percent % 2FY % percent % 2B % 2FY % percent % 2 FUImAwXwx % percent % 2BXKBNrD4z5X % percent % 2Bg % 3D % 3D; _ ga = GA1.2.1860272764.1387960845 ");
Method. setRequestHeader ("X-Requested-With", "XMLHttpRequest ");
// Set request parameters

Method. addParameter ("mobile", mobile );
Method. addParameter ("sendStr", sendStr );
Method. addParameter ("_ csrf_token", _ csrf_token );

String responseStr = "";
Try {
Client.exe cuteMethod (method );
// ResponseStr = method. getResponseBodyAsString ();
InputStream resStream = method. getResponseBodyAsStream ();
BufferedReader br = new BufferedReader (new InputStreamReader (
ResStream ));
StringBuffer resBuffer = new StringBuffer ();
String resTemp = "";
While (resTemp = br. readLine ())! = Null ){
ResBuffer. append (resTemp );
   }
ResponseStr = resBuffer. toString ();
Method. releaseConnection ();

} Catch (IOException e ){
// TODO Auto-generated catch block
E. printStackTrace ();
  }

Return responseStr;

 }
}

Successful return value after request: {"messageId": 107913122600012591, "success": true, "tips": "200 successful "}

If no result is returned, the sending fails, possibly due to a session issue.

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.