代码拉取完成,页面将自动刷新
该repo作为http://smscode.cn短信接口的插件包;
使用httpclient
发起post请求;
<dependency>
<groupId>cn.smscode</groupId>
<artifactId>smscode-java</artifactId>
<version>1.0.0</version>
</dependency>
使用http://maven.oschina.net的仓库;
<repository>
<id>oschina</id>
<name>oschina</name>
<url>http://maven.oschina.net/content/groups/public/</url>
</repository>
SmsCaptchaService.getInstance("11", "test", "test").text("验证码:0000.[smscode]").to(“13800000000”).send();
[SmsCaptchaService][SmsCaptchaService]
: 提供短信服务;
SmsCaptchaService getInstance(String userid, String account, String password)
: 生成实例;
SmsCaptchaService text(String content)
: 内容赋值;
SmsCaptchaService text(SmsMessage smsMessage)
,参考SmsMessage: 通过SmsMessage
用来封装模版短信和验证码生成待发送内容,如模版短信:注册验证码:0000。[smscode],所需喜欢验证码内容:"0000",指定验证码字符串,或默认规则生成的4位数字验证码,参考Captcha;
验证码可自定义规则和使用默认规则进行生成;
SmsCaptchaService to(String cellPhoneNumber)
: 手机号码;
boolean send()
: 调用PhoneTextHelper进行推送;
[PhoneTextHelper][PhoneTextHelper]
/**
* 使用httpclient发起post请求
*
* @param mobile 目的号码,必须为手机号
* @param content 短信内容信息
* @return xml解析后的的hashMap
*/
public Map<String, String> phoneText(String mobile, String content) {
// 验证参数是否正确, 错误则返回为空值
if (!checkRequestParameter() || !StringUtils.isNotEmpty(mobile) || !StringUtils.isNotEmpty(content)) {
return null;
}
// 封装post请求参数
List<NameValuePair> postParameters = new ArrayList<NameValuePair>();
postParameters.add(new BasicNameValuePair("action", send_action));
postParameters.add(new BasicNameValuePair("userid", userid));
postParameters.add(new BasicNameValuePair("account", account));
postParameters.add(new BasicNameValuePair("password", password));
postParameters.add(new BasicNameValuePair("mobile", mobile));
postParameters.add(new BasicNameValuePair("content", content));
postParameters.add(new BasicNameValuePair("sendTime", ""));
postParameters.add(new BasicNameValuePair("checkcontent", checkcontent));
CloseableHttpClient httpClient = HttpClients.createDefault();
CloseableHttpResponse closeableHttpResponse = null;
HttpPost httpPost = new HttpPost(send_url);
UrlEncodedFormEntity urlEncodedFormEntity;
Map<String, String> responseMap = null;
// 发送post请求
try {
// 根据文档提示,使用utf-8格式编码
urlEncodedFormEntity = new UrlEncodedFormEntity(postParameters, "utf-8");
httpPost.setEntity(urlEncodedFormEntity);
closeableHttpResponse = httpClient.execute(httpPost);
if (closeableHttpResponse.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
HttpEntity httpEntity = closeableHttpResponse.getEntity();
if (httpEntity != null) {
// 将响应返回的xml解析为HashMap
responseMap = parseXML(httpEntity.getContent());
}
}
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (DocumentException e) {
e.printStackTrace();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
closeableHttpResponse.close();
} catch (IOException e) {
e.printStackTrace();
}
try {
httpClient.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return responseMap;
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。