短信平台对接案例

短信平台会给一份接口文档,参照接口文档参数要求来做即可。
以下都是post请求
案例代码1

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

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.HttpPost;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;

public class MessagePlatfromDemo {
	public static void main(String[] args) {
		String platformUrl = "xxxx";
		//声明List集合,用来封装表单中的参数
		List<NameValuePair> paramsList = new ArrayList<NameValuePair>();
		paramsList.add(new BasicNameValuePair("参数", "值"));
		paramsList.add(new BasicNameValuePair("参数2", "值2"));
		paramsList.add(new BasicNameValuePair("参数3", "值3"));

		sendPostRequest(platformUrl,paramsList);
	}

	public static String sendPostRequest(String url, List<NameValuePair> params) {
		String result = "";
		// 创建httpClient对象
		HttpClient httpClient = HttpClients.createDefault();
		// 创建httpPost
		HttpPost httpPost = new HttpPost(url);

		try {
			httpPost.setEntity(new UrlEncodedFormEntity(params, "UTF-8"));
			HttpResponse response = null;
			// 执行请求
			try {
				response = httpClient.execute(httpPost);
				HttpEntity entity = response.getEntity();
				if (entity != null) {
					InputStream inputStream = entity.getContent();
					result = getExecuteStatus(inputStream);

				}

			} catch (IOException e) {
				e.printStackTrace();
			}
			;

		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}

		return result;

	}

	public static String getExecuteStatus(InputStream is) {
		BufferedReader reader = new BufferedReader(new InputStreamReader(is));
		String line = "";
		try {
			while ((line = reader.readLine()) != null) {
				/**
				 * 处理返回结果内容 可能是xml格式或者其他
				 */

			}
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		// 返回能判断成功的值
		return null;
	}

}

案例代码2

package com.liangxun.common;

import java.io.DataInputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.net.URL;
import java.net.URLConnection;

public class Message 
{
	/**
	 * 通过URL地址的形式发送短信
	 * @param mobileNum
	 * @param content
	 * @return
	 */
	public static String sendMobileMessageByURL(String mobileNum,String content) {
		String state = "0";
		try {
	      URL url = new URL("http://xxxxxx");//短信URL地址
	      URLConnection connection = url.openConnection();
	      connection.setDoOutput(true);
	      OutputStreamWriter out = new OutputStreamWriter(connection.getOutputStream(), "UTF-8");
	      String MobileMessageInfo = "参数1=111111&参数2="+MessageMD5.MD5("111111")+"&PHONE="+mobileNum+"&CONTENT="+content;
	      out.write(MobileMessageInfo);
	      out.flush();
	      out.close();
	      DataInputStream in = new DataInputStream(connection.getInputStream());
	      state = in.readLine();//1为短信发送成功,否则短信发送失败
	      in.close();
	    }
	    catch (IOException e) {
	      System.out.println(e);
	    }
	    return state;
	}
	
	public static void main(String args[]){
		sendMobileMessageByURL("156511xx907","您的验证码:123456【时代】");
	}
}

package com.liangxun.common;

import java.security.MessageDigest;

public class MessageMD5 {
	 public final static String MD5(String s) {
		    char hexdigits[] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd','e', 'f'};

		    try {
		      byte[] strTemp = s.getBytes();
		      MessageDigest mdTemp = MessageDigest.getInstance("MD5");
		      mdTemp.update(strTemp);
		      byte[] md = mdTemp.digest();
		      int j = md.length;
		      char str[] = new char[j * 2];
		      int k = 0;
		      for (int i = 0; i < j; i++) {
		        byte byte0 = md[i];
		        str[k++] = hexdigits[byte0 >>> 4 & 0xf];
		        str[k++] = hexdigits[byte0 & 0xf];
		      }
		      return new String(str);
		    }
		    catch (Exception e) {
		      return null;
		    }
		  }

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值