短信java_java实现发送短信
发布日期:2021-06-24 17:57:23 浏览次数:2 分类:技术文章

本文共 7456 字,大约阅读时间需要 24 分钟。

1 packageltd.newbee.mall.util;2

3 importjava.io.UnsupportedEncodingException;4 importjava.net.URLEncoder;5 importjava.security.KeyManagementException;6 importjava.security.NoSuchAlgorithmException;7 importjava.security.cert.X509Certificate;8 importjava.util.ArrayList;9 importjava.util.List;10 importjava.util.Map;11

12 importjavax.net.ssl.SSLContext;13 importjavax.net.ssl.TrustManager;14 importjavax.net.ssl.X509TrustManager;15

16 importorg.apache.commons.lang.StringUtils;17 importorg.apache.http.HttpResponse;18 importorg.apache.http.NameValuePair;19 importorg.apache.http.client.HttpClient;20 importorg.apache.http.client.entity.UrlEncodedFormEntity;21 importorg.apache.http.client.methods.HttpDelete;22 importorg.apache.http.client.methods.HttpGet;23 importorg.apache.http.client.methods.HttpPost;24 importorg.apache.http.client.methods.HttpPut;25 importorg.apache.http.conn.ClientConnectionManager;26 importorg.apache.http.conn.scheme.Scheme;27 importorg.apache.http.conn.scheme.SchemeRegistry;28 importorg.apache.http.conn.ssl.SSLSocketFactory;29 importorg.apache.http.entity.ByteArrayEntity;30 importorg.apache.http.entity.StringEntity;31 importorg.apache.http.impl.client.DefaultHttpClient;32 importorg.apache.http.message.BasicNameValuePair;33

34 public classHttpUtil {35

36 /**

37 * get38 *39 *@paramhost40 *@parampath41 *@parammethod42 *@paramheaders43 *@paramquerys44 *@return

45 *@throwsException46 */

47 public staticHttpResponse doGet(String host, String path, String method,48 Mapheaders,49 Mapquerys)50 throwsException {51 HttpClient httpClient =wrapClient(host);52

53 HttpGet request = newHttpGet(buildUrl(host, path, querys));54 for (Map.Entrye : headers.entrySet()) {55 request.addHeader(e.getKey(), e.getValue());56 }57

58 returnhttpClient.execute(request);59 }60

61 /**

62 * post form63 *64 *@paramhost65 *@parampath66 *@parammethod67 *@paramheaders68 *@paramquerys69 *@parambodys70 *@return

71 *@throwsException72 */

73 public staticHttpResponse doPost(String host, String path, String method,74 Mapheaders,75 Mapquerys,76 Mapbodys)77 throwsException {78 HttpClient httpClient =wrapClient(host);79

80 HttpPost request = newHttpPost(buildUrl(host, path, querys));81 for (Map.Entrye : headers.entrySet()) {82 request.addHeader(e.getKey(), e.getValue());83 }84

85 if (bodys != null) {86 List nameValuePairList = new ArrayList();87

88 for(String key : bodys.keySet()) {89 nameValuePairList.add(newBasicNameValuePair(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 returnhttpClient.execute(request);97 }98

99 /**

100 * Post String101 *102 *@paramhost103 *@parampath104 *@parammethod105 *@paramheaders106 *@paramquerys107 *@parambody108 *@return

109 *@throwsException110 */

111 public staticHttpResponse doPost(String host, String path, String method,112 Mapheaders,113 Mapquerys,114 String body)115 throwsException {116 HttpClient httpClient =wrapClient(host);117

118 HttpPost request = newHttpPost(buildUrl(host, path, querys));119 for (Map.Entrye : 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 returnhttpClient.execute(request);128 }129

130 /**

131 * Post stream132 *133 *@paramhost134 *@parampath135 *@parammethod136 *@paramheaders137 *@paramquerys138 *@parambody139 *@return

140 *@throwsException141 */

142 public staticHttpResponse doPost(String host, String path, String method,143 Mapheaders,144 Mapquerys,145 byte[] body)146 throwsException {147 HttpClient httpClient =wrapClient(host);148

149 HttpPost request = newHttpPost(buildUrl(host, path, querys));150 for (Map.Entrye : headers.entrySet()) {151 request.addHeader(e.getKey(), e.getValue());152 }153

154 if (body != null) {155 request.setEntity(newByteArrayEntity(body));156 }157

158 returnhttpClient.execute(request);159 }160

161 /**

162 * Put String163 *@paramhost164 *@parampath165 *@parammethod166 *@paramheaders167 *@paramquerys168 *@parambody169 *@return

170 *@throwsException171 */

172 public staticHttpResponse doPut(String host, String path, String method,173 Mapheaders,174 Mapquerys,175 String body)176 throwsException {177 HttpClient httpClient =wrapClient(host);178

179 HttpPut request = newHttpPut(buildUrl(host, path, querys));180 for (Map.Entrye : 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 returnhttpClient.execute(request);189 }190

191 /**

192 * Put stream193 *@paramhost194 *@parampath195 *@parammethod196 *@paramheaders197 *@paramquerys198 *@parambody199 *@return

200 *@throwsException201 */

202 public staticHttpResponse doPut(String host, String path, String method,203 Mapheaders,204 Mapquerys,205 byte[] body)206 throwsException {207 HttpClient httpClient =wrapClient(host);208

209 HttpPut request = newHttpPut(buildUrl(host, path, querys));210 for (Map.Entrye : headers.entrySet()) {211 request.addHeader(e.getKey(), e.getValue());212 }213

214 if (body != null) {215 request.setEntity(newByteArrayEntity(body));216 }217

218 returnhttpClient.execute(request);219 }220

221 /**

222 * Delete223 *224 *@paramhost225 *@parampath226 *@parammethod227 *@paramheaders228 *@paramquerys229 *@return

230 *@throwsException231 */

232 public staticHttpResponse doDelete(String host, String path, String method,233 Mapheaders,234 Mapquerys)235 throwsException {236 HttpClient httpClient =wrapClient(host);237

238 HttpDelete request = newHttpDelete(buildUrl(host, path, querys));239 for (Map.Entrye : headers.entrySet()) {240 request.addHeader(e.getKey(), e.getValue());241 }242

243 returnhttpClient.execute(request);244 }245

246 private static String buildUrl(String host, String path, Map querys) throwsUnsupportedEncodingException {247 StringBuilder sbUrl = newStringBuilder();248 sbUrl.append(host);249 if (!StringUtils.isBlank(path)) {250 sbUrl.append(path);251 }252 if (null !=querys) {253 StringBuilder sbQuery = newStringBuilder();254 for (Map.Entryquery : querys.entrySet()) {255 if (0

274 returnsbUrl.toString();275 }276

277 private staticHttpClient wrapClient(String host) {278 HttpClient httpClient = newDefaultHttpClient();279 if (host.startsWith("https://")) {280 sslClient(httpClient);281 }282

283 returnhttpClient;284 }285

286 private static voidsslClient(HttpClient httpClient) {287 try{288 SSLContext ctx = SSLContext.getInstance("TLS");289 X509TrustManager tm = newX509TrustManager() {290 @Override291 publicX509Certificate[] getAcceptedIssuers() {292 return null;293 }294 @Override295 public voidcheckClientTrusted(X509Certificate[] xcs, String str) {296

297 }298 @Override299 public voidcheckServerTrusted(X509Certificate[] xcs, String str) {300

301 }302 };303 ctx.init(null, new TrustManager[] { tm }, null);304 SSLSocketFactory ssf = newSSLSocketFactory(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 newRuntimeException(ex);311 } catch(NoSuchAlgorithmException ex) {312 throw newRuntimeException(ex);313 }314 }315 }

转载地址:https://blog.csdn.net/weixin_34237700/article/details/114023512 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!

上一篇:网上书店系统mysql设计_数据库设计--《网上书店系统》
下一篇:python 多态 锁_多态 - 廖雪峰的官方网站

发表评论

最新留言

能坚持,总会有不一样的收获!
[***.219.124.196]2024年04月03日 21时06分09秒