java实现发送短信
Posted weakup
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了java实现发送短信相关的知识,希望对你有一定的参考价值。
java实现发送短信(本章调用阿里云API)
地址如下
https://market.aliyun.com/products/56928004/cmapi022659.html?spm=5176.2020520132.101.5.TsClbR#sku=yuncode16659000011
1 import java.util.HashMap; 2 import java.util.Map; 3 4 import org.apache.http.HttpResponse; 5 import org.springframework.beans.factory.annotation.Autowired; 6 import org.springframework.beans.factory.annotation.Qualifier; 7 import org.springframework.stereotype.Controller; 8 import org.springframework.web.bind.annotation.RequestMapping; 9 10 import com.bjpowernode.beans.Student; 11 import com.bjpowernode.service.IStudentService; 12 13 @Controller 14 @RequestMapping("/test") 15 public class TestController { 16 17 @RequestMapping("/t.do" ) 18 public void t() { 19 String host = "http://yzx.market.alicloudapi.com"; 20 String path = "/yzx/sendSms"; 21 String method = "POST"; 22 String appcode = "这里填写你的appcode"; 23 Map<String, String> headers = new HashMap<String, String>(); 24 //最后在header中的格式(中间是英文空格)为Authorization:APPCODE 83359fd73fe94948385f570e3c139105 25 headers.put("Authorization", "APPCODE " + appcode); 26 Map<String, String> querys = new HashMap<String, String>(); 27 querys.put("mobile", "这里填写你要发送的手机号码"); 28 //querys.put("param", "code:1234"); 29 //querys.put("param", "这里填写你和商家定义的变量名称和变量值填写格式看上一行代码"); 30 querys.put("tpl_id", "这里填写你和商家商议的模板"); 31 Map<String, String> bodys = new HashMap<String, String>(); 32 33 34 try { 35 /** 36 * 重要提示如下: 37 * HttpUtils请从 38 * https://github.com/aliyun/api-gateway-demo-sign-java/blob/master/src/main/java/com/aliyun/api/gateway/demo/util/HttpUtils.java 39 * 下载 40 * 41 * 相应的依赖请参照 42 * https://github.com/aliyun/api-gateway-demo-sign-java/blob/master/pom.xml 43 */ 44 HttpResponse response = HttpUtil.doPost(host, path, method, headers, querys, bodys); 45 System.out.println(response.toString()); 46 47 //获取response的body 48 //System.out.println(EntityUtils.toString(response.getEntity())); 49 } catch (Exception e) { 50 e.printStackTrace(); 51 } 52 } 53 }
1 package ltd.newbee.mall.util; 2 3 import java.io.UnsupportedEncodingException; 4 import java.net.URLEncoder; 5 import java.security.KeyManagementException; 6 import java.security.NoSuchAlgorithmException; 7 import java.security.cert.X509Certificate; 8 import java.util.ArrayList; 9 import java.util.List; 10 import java.util.Map; 11 12 import javax.net.ssl.SSLContext; 13 import javax.net.ssl.TrustManager; 14 import javax.net.ssl.X509TrustManager; 15 16 import org.apache.commons.lang.StringUtils; 17 import org.apache.http.HttpResponse; 18 import org.apache.http.NameValuePair; 19 import org.apache.http.client.HttpClient; 20 import org.apache.http.client.entity.UrlEncodedFormEntity; 21 import org.apache.http.client.methods.HttpDelete; 22 import org.apache.http.client.methods.HttpGet; 23 import org.apache.http.client.methods.HttpPost; 24 import org.apache.http.client.methods.HttpPut; 25 import org.apache.http.conn.ClientConnectionManager; 26 import org.apache.http.conn.scheme.Scheme; 27 import org.apache.http.conn.scheme.SchemeRegistry; 28 import org.apache.http.conn.ssl.SSLSocketFactory; 29 import org.apache.http.entity.ByteArrayEntity; 30 import org.apache.http.entity.StringEntity; 31 import org.apache.http.impl.client.DefaultHttpClient; 32 import org.apache.http.message.BasicNameValuePair; 33 34 public class HttpUtil { 35 36 /** 37 * get 38 * 39 * @param host 40 * @param path 41 * @param method 42 * @param headers 43 * @param querys 44 * @return 45 * @throws Exception 46 */ 47 public static HttpResponse doGet(String host, String path, String method, 48 Map<String, String> headers, 49 Map<String, String> querys) 50 throws Exception { 51 HttpClient httpClient = wrapClient(host); 52 53 HttpGet request = new HttpGet(buildUrl(host, path, querys)); 54 for (Map.Entry<String, String> e : headers.entrySet()) { 55 request.addHeader(e.getKey(), e.getValue()); 56 } 57 58 return httpClient.execute(request); 59 } 60 61 /** 62 * post form 63 * 64 * @param host 65 * @param path 66 * @param method 67 * @param headers 68 * @param querys 69 * @param bodys 70 * @return 71 * @throws Exception 72 */ 73 public static HttpResponse doPost(String host, String path, String method, 74 Map<String, String> headers, 75 Map<String, String> querys, 76 Map<String, String> bodys) 77 throws Exception { 78 HttpClient httpClient = wrapClient(host); 79 80 HttpPost request = new HttpPost(buildUrl(host, path, querys)); 81 for (Map.Entry<String, String> e : headers.entrySet()) { 82 request.addHeader(e.getKey(), e.getValue()); 83 } 84 85 if (bodys != null) { 86 List<NameValuePair> nameValuePairList = new ArrayList<NameValuePair>(); 87 88 for (String key : bodys.keySet()) { 89 nameValuePairList.add(new BasicNameValuePair(key, bodys.get(key))); 90 } 91 UrlEncodedFormEntity formEntity = new UrlEncodedFormEntity(nameValuePairList, "utf-8"); 92 formEntity.setContentType("application/x-www-form-urlencoded; charset=UTF-8"); 93 request.setEntity(formEntity); 94 } 95 96 return httpClient.execute(request); 97 } 98 99 /** 100 * Post String 101 * 102 * @param host 103 * @param path 104 * @param method 105 * @param headers 106 * @param querys 107 * @param body 108 * @return 109 * @throws Exception 110 */ 111 public static HttpResponse doPost(String host, String path, String method, 112 Map<String, String> headers, 113 Map<String, String> querys, 114 String body) 115 throws Exception { 116 HttpClient httpClient = wrapClient(host); 117 118 HttpPost request = new HttpPost(buildUrl(host, path, querys)); 119 for (Map.Entry<String, String> e : headers.entrySet()) { 120 request.addHeader(e.getKey(), e.getValue()); 121 } 122 123 if (StringUtils.isNotBlank(body)) { 124 request.setEntity(new StringEntity(body, "utf-8")); 125 } 126 127 return httpClient.execute(request); 128 } 129 130 /** 131 * Post stream 132 * 133 * @param host 134 * @param path 135 * @param method 136 * @param headers 137 * @param querys 138 * @param body 139 * @return 140 * @throws Exception 141 */ 142 public static HttpResponse doPost(String host, String path, String method, 143 Map<String, String> headers, 144 Map<String, String> querys, 145 byte[] body) 146 throws Exception { 147 HttpClient httpClient = wrapClient(host); 148 149 HttpPost request = new HttpPost(buildUrl(host, path, querys)); 150 for (Map.Entry<String, String> e : headers.entrySet()) { 151 request.addHeader(e.getKey(), e.getValue()); 152 } 153 154 if (body != null) { 155 request.setEntity(new ByteArrayEntity(body)); 156 } 157 158 return httpClient.execute(request); 159 } 160 161 /** 162 * Put String 163 * @param host 164 * @param path 165 * @param method 166 * @param headers 167 * @param querys 168 * @param body 169 * @return 170 * @throws Exception 171 */ 172 public static HttpResponse doPut(String host, String path, String method, 173 Map<String, String> headers, 174 Map<String, String> querys, 175 String body) 176 throws Exception { 177 HttpClient httpClient = wrapClient(host); 178 179 HttpPut request = new HttpPut(buildUrl(host, path, querys)); 180 for (Map.Entry<String, String> e : headers.entrySet()) { 181 request.addHeader(e.getKey(), e.getValue()); 182 } 183 184 if (StringUtils.isNotBlank(body)) { 185 request.setEntity(new StringEntity(body, "utf-8")); 186 } 187 188 return httpClient.execute(request); 189 } 190 191 /** 192 * Put stream 193 * @param host 194 * @param path 195 * @param method 196 * @param headers 197 * @param querys 198 * @param body 199 * @return 200 * @throws Exception 201 */ 202 public static HttpResponse doPut(String host, String path, String method, 203 Map<String, String> headers, 204 Map<String, String> querys, 205 byte[] body) 206 throws Exception { 207 HttpClient httpClient = wrapClient(host); 208 209 HttpPut request = new HttpPut(buildUrl(host, path, querys)); 210 for (Map.Entry<String, String> e : headers.entrySet()) { 211 request.addHeader(e.getKey(), e.getValue()); 212 } 213 214 if (body != null) { 215 request.setEntity(new ByteArrayEntity(body)); 216 } 217 218 return httpClient.execute(request); 219 } 220 221 /** 222 * Delete 223 * 224 * @param host 225 * @param path 226 * @param method 227 * @param headers 228 * @param querys 229 * @return 230 * @throws Exception 231 */ 232 public static HttpResponse doDelete(String host, String path, String method, 233 Map<String, String> headers, 234 Map<String, String> querys) 235 throws Exception { 236 HttpClient httpClient = wrapClient(host); 237 238 HttpDelete request = new HttpDelete(buildUrl(host, path, querys)); 239 for (Map.Entry<String, String> e : headers.entrySet()) { 240 request.addHeader(e.getKey(), e.getValue()); 241 } 242 243 return httpClient.execute(request); 244 } 245 246 private static String buildUrl(String host, String path, Map<String, String> querys) throws UnsupportedEncodingException { 247 StringBuilder sbUrl = new StringBuilder(); 248 sbUrl.append(host); 249 if (!StringUtils.isBlank(path)) { 250 sbUrl.append(path); 251 } 252 if (null != querys) { 253 StringBuilder sbQuery = new StringBuilder(); 254 for (Map.Entry<String, String> query : querys.entrySet()) { 255 if (0 < sbQuery.length()) { 256 sbQuery.append("&"); 257 } 258 if (StringUtils.isBlank(query.getKey()) && !StringUtils.isBlank(query.getValue())) { 259 sbQuery.append(query.getValue()); 260 } 261 if (!StringUtils.isBlank(query.getKey())) { 262 sbQuery.append(query.getKey()); 263 if (!StringUtils.isBlank(query.getValue())) { 264 sbQuery.append("="); 265 sbQuery.append(URLEncoder.encode(query.getValue(), "utf-8")); 266 } 267 } 268 } 269 if (0 < sbQuery.length()) { 270 sbUrl.append("?").append(sbQuery); 271 } 272 } 273 274 return sbUrl.toString(); 275 } 276 277 private static HttpClient wrapClient(String host) { 278 HttpClient httpClient = new DefaultHttpClient(); 279 if (host.startsWith("https://")) { 280 sslClient(httpClient); 281 } 282 283 return httpClient; 284 } 285 286 private static void sslClient(HttpClient httpClient) { 287 try { 288 SSLContext ctx = SSLContext.getInstance("TLS"); 289 X509TrustManager tm = new X509TrustManager() { 290 @Override 291 public X509Certificate[] getAcceptedIssuers() { 292 return null; 293 } 294 @Override 295 public void checkClientTrusted(X509Certificate[] xcs, String str) { 296 297 } 298 @Override 299 public void checkServerTrusted(X509Certificate[] xcs, String str) { 300 301 } 302 }; 303 ctx.init(null, new TrustManager[] { tm }, null); 304 SSLSocketFactory ssf = new SSLSocketFactory(ctx); 305 ssf.setHostnameVerifier(SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER); 306 ClientConnectionManager ccm = httpClient.getConnectionManager(); 307 SchemeRegistry registry = ccm.getSchemeRegistry(); 308 registry.register(new Scheme("https", 443, ssf)); 309 } catch (KeyManagementException ex) { 310 throw new RuntimeException(ex); 311 } catch (NoSuchAlgorithmException ex) { 312 throw new RuntimeException(ex); 313 } 314 } 315 }
1 <?xml version="1.0" encoding="UTF-8"?> 2 <project xmlns="http://maven.apache.org/POM/4.0.0" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 4 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 5 <modelVersion>4.0.0</modelVersion> 6 7 <groupId>com.aliyun.api.gateway</groupId> 8 <artifactId>java.demo</artifactId> 9 <version>1.0-SNAPSHOT</version> 10 11 <dependencies> 12 <dependency> 13 <groupId>com.alibaba</groupId> 14 <artifactId>fastjson</artifactId> 15 <version>1.2.15</version> 16 </dependency> 17 <dependency> 18 <groupId>org.apache.httpcomponents</groupId> 19 <artifactId>httpclient</artifactId> 20 <version>4.2.1</version> 21 </dependency> 22 <dependency> 23 <groupId>org.apache.httpcomponents</groupId> 24 <artifactId>httpcore</artifactId> 25 <version>4.2.1</version> 26 </dependency> 27 <dependency> 28 <groupId>commons-lang</groupId> 29 <artifactId>commons-lang</artifactId> 30 <version>2.6</version> 31 </dependency> 32 <dependency> 33 <groupId>org.eclipse.jetty</groupId> 34 <artifactId>jetty-util</artifactId> 35 <version>9.3.7.v20160115</version> 36 </dependency> 37 <dependency> 38 <groupId>junit</groupId> 39 <artifactId>junit</artifactId> 40 <version>4.5</version> 41 <scope>test</scope> 42 </dependency> 43 </dependencies> 44 </project>
https://blog.csdn.net/weixin_39549656/article/details/79108151
以上是关于java实现发送短信的主要内容,如果未能解决你的问题,请参考以下文章