互亿无线 · 文档中心

短信营销 - 代码示例


JAVA对接短信营销接口DEMO示例
本文为您提供了JAVA语言版本的短信营销接口对接DEMO示例

短信营销接口文档 点击下载

短信营销接口接入指南 点击访问

// 接口类型:互亿无线营销短信批量发送DEMO
// 账户注册:请通过该地址开通账户 http://user.ihuyi.com/

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpException;
import org.apache.commons.httpclient.methods.PostMethod;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;

import util.StringUtil;

public class sendsms {
	private static String Url = "http://api.ihuyi.com/sms-yx/v1/batchSend";
	
	public static void main(String [] args) {
		
		HttpClient client = new HttpClient(); 
		PostMethod method = new PostMethod(Url);
		client.getParams().setContentCharset("UTF-8");
		method.setRequestHeader("ContentType", "application/json;charset=UTF-8");

		// 需要修改的参数-开始
		String api_id = "sms-xxxxxxxx";
		String api_key = "xxxxxxxxxxxxxxxxxxx";
		String[] phone = {"1366197xxxx","1366197xxxx"};
		String sign_name = "互亿无线";
		int product_id = 1018;
		String content = new String("您好,短信内容,回T退订");
		// 需要修改的参数-结束

		long timest = System.currentTimeMillis() / 1000;
		String timestamp = "" + timest;
		String request_id = timestamp + (int)((Math.random()*9+1)*100000);

		// 生成sign方式一
//		Map map = new HashMap();
//		map.put("api_id", api_id);
//		map.put("api_key", api_key);
//		map.put("request_id", request_id);
//		map.put("timestamp", ""+timest);
//		String sign = StringUtil.getSign(map);
//		System.out.println(sign);

		// 生成sign方式二
		String sig = "api_id=" + api_id + "&api_key=" + api_key + "&request_id=" + request_id + "×tamp=" + timest;
		// System.out.println(sign);
		String sign = StringUtil.MD5Encode(sig).toLowerCase();

		JSONObject data = new JSONObject();
		data.put("api_id", api_id);
		data.put("signature", sign);
		data.put("phone", phone);
		data.put("sign_name", sign_name);
		data.put("request_id", request_id);
		data.put("timestamp", timest);
		data.put("product_id", product_id);
		data.put("send_time", "");
	    data.put("content", content);
		
	    // String arrStr = JSON.toJSONString(data);
	    String objStr = JSON.toJSONString(data);
	    System.out.println(data);
		method.setRequestBody(objStr);

		try {
			client.executeMethod(method);
			String SubmitResult =method.getResponseBodyAsString();
			// System.out.println(SubmitResult);
			JSONObject obj = JSON.parseObject(SubmitResult);		
			System.out.println(obj);
			if("OK".equals(obj.getString("code"))){
				System.out.println("短信提交成功");
			}else{
				 System.out.println("短信提交失败");
			}
		} catch (HttpException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally{
			// Release connection
			method.releaseConnection();
			// client.getConnectionManager().shutdown();
		}
		
	}


}

现在注册,即享新用户礼包!

请输入验证码免费体验产品

收不到短信验证码?
×
服务热线:
4008 808 898