SMSUtil

import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;

public class SMSUtils {
	
	/**
	 * 调用postNormalMessage方法
	 * 需要5个参数
	 * 参数1:ID(是什么ID)
	 * 参数2:密码
	 * 参数3:手机号(这里的参数是一个字符串,但是如果是多个是用逗号隔开?还是只能传一个,如果是多个需要循环遍历)
	 * 参数5:Type A/LA代表选择哪种类型 决定长度  收费不一样 默认是A LA收费更贵
	 * 参数4:message 代表消息主体
	 */
	public static void postNormalMessage(String message,String mobile) {
		
		/**
		 * 供应商的地址
		 */
		final String gatewayURL ="https://www.commzgate.net/gateway/SendMsg";
		/**
		 * 这个ID是供应商提供的,不是邮箱ID
		 */
		final String ID = "admin";
		/**
		 * 密码
		 */
		final String password = "1234";
		/**
		 * 需要发送的手机号码,到时候可能需要通过传入参数的形式发送
		 */
		//final String mobile = "18888888888";
		/**
		 * 参数5:Type A/LA代表选择哪种类型 决定长度  收费不一样 默认是A LA收费更贵
		 */
		final String type = "A";
		
		/**
		 * 定义一个长度为2的String数组 Array with 2 parts.
		 * Part 1 is what YOU receive from CG.
		 * Part 2 is what YOU send
		 */
		String[] response = new String[2];
		
		try{			 
	         HttpURLConnection httpURLConnector = null;
	         //声明数据输出流对象
	         DataOutputStream out =null;
	         BufferedReader in =null;
	         //1.得到访问地址的URL
	         URL theURL = new URL(gatewayURL+"?");
	         //2.得到网络访问对象java.net.HttpURLConnection
	         httpURLConnector = (HttpURLConnection) theURL.openConnection();
	         //3.设置请求方式为POST
	         httpURLConnector.setRequestMethod("POST");
	         httpURLConnector.setRequestProperty("Content-Type", "application/x-www-form-urlencoded;charset=UTF-8");
	         //4.设置请求参数(过期时间,输入、输出流、访问方式),以流的形式进行连接 
	         //设置是否从httpUrlConnection读入
	         httpURLConnector.setDoInput(true);
	         //设置是否向HttpURLConnection输出
	         httpURLConnector.setDoOutput(true);
	         //设置超时时间
	         httpURLConnector.setReadTimeout(30000);//30000
	         //连接
	         httpURLConnector.connect();
	         //httpURLConnector.getOutputStream()获取outputStream
	         //实例化数据输出流对象 从流中读取响应信息
             out = new DataOutputStream(httpURLConnector.getOutputStream());

             StringBuffer sb = new StringBuffer();
             sb.append("ID=").append(URLEncoder.encode(ID, "UTF-8"));
             sb.append("&").append("Password=").append(URLEncoder.encode(password, "UTF-8"));
             sb.append("&").append("Mobile=").append(URLEncoder.encode(mobile, "UTF-8"));
             sb.append("&").append("Message=").append(URLEncoder.encode(message, "UTF-8"));
             sb.append("&").append("Type=").append(URLEncoder.encode(type, "UTF-8"));

             //这里发送的信息是自己给的参数拼装好的
             response[1] = "Posting: "+gatewayURL+"?"+sb.toString();
             //把消息体写给CG,writeBytes()传的参数是字符串
             out.writeBytes(sb.toString());
             //API提供的是传入字符串,所以传入字节数组先放在这里
             //out.write(sb.toString().getBytes());
             out.close();
             
             //这个判断是自己加的,默认给的api是没有这个判断的
             if(httpURLConnector.getResponseCode()==200) {
            	 //正常成功是200的,只是不知道提供API会不会做特殊处理
            	 System.out.println("如果responseCode等于200就说明请求成功了");
             }else {
            	 System.out.println("请求失败");
             }
             in = new BufferedReader(new InputStreamReader(httpURLConnector.getInputStream()));
             //读取数据,这里只读取一行,所以CG只返回一行,不需要循环读取
             response[0] = in.readLine();
             // 关闭流
             in.close();
             // 断开连接,释放资源
             httpURLConnector.disconnect();
        }catch(Exception e){
        	System.out.println("发送短信有异常");
        }
	}
	
}

猜你喜欢

转载自blog.csdn.net/Leon_Jinhai_Sun/article/details/85273335