短信发送接口demo

public class SendValidCode {
	// 短信发送的接口网关
	private static String sendUrl = "****************************";

	/**
	 * @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 + ",请在60秒内完成验证。如非本人操作,请忽略。";// 发送消息的内容
		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", "185****3547", "万兆天空", "123456","0");
		System.out.println(result+">>>>>>>");

	 }*/

}

  

时间: 2024-08-13 13:19:19

短信发送接口demo的相关文章

短信发送接口被恶意访问的网络攻击事件(二)肉搏战-阻止恶意请求

图形验证码+ip(用户id)+https http://www.cnblogs.com/han-1034683568/p/7040417.html 前言 承接前文<短信发送接口被恶意访问的网络攻击事件(一)紧张的遭遇战险胜>,在解决了短信发送的问题后,长长地舒了口气,也就各忙各的事情去了,本以为应该是个完美的收场,哪知道只是泥泞道路的前一段,收场是收不了了,还是要去应付接下来的烂摊子,因为攻击者并没有停止攻击,虽然恶意请求已经可以被识别并且不会被业务服务器处理,也不会去触发短信发送接口,但是请

阿里大鱼短信发送接口开发

一. API接口介绍 alibaba.aliqin.fc.sms.num.send (短信发送) 向指定手机号码发送模板短信,模板内可设置部分变量.使用前需要在阿里大于管理中心添加短信签名与短信模板.测试时请直接使用正式环境HTTP请求地址. [重要]批量发送(一次传递多个号码eg:1381111111,1382222222)会产生相应的延迟,触达时间要求高的建议单条发送 公共参数 请求地址: 环境 HTTP请求地址 HTTPS请求地址 正式环境 http://gw.api.taobao.com

php编程之短信发送接口调用流程的实现

对于一个交互型网站来说,需要用户来登录注册使用,那么一般网站的技术人员肯定需要实现注册时的短信发送功能,但是这个功能很多技术人员发现在添加的过程中有许多问题,要么是无法接收,要么就是时间慢等等原因,对于这个问题,下面就跟大家说一下如何快速有效的实现. 本文主要讲解的是如何利用m.5c.com.cn短信平台发送短信的过程中遇到的问题及接口调用流程实现. 1.打开php.ini,找到extension=php_openssl.dll,将前面的分号去掉,重启服务器.若此配置已打开,跳过这一步. 2.发

验证码,短信发送接口

所在项目 功能 所在页面 action方法 所调用短信发送方法 shop 用手机号找回密码,获取验证码 http://www.liwai.com/malluser/initFindPWD.htm com.m6699.cyt.view.mall.BtcRegistAction.checkMobile2() com.m6699.cyt.util.Message3G.sendMsg(String, String) shopmini 输入绑定手机号找回密码,获取验证码 http://m.liwai.co

【PYTHON】创蓝253云通讯平台国际短信API接口DEMO

#!/usr/local/bin/python#-- coding:utf-8 -- Author: jacky Time: 14-2-22 下午11:48 Desc: 短信http接口的python代码调用示例 import httplibimport urllibimport json#服务地址 host = "intapi.253.com" #端口号port = 80 #版本号version = "v1.1" #查账户信息的URIbalance_get_uri

【C(C++)】创蓝253云通讯平台国际短信API接口DEMO

#include <arpa/inet.h>#include <assert.h>#include <errno.h>#include <netinet/in.h>#include <signal.h>#include <stdlib.h>#include <stdio.h>#include <string.h>#include <sys/types.h>#include <sys/socke

【Ruby】国际短信API接口demo

说明: 以下代码只是为了方便客户测试而提供的示例代码,客户可以根据自己的需要另行编写 该代码仅供学习和研究接口使用,只是提供了一个参考 require 'net/http' require 'uri' require 'json' params = { "account" => "", "password" => "a.123456", 手机号码,格式(区号+手机号码),例如:8615800000000,其中86为

【shell】创蓝253云通讯平台国际短信API接口DEMO

#!/bin/sh account and password can be change to what you wanna! #author ChuangLan#修改为您的accountaccount=""#修改为您的pwpassword="a.123456"#手机号码,格式(区号+手机号码),例如:8615800000000,其中86为中国的区号mobile="8615800000000"#设置您要发送的内容msg="[253云通讯

【ASP】创蓝253云通讯平台国际短信API接口DEMO

<%@LANGUAGE="VBSCRIPT" CODEPAGE="65001"%> <% Function Post(url,data) dim Https set Https=server.createobject("MSXML2.XMLHTTP") Https.open "POST",url,false Https.setRequestHeader "Content-Type",&qu