/*** Created by Bingone on 15/12/16.*/ Importorg.apache.http.HttpEntity; ImportOrg.apache.http.NameValuePair; Importorg.apache.http.client.entity.UrlEncodedFormEntity; ImportOrg.apache.http.client.methods.CloseableHttpResponse; ImportOrg.apache.http.client.methods.HttpPost; Importorg.apache.http.impl.client.CloseableHttpClient; Importorg.apache.http.impl.client.HttpClients; ImportOrg.apache.http.message.BasicNameValuePair; Importorg.apache.http.util.EntityUtils; Importjava.io.IOException; Importjava.net.URISyntaxException; ImportJava.net.URLEncoder; Importjava.util.ArrayList; ImportJava.util.HashMap; Importjava.util.List; ImportJava.util.Map;/*** SMS HTTP Interface Java code invocation example * based on Apache HttpClient 4.3 * *@authorSongchao *@since2015-04-03*/ Public classJavasmsapi {//HTTP address to check account information Private StaticString uri_get_user_info = "Https://sms.yunpian.com/v1/user/get.json"; //the HTTP address of the Smart match template send interface Private StaticString uri_send_sms = "Https://sms.yunpian.com/v1/sms/send.json"; //HTTP address of the template send interface Private StaticString uri_tpl_send_sms = "Https://sms.yunpian.com/v1/sms/tpl_send.json"; //send the HTTP address of the Voice Verification Code interface Private StaticString uri_send_voice = "Https://voice.yunpian.com/v1/voice/send.json"; //encoding format. Send encoding format unified with UTF-8 Private StaticString ENCODING = "UTF-8"; Public Static voidMain (string[] args)throwsIOException, urisyntaxexception {//modified to your apikey.apikey available on the official website (http://www.yuanpian.comafter logging in, getString Apikey = "Xxxxxxxxxxxxxxxxxxxxx"; //change to the phone number you want to sendString mobile = "130xxxxxxxx"; /**************** Account Information call Example *****************/System.out.println (Javasmsapi.getuserinfo (Apikey)); /**************** SMS (recommended) using the Smart Match template interface *****************/ //set what you want to send (the content must match a template.) The following example matches the system-provided 1th template)String Text = "Cloud Mesh" Your verification code is 1234 "; //Send SMS Call Example//System.out.println (javasmsapi.sendsms (apikey, Text, mobile)); /**************** texting using the specified template interface (not recommended, smart Match template interface is recommended) *****************/ //set the template ID, such as using the number 1th Template: "#company #" Your captcha is #code# Longtpl_id = 1; //set the corresponding template variable valueString Tpl_value= Urlencoder.encode ("#code #", ENCODING) + "=" + Urlencoder.encode ("1234", ENCODING) + "&" + Urlenc Oder.encode ("#company #", ENCODING) + "=" + Urlencoder.encode ("Cloud mesh", ENCODING); //example of a call sent by a templateSystem.out.println (Tpl_value); System.out.println (Javasmsapi.tplsendsms (Apikey, tpl_id, Tpl_value, Mobile)); /**************** using the interface to send a voice verification code *****************/String Code= "1234"; //System.out.println (Javasmsapi.sendvoice (Apikey, Mobile, Code)); } /*** Access to account information * *@returnJSON format string *@throwsjava.io.IOException*/ Public StaticString GetUserInfo (String apikey)throwsIOException, urisyntaxexception {Map<string, string> params =NewHashmap<string, string>(); Params.put ("Apikey", Apikey); returnPost (uri_get_user_info, params); } /*** Smart Match Template Interface text message * *@paramApikey Apikey *@paramtext text message content *@paramMobile-Accepted phone number *@returnJSON format string *@throwsIOException*/ Public StaticString Sendsms (String apikey, string text, String mobile)throwsIOException {Map<string, string> params =NewHashmap<string, string>(); Params.put ("Apikey", Apikey); Params.put ("Text", text); Params.put ("Mobile", mobile); returnPost (uri_send_sms, params); } /*** Send SMS via template (not recommended) * *@paramApikey Apikey *@paramtpl_id Template ID *@paramtpl_value Template Variable Value *@paramMobile-Accepted phone number *@returnJSON format string *@throwsIOException*/ Public StaticString Tplsendsms (String Apikey,Longtpl_id, String Tpl_value, String mobile)throwsIOException {Map<string, string> params =NewHashmap<string, string>(); Params.put ("Apikey", Apikey); Params.put ("TPL_ID", String.valueof (tpl_id)); Params.put ("Tpl_value", Tpl_value); Params.put ("Mobile", mobile); returnPost (uri_tpl_send_sms, params); } /*** Send voice verification code via interface *@paramApikey Apikey *@parammobile phone number received by mobile *@paramCode Verification Code *@return */ Public Staticstring Sendvoice (String Apikey, String Mobile, String code) {Map<string, string> params =NewHashmap<string, string>(); Params.put ("Apikey", Apikey); Params.put ("Mobile", mobile); Params.put ("Code", code); returnPost (uri_send_voice, params); } /*** General Post method based on httpclient 4.3 * *@paramURL submitted by URLs *@paramParamsmap Submit < parameters, Value >map *@returnSubmit Response*/ Public StaticString post (string URL, map<string, string>Paramsmap) {closeablehttpclient Client=Httpclients.createdefault (); String ResponseText= ""; Closeablehttpresponse Response=NULL; Try{HttpPost Method=Newhttppost (URL); if(Paramsmap! =NULL) {List<NameValuePair> paramlist =NewArraylist<namevaluepair>(); for(Map.entry<string, string>Param:paramsMap.entrySet ()) {Namevaluepair pair=NewBasicnamevaluepair (Param.getkey (), Param.getvalue ()); Paramlist.add (pair); } method.setentity (Newurlencodedformentity (Paramlist, ENCODING)); } Response=Client.execute (method); Httpentity Entity=response.getentity (); if(Entity! =NULL) {ResponseText=entityutils.tostring (entity); } } Catch(Exception e) {e.printstacktrace (); } finally { Try{response.close (); } Catch(Exception e) {e.printstacktrace (); } } returnresponsetext; }}
SMS Interface API