200字范文,内容丰富有趣,生活中的好帮手!
200字范文 > 使用SMS网建短信通平台发送短信-Java示例

使用SMS网建短信通平台发送短信-Java示例

时间:2024-06-27 21:07:40

相关推荐

使用SMS网建短信通平台发送短信-Java示例

中国网建短信平台接口

/api.shtml

步骤:

1. 短信通平台用户注册:/reg.shtml

2.用户登录:用户登录之后,可以发送5条免费短信。

3.用户信息修改:需要修改密码和短信签名(短信开头会加上【短信签名内容】,一般为公司名或产品名的简称)

4.点击短信API接口

点击JAVA程序示例

下载演示程序

解压后,打开其中一个目录,如msgProGBK,目录结构如下

5.测试

5.1 用eclipse打开

用eclipse打开解压后的目录,注意需要给.java文件添加包,例如:

demo里代码已经写好,贴出来,方便查看

HttpClientUtil.java

package msgProGBK;import java.io.IOException;import .URL;import java.util.ArrayList;import java.util.HashMap;import java.util.List;import java.util.Map;import org.apache.http.HttpEntity;import org.apache.http.NameValuePair;import org.apache.http.client.config.RequestConfig;import org.apache.http.client.entity.UrlEncodedFormEntity;import org.apache.http.client.methods.CloseableHttpResponse;import org.apache.http.client.methods.HttpGet;import org.apache.http.client.methods.HttpPost;import org.apache.http.conn.ssl.DefaultHostnameVerifier;import org.apache.http.conn.util.PublicSuffixMatcher;import org.apache.http.conn.util.PublicSuffixMatcherLoader;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;public class HttpClientUtil { private RequestConfig requestConfig = RequestConfig.custom() .setSocketTimeout(15000) .setConnectTimeout(15000) .setConnectionRequestTimeout(15000) .build(); private static HttpClientUtil instance = null; private HttpClientUtil(){} public static HttpClientUtil getInstance(){ if (instance == null) { instance = new HttpClientUtil(); } return instance; } /** * 发送 post请求 * @param httpUrl 地址 */ public String sendHttpPost(String httpUrl) { HttpPost httpPost = new HttpPost(httpUrl);// 创建httpPost return sendHttpPost(httpPost,"utf-8"); } /** * 发送 post请求 * @param httpUrl 地址 * @param maps 参数 * @param type 字符编码格式 */ public String sendHttpPost(String httpUrl, Map<String, String> maps,String type) { HttpPost httpPost = new HttpPost(httpUrl);// 创建httpPost // 创建参数队列 List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(); for (String key : maps.keySet()) { nameValuePairs.add(new BasicNameValuePair(key, maps.get(key))); } try { httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs, type)); } catch (Exception e) { e.printStackTrace(); } return sendHttpPost(httpPost,type); } /** * 发送Post请求 * @param httpPost * @return */ private String sendHttpPost(HttpPost httpPost,String reponseType) { CloseableHttpClient httpClient = null; CloseableHttpResponse response = null; HttpEntity entity = null; String responseContent = null; try { // 创建默认的httpClient实例. httpClient = HttpClients.createDefault(); httpPost.setConfig(requestConfig); // 执行请求 response = httpClient.execute(httpPost); entity = response.getEntity(); responseContent = EntityUtils.toString(entity, reponseType); } catch (Exception e) { e.printStackTrace(); } finally { try { // 关闭连接,释放资源 if (response != null) { response.close(); } if (httpClient != null) { httpClient.close(); } } catch (IOException e) { e.printStackTrace(); } } return responseContent; } /** * 发送 get请求 * @param httpUrl */ public String sendHttpGet(String httpUrl) { HttpGet httpGet = new HttpGet(httpUrl);// 创建get请求 return sendHttpGet(httpGet); } /** * 发送 get请求Https * @param httpUrl */ public String sendHttpsGet(String httpUrl) { HttpGet httpGet = new HttpGet(httpUrl);// 创建get请求 return sendHttpsGet(httpGet); } /*** @Title: sendMsgUtf8* @Description: TODO(发送utf8)* @param: @param Uid* @param: @param Key* @param: @param content* @param: @param mobiles* @param: @return* @return: int* @throws*/@SuppressWarnings({ "rawtypes", "unchecked" })public int sendMsgUtf8(String Uid,String Key,String content,String mobiles){Map maps = new HashMap();maps.put("Uid", Uid);maps.put("Key", Key);maps.put("smsMob", mobiles);maps.put("smsText", content);String result = sendHttpPost("http://utf8.", maps, "utf-8");return Integer.parseInt(result);}/*** @Title: sendMsgUtf8* @Description: TODO(发送utf8)* @param: @param Uid* @param: @param Key* @param: @param content* @param: @param mobiles* @param: @return* @throws*/@SuppressWarnings({ "rawtypes", "unchecked" })public int sendMsgGbk(String Uid,String Key,String content,String mobiles){Map maps = new HashMap();maps.put("Uid", Uid);maps.put("Key", Key);maps.put("smsMob", mobiles);maps.put("smsText", content);String result = sendHttpPost("http://gbk.", maps, "gbk");return Integer.parseInt(result);}/** * 发送Get请求 * @param httpPost * @return */ private String sendHttpGet(HttpGet httpGet) { CloseableHttpClient httpClient = null; CloseableHttpResponse response = null; HttpEntity entity = null; String responseContent = null; try { // 创建默认的httpClient实例. httpClient = HttpClients.createDefault(); httpGet.setConfig(requestConfig); // 执行请求 response = httpClient.execute(httpGet); entity = response.getEntity(); responseContent = EntityUtils.toString(entity, "UTF-8"); } catch (Exception e) { e.printStackTrace(); } finally { try { // 关闭连接,释放资源 if (response != null) { response.close(); } if (httpClient != null) { httpClient.close(); } } catch (IOException e) { e.printStackTrace(); } } return responseContent; } /** * 发送Get请求Https * @param httpPost * @return */ private String sendHttpsGet(HttpGet httpGet) { CloseableHttpClient httpClient = null; CloseableHttpResponse response = null; HttpEntity entity = null; String responseContent = null; try { // 创建默认的httpClient实例. PublicSuffixMatcher publicSuffixMatcher = PublicSuffixMatcherLoader.load(new URL(httpGet.getURI().toString())); DefaultHostnameVerifier hostnameVerifier = new DefaultHostnameVerifier(publicSuffixMatcher); httpClient = HttpClients.custom().setSSLHostnameVerifier(hostnameVerifier).build(); httpGet.setConfig(requestConfig); // 执行请求 response = httpClient.execute(httpGet); entity = response.getEntity(); responseContent = EntityUtils.toString(entity, "UTF-8"); } catch (Exception e) { e.printStackTrace(); } finally { try { // 关闭连接,释放资源 if (response != null) { response.close(); } if (httpClient != null) { httpClient.close(); } } catch (IOException e) { e.printStackTrace(); } } return responseContent; } /*** @Title: getErrorMsg* @Description: TODO(返回异常原因)* @param: @param errorCode*/public String getErrorMsg(int errorCode){if(errorCode==-1){return "没有该用户账户";}else if(errorCode==-2){return "接口密钥不正确";}else if(errorCode==-3){return "短信数量不足";}else if(errorCode==-4){return "手机号格式不正确";}else if(errorCode==-21){return "MD5接口密钥加密不正确";}else if(errorCode==-11){return "该用户被禁用";}else if(errorCode==-14){return "短信内容出现非法字符";}else if(errorCode==-41){return "手机号码为空";}else if(errorCode==-42){return "短信内容为空";}else if(errorCode==-51){return "短信签名格式不正确";}else if(errorCode==-6){return "IP限制";}else{return "未知错误码:"+errorCode;}}}

test.java

package msgProGBK;import java.util.HashMap;import java.util.Map;public class test {//用户名private static String Uid = "填入网建网的登录用户名";//接口安全秘钥private static String Key = "填入密钥";//手机号码,多个号码如13800000000,13800000001,13800000002private static String smsMob = "填写要发送到的手机号";//短信内容private static String smsText = "验证码:测试一下8888";public static void main(String[] args) {HttpClientUtil client = HttpClientUtil.getInstance();//GBK发送int resultGbk = client.sendMsgGbk(Uid, Key, smsText, smsMob );if(resultGbk>0){System.out.println("GBK成功发送条数=="+resultGbk);}else{System.out.println(client.getErrorMsg(resultGbk));}}}

注意修改如下信息:

Uid = "填入网建网的登录用户名";

Key = "填入密钥"; 参考:密钥的获取方式

smsMob = "填写要发送到的手机号";

smsText = "验证码:测试一下8888";

密钥的获取方式:点击修改短息密钥,复制短信密钥,填入Key中

运行test.java, 手机接收到的短信如下:

5.2 用IDEA maven方式

用maven方式更容易追踪源码,方便理解代码。

新建maven工程,选maven-archetype-quickstart

修改pom.xml,添加依赖

<dependency><groupId>org.apache.httpcomponents</groupId><artifactId>httpclient</artifactId><version>4.5.10</version></dependency><dependency><groupId>commons-logging</groupId><artifactId>commons-logging</artifactId><version>1.2</version></dependency><dependency><groupId>org.apache.httpcomponents</groupId><artifactId>httpcore</artifactId><version>4.4.12</version></dependency>

新建两个类

HttpClientUtil.java和test.java,代码和上面一样。

运行测试。

完成!enjoy it!

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。