这两天一直在看webservice,因为要实现一个手机APP发送请求,获取验证码的功能,需要调用短信平台服务,短信服务平台有好多家,每家的API接口又都不一样,今天测试了两家,一家是易信通的,还有一家是云片的。我觉得易信通挺好用,但是价格贵了,在这里介绍下云片。
云片官方API
/**
* Created by bingone on 15/12/16.
*/
import org.apache.http.HttpEntity;
import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import java.io.IOException;
import java.net.URISyntaxException;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* 短信http接口的java代码调用示例
* 基于Apache HttpClient 4.3
*
* @author songchao
* @since 2015-04-03
*/
public class JavaSmsApi {
//查账户信息的http地址
private static String URI_GET_USER_INFO = "https://sms.yunpian.com/v2/user/get.json";
//智能匹配模板发送接口的http地址
private static String URI_SEND_SMS = "https://sms.yunpian.com/v2/sms/single_send.json";
//模板发送接口的http地址
private static String URI_TPL_SEND_SMS = "https://sms.yunpian.com/v2/sms/tpl_single_send.json";
//发送语音验证码接口的http地址
private static String URI_SEND_VOICE = "https://voice.yunpian.com/v2/voice/send.json";
//绑定主叫、被叫关系的接口http地址
private static String URI_SEND_BIND = "https://call.yunpian.com/v2/call/bind.json";
//解绑主叫、被叫关系的接口http地址
private static String URI_SEND_UNBIND = "https://call.yunpian.com/v2/call/unbind.json";
//编码格式。发送编码格式统一用UTF-8
private static String ENCODING = "UTF-8";
public static void main(String[] args) throws IOException, URISyntaxException {
//修改为您的apikey.apikey可在官网(http://www.yunpian.com)登录后获取
String apikey = "xxxxxxxxxxxxxxxxxxxxx";
//修改为您要发送的手机号
String mobile = "130xxxxxxxx";
/**************** 查账户信息调用示例 *****************/
System.out.println(JavaSmsApi.getUserInfo(apikey));
/**************** 使用智能匹配模板接口发短信(推荐) *****************/
//设置您要发送的内容(内容必须和某个模板匹配。以下例子匹配的是系统提供的1号模板)
String text = "【云片网】您的验证码是1234";
//发短信调用示例
// System.out.println(JavaSmsApi.sendSms(apikey, text, mobile));
/**************** 使用指定模板接口发短信(不推荐,建议使用智能匹配模板接口) *****************/
//设置模板ID,如使用1号模板:【#company#】您的验证码是#code#
long tpl_id = 1;
//设置对应的模板变量值
String tpl_value = URLEncoder.encode("#code#",ENCODING) +"="
+ URLEncoder.encode("1234", ENCODING) + "&"
+ URLEncoder.encode("#company#",ENCODING) + "="
+ URLEncoder.encode("云片网",ENCODING);
//模板发送的调用示例
System.out.println(tpl_value);
System.out.println(JavaSmsApi.tplSendSms(apikey, tpl_id, tpl_value, mobile));
/**************** 使用接口发语音验证码 *****************/
String code = "1234";
//System.out.println(JavaSmsApi.sendVoice(apikey, mobile ,code));
/**************** 使用接口绑定主被叫号码 *****************/
String from = "+86130xxxxxxxx";
String to = "+86131xxxxxxxx";
Integer duration = 30*60;// 绑定30分钟
// System.out.println(JavaSmsApi.bindCall(apikey, from ,to , duration));
/**************** 使用接口解绑主被叫号码 *****************/
// System.out.println(JavaSmsApi.unbindCall(apikey, from, to));
}
/**
* 取账户信息
*
* @return json格式字符串
* @throws java.io.IOException
*/
public static String getUserInfo(String apikey) throws IOException, URISyntaxException {
Map<String, String> params = new HashMap<String, String>();
params.put("apikey", apikey);
return post(URI_GET_USER_INFO, params);
}
/**
* 智能匹配模板接口发短信
*
* @param apikey apikey
* @param text 短信内容
* @param mobile 接受的手机号
* @return json格式字符串
* @throws IOException
*/
public static String sendSms(String apikey, String text, String mobile) throws IOException {
Map<String, String> params = new HashMap<String, String>();
params.put("apikey", apikey);
params.put("text", text);
params.put("mobile", mobile);
return post(URI_SEND_SMS, params);
}
/**
* 通过模板发送短信(不推荐)
*
* @param apikey apikey
* @param tpl_id 模板id
* @param tpl_value 模板变量值
* @param mobile 接受的手机号
* @return json格式字符串
* @throws IOException
*/
public static String tplSendSms(String apikey, long tpl_id, String tpl_value, String mobile) throws IOException {
Map<String, String> params = new HashMap<String, String>();
params.put("apikey", apikey);
params.put("tpl_id", String.valueOf(tpl_id));
params.put("tpl_value", tpl_value);
params.put("mobile", mobile);
return post(URI_TPL_SEND_SMS, params);
}
/**
* 通过接口发送语音验证码
* @param apikey apikey
* @param mobile 接收的手机号
* @param code 验证码
* @return
*/
public static String sendVoice(String apikey, String mobile, String code) {
Map<String, String> params = new HashMap<String, String>();
params.put("apikey", apikey);
params.put("mobile", mobile);
params.put("code", code);
return post(URI_SEND_VOICE, params);
}
/**
* 通过接口绑定主被叫号码
* @param apikey apikey
* @param from 主叫
* @param to 被叫
* @param duration 有效时长,单位:秒
* @return
*/
public static String bindCall(String apikey, String from, String to , Integer duration ) {
Map<String, String> params = new HashMap<String, String>();
params.put("apikey", apikey);
params.put("from", from);
params.put("to", to);
params.put("duration", String.valueOf(duration));
return post(URI_SEND_BIND, params);
}
/**
* 通过接口解绑绑定主被叫号码
* @param apikey apikey
* @param from 主叫
* @param to 被叫
* @return
*/
public static String unbindCall(String apikey, String from, String to) {
Map<String, String> params = new HashMap<String, String>();
params.put("apikey", apikey);
params.put("from", from);
params.put("to", to);
return post(URI_SEND_UNBIND, params);
}
/**
* 基于HttpClient 4.3的通用POST方法
*
* @param url 提交的URL
* @param paramsMap 提交<参数,值>Map
* @return 提交响应
*/
public static String post(String url, Map<String, String> paramsMap) {
CloseableHttpClient client = HttpClients.createDefault();
String responseText = "";
CloseableHttpResponse response = null;
try {
HttpPost method = new HttpPost(url);
if (paramsMap != null) {
List<NameValuePair> paramList = new ArrayList<NameValuePair>();
for (Map.Entry<String, String> param : paramsMap.entrySet()) {
NameValuePair pair = new BasicNameValuePair(param.getKey(), param.getValue());
paramList.add(pair);
}
method.setEntity(new UrlEncodedFormEntity(paramList, ENCODING));
}
response = client.execute(method);
HttpEntity entity = response.getEntity();
if (entity != null) {
responseText = EntityUtils.toString(entity, ENCODING);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
response.close();
} catch (Exception e) {
e.printStackTrace();
}
}
return responseText;
}
}
上面是云片官方给的代码实例,一看那么长的一大溜,吐血的心都有了。不过仔细看下,用到的并不多,云片只是提供了丰富的功能而已,下面是精简版。
这里写代码片package com.sms.demo;
/**
* Created by bingone on 15/12/16.
*/
import org.apache.http.HttpEntity;
import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import java.io.IOException;
import java.net.URISyntaxException;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* 短信http接口的java代码调用示例
* 基于Apache HttpClient 4.3
*
* @author songchao
* @since 2015-04-03
*/
public class JavaSmsApi {
//智能匹配模版发送接口的http地址
private static String URI_SEND_SMS = "https://sms.yunpian.com/v2/sms/single_send.json";
//编码格式。发送编码格式统一用UTF-8
private static String ENCODING = "UTF-8";
public static void main(String[] args) throws IOException, URISyntaxException {
//修改为您的apikey.apikey可在官网(http://www.yuanpian.com)登录后获取
String apikey = "45b98****************af702";
//修改为您要发送的手机号
String mobile = URLEncoder.encode("181****0130",ENCODING);
String text = "【云片网】您的验证码是1234";
//模板发送的调用示例
System.out.println(JavaSmsApi.sendSms(apikey, text, mobile));
}
/**
* 智能匹配模版接口发短信
*
* @param apikey apikey
* @param text 短信内容
* @param mobile 接受的手机号
* @return json格式字符串
* @throws IOException
*/
public static String sendSms(String apikey, String text, String mobile) throws IOException {
Map<String, String> params = new HashMap<String, String>();
params.put("apikey", apikey);
params.put("text", text);
params.put("mobile", mobile);
return post(URI_SEND_SMS, params);
}
/**
* 基于HttpClient 4.3的通用POST方法
*
* @param url 提交的URL
* @param paramsMap 提交<参数,值>Map
* @return 提交响应
*/
public static String post(String url, Map<String, String> paramsMap) {
CloseableHttpClient client = HttpClients.createDefault();
String responseText = "";
CloseableHttpResponse response = null;
try {
HttpPost method = new HttpPost(url);
if (paramsMap != null) {
List<NameValuePair> paramList = new ArrayList<NameValuePair>();
for (Map.Entry<String, String> param : paramsMap.entrySet()) {
NameValuePair pair = new BasicNameValuePair(param.getKey(), param.getValue());
paramList.add(pair);
}
method.setEntity(new UrlEncodedFormEntity(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();
}
}
return responseText;
}
}
这样看是不是爽了很多,当然,上面发送的验证码是固定不变的,想要发送动态验证码,还要写一个方法,直接调用即可,代码如下
public static String createRandomVcode(){
//验证码
String vcode = "";
for (int i = 0; i < 6; i++) {
vcode = vcode + (int)(Math.random() * 9);
}
return vcode;
}
要注意的是
1.获取apikey
//修改为您的apikey.apikey可在官网(http://www.yuanpian.com)登录后获取
String apikey = "45b98****************af702";
//修改为您要发送的手机号
String mobile = URLEncoder.encode("181****0130",ENCODING);
apikey是要注册云片获取的,电话号写个能做测试的电话号码即可
2.添加jar包
云片的官网就有提供
下载地址
总结
对于webservice,怎么说呢,我也一头雾水,不太理解。
Web Service, 能使得运行在不同机器上的不同应用无须借助附加的、专门的第三方软件或硬件, 就可相互交换数据或集成。依据Web Service规范实施的应用之间, 无论它们所使用的语言、 平台或内部协议是什么, 都可以相互交换数据
通俗的讲,Web Service就是一个部署在Web服务器上的一个,它向外界暴露出一个能够通过Web进行调用的API。这就是说,你能够用编程的方法通过Web来调用这个应用程序。我们把调用这个Web Service 的应用程序叫做客户端,发布这个web服务的机应用程序器称为Web Service服务器