/**
 * Company Name : 中贸促信息技术有限责任公司
 * Project Name:sfzxWebsite
 * File Name:SmsController.java
 * Package Name:ccpit.business.smsManage
 * Date:2017年5月8日下午3:26:23
 * Copyright (c) 2017, dingwei@ccpit.org All Rights Reserved.
 *
*/

package org.ccpit.business.smsManage;

import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

import net.sf.json.JSONObject;

/**
 * ClassName:SmsController <br/>
 * Function: TODO ADD FUNCTION. <br/>
 * Reason:	 TODO ADD REASON. <br/>
 * Date:     2017年5月8日 下午3:26:23 <br/>
 * @author   dingwei
 * @version  
 * @since    JDK 1.8.0
 * @see 	 
 */
public class SmsController {

	//发送短信地址
	private static String SEND_SMS_PATH = "http://c.realsms.cn:38811/api/v10/send?usr=test02&pwd=123123&src=999&dest=TELEPHONE&msg=SMSCONTENT";
	
	public static void main(String[] args){
		String newTelephone = "18019620836";
		String newSmsContent = "验证码：ifco客户端测试";
		sendSms(newTelephone,newSmsContent);
	}
	
	/**
	 * 
	 * sendSms:(发送短信功能). <br/>
	 * @author dingwei
	 * @param newTelephone 手机号
	 * @param newSmsContent 短信内容
	 * Date:2017年5月8日下午4:29:04
	 * @since JDK 1.8.0
	 */
	private static void sendSms(String newTelephone,String newSmsContent){
		String send_sms_path = SEND_SMS_PATH.replace("TELEPHONE", newTelephone).replace("SMSCONTENT", newSmsContent);
		JSONObject jsonObject = null;
		try {
			URL url = new URL(send_sms_path);
			HttpURLConnection http =  (HttpURLConnection) url.openConnection();
			http.setRequestMethod("GET");
			http.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
			http.setDoInput(true);
			http.setDoOutput(true);
			System.setProperty("sun.net.client.defaultConnectTimeout", "30000");// 连接超时30秒
			System.setProperty("sun.net.client.defaultReadTimeout", "30000"); // 读取超时30秒

			InputStream is = http.getInputStream();
			int contentLength = http.getContentLength();
			byte[] bt = new byte[1024];
			http.connect();
			is.read(bt);
			String result = new String(bt,"UTF-8");
			jsonObject = JSONObject.fromObject(result);
			int sendCode = (int) jsonObject.get("code");
			if(0 == sendCode){
				System.out.println(jsonObject.get("msg"));
			}else {
				System.out.println("短信发送失败！");
			}
			is.close();
		} catch (MalformedURLException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}

