短信发送工具(2)

/**  
 * Copyright © 2017公司名字. All rights reserved.
 *
 * @Title: SendValidCode.java
 * @Prject: test
 * @Package: testsendvalidation
 * @Description: TODO
 * @author:  
 * @date: 
 * @version: V1.0  
 */
package com.js.ai.smssend;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.security.MessageDigest;
import java.util.ArrayList;
import java.util.List;

import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESKeySpec;
import javax.crypto.spec.IvParameterSpec;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;

/**
 * @ClassName: SendValidCode
 * @Description: 发送验证码
 * @author: 
 * @date: 
 */
public class SendValidCode {
	// 短信发送的接口网关
	private static String sendUrl = "http://120.55.197.77:1210/Services/MsgSend.asmx/sendMsgByEncrypt";

	/**
	 * @Title: sendMessage
	 * @Description: 验证码短信发送
	 * @param userName:发送的用户名
	 * @param userPass:密码
	 * @param iphoneNum:电话号码
	 * @param signature:签名
	 * @return
	 * 
	 * @return: String返回发送的结果
	 */
	public static String  sendMessage(String userName, String userPass, String iphoneNum, String signature,
			String validcode, String channel) {
		// 发送结果
		String msgContent = "验证码:" + validcode + ",请在5分钟内完成验证。如非本人操作,请忽略。";// 发送消息的内容
		String encrypStrPara1 = "userPass=" + userPass + "&DesNo=" + iphoneNum + "&Msg=" + msgContent + "【" + signature +"】&Channel=" + channel;

		String encryptStr = DESEncrypt(encrypStrPara1, userPass);

		List<NameValuePair> nvps = new ArrayList<NameValuePair>();
		nvps.add(new BasicNameValuePair("userCode", userName));
		nvps.add(new BasicNameValuePair("submitInfo", encryptStr));

		String post = httpPost(sendUrl, nvps); // post请求

		//String getparam = "userCode=" + userName +"&submitInfo=" + encryptStr;
		//String result = httpGet(sendUrl, getparam); // get请求

		// 返回结果
		return  post;

; } private static String DESEncrypt(String encryptStr, String key) { String result = ""; try { String encryptKey = SHA1(key).substring(0, 8).toUpperCase(); ; DESKeySpec desKeySpec = new DESKeySpec(encryptKey.getBytes()); SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES"); SecretKey securekey = keyFactory.generateSecret(desKeySpec); Cipher cipher = Cipher.getInstance("DES/CBC/PKCS5Padding"); IvParameterSpec ivp = new IvParameterSpec(encryptKey.getBytes()); cipher.init(Cipher.ENCRYPT_MODE, securekey, ivp); byte[] encryptResult = cipher.doFinal(encryptStr.getBytes("GB2312")); result = BinaryToHexString(encryptResult); } catch (Exception e) { System.out.println(e.getStackTrace()); System.out.println(e.getMessage()); } return result; } private static String SHA1(String key) { StringBuffer sb = new StringBuffer(); try { MessageDigest md = MessageDigest.getInstance("SHA-1"); md.update(key.getBytes("GB2312")); byte[] resultsha = md.digest(); for (byte b : resultsha) { int i = b & 0xff; if (i < 0xf) { sb.append(0); } sb.append(Integer.toHexString(i)); } } catch (Exception e) { } return sb.toString(); } private static String BinaryToHexString(byte[] bytes) { String hexStr = "0123456789ABCDEF"; String result = ""; String hex = ""; for (int i = 0; i < bytes.length; i++) { // 字节高4位 hex = String.valueOf(hexStr.charAt((bytes[i] & 0xF0) >> 4)); // 字节低4位 hex += String.valueOf(hexStr.charAt(bytes[i] & 0x0F)); result += hex; } return result; } private static String httpPost(String url, List<NameValuePair> params) { String result = ""; try { HttpClient httpclient = new DefaultHttpClient(); HttpPost httpPost = new HttpPost(url); httpPost.setEntity(new UrlEncodedFormEntity(params, "UTF-8")); HttpResponse response = httpclient.execute(httpPost); HttpEntity entity = response.getEntity(); if (entity != null) { InputStream instreams = entity.getContent(); result = convertStreamToString(instreams); } } catch (Exception e) { } return result; } private static String httpGet(String url, String params) { String result = ""; try { HttpClient client = new DefaultHttpClient(); if (params != "") { url = url + "?" + params; } HttpGet httpget = new HttpGet(url); HttpResponse response = client.execute(httpget); HttpEntity entity = response.getEntity(); if (entity != null) { InputStream instreams = entity.getContent(); result = convertStreamToString(instreams); } } catch (Exception e) { } return result; } private static String convertStreamToString(InputStream is) { BufferedReader reader = new BufferedReader(new InputStreamReader(is)); StringBuilder sb = new StringBuilder(); String line = null; try { while ((line = reader.readLine()) != null) { sb.append(line + "\n"); } } catch (IOException e) { e.printStackTrace(); } finally { try { is.close(); } catch (IOException e) { e.printStackTrace(); } } return sb.toString(); } /*public static void main(String[] args) { String result = SendValidCode.sendMessage("XMSTCF", "XMST123abc", "15080328553", "万兆天空", "123456","0"); System.err.println(result+">>>>>>>"); }*/ }

  

posted @ 2017-04-21 18:37  ATJAVA  阅读(221)  评论(0编辑  收藏  举报