Using the Chinese network to implement Java to send SMS to the mobile phone function

Source: Internet
Author: User

The company needs to use to send a verification code to the designated mobile phone function, search on the Internet, there is the use of ' SMS cat ', there is use of webservice, there is use of third-party, I use the Chinese network built to provide the API to achieve;
China's network to build a registration to send five SMS and three MMS to do the test, which is enough, today the company just bought thousands of to do the test!
The first is the choice of encoding:

GBK编码发送接口地址:http://gbk.sms.webchinese.cn/?Uid=本站用户名&Key=接口安全秘钥&smsMob=手机号码&smsText=验证码:8888 UTF-8编码发送接口地址:http://utf8.sms.webchinese.cn/?Uid=本站用户名&Key=接口安全秘钥&smsMob=手机号码&smsText=验证码:8888

The parameters are detailed in the following table

I use the UTF-8, the official website provides a demo example, with the httpclient is 3.x, but I want to use 4.3.3, with the company Project match,

public string Sendsecuritycode (string tel, string code) throws Clientprotocolexception, IOException {closeablehttp Client client = Httpclientbuilder. Create(). Build(); HttpPost post = new HttpPost ("http://utf8.sms.webchinese.cn"); Post. AddHeader("Content-type","Application/x-www-form-urlencoded;charset=utf-8");//Set transcoding in header filelist<namevaluepair> formparams = new Arraylist<namevaluepair> ();Formparams. Add(New Basicnamevaluepair ("Uid","Company Name"));Formparams. Add(New Basicnamevaluepair ("Key","SMS Key"));Formparams. Add(New Basicnamevaluepair ("Smsmob", tel));Formparams. Add(New Basicnamevaluepair ("Smstext","Welcome to be an app user, Captcha:"+ code));try {Post. Setentity(New Urlencodedformentity (Formparams,"Utf-8"));//Set parameter encoding format} catch (Exception E1) {E1. Printstacktrace();} Closeablehttpresponse response = Client. Execute(POST);int StatusCode = response. Getstatusline(). Getstatuscode();System. out. println("StatusCode:"+statuscode);header[] Headers = response. Getallheaders();for (Header h:headers) {System. out. println(h. toString());The String result = Entityutils. toString(Response. GetEntity());System. out. println("----"+result);//print return message statusPost. Releaseconnection();Client. Close();return result;}

Must pay attention to coding, because if the code is not uniform, resulting in garbled characters, will be intercepted, resulting in text messages can not be sent out, the morning on the card in this piece.
Notice the result in the code, which returns the resulting code, as in the table below

There are a few points to note, one is the SMS key, one is the SMS signature!
Respectively in the "Modify the text message key" and "User Information modification", these two are required, otherwise the text will not be sent out, or intercepted!
Here, run the code and wait for the phone's SMS!
(Specific to the Chinese network to build the official website for viewing and consulting customer service ...) Not for advertising .... )

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Using the Chinese network to implement Java to send SMS to the mobile phone function

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.