TestSMS
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 {/*** 調(diào)用postNormalMessage方法* 需要5個(gè)參數(shù)* 參數(shù)1:ID(是什么ID)* 參數(shù)2:密碼* 參數(shù)3:手機(jī)號(hào)(這里的參數(shù)是一個(gè)字符串,但是如果是多個(gè)是用逗號(hào)隔開(kāi)?還是只能傳一個(gè),如果是多個(gè)需要循環(huán)遍歷)* 參數(shù)5:Type A/LA代表選擇哪種類型 決定長(zhǎng)度 收費(fèi)不一樣 默認(rèn)是A LA收費(fèi)更貴* 參數(shù)4:message 代表消息主體*/public static void postNormalMessage(String message,String mobile) {/*** 供應(yīng)商的地址*/final String gatewayURL ="https://www.commzgate.net/gateway/SendMsg";/*** 這個(gè)ID是供應(yīng)商提供的,不是郵箱ID*/final String ID = "965200028888";/*** 密碼*/final String password = "Yuan07058";/*** 需要發(fā)送的手機(jī)號(hào)碼,到時(shí)候可能需要通過(guò)傳入?yún)?shù)的形式發(fā)送*///final String mobile = "65983732128888";/*** 參數(shù)5:Type A/LA代表選擇哪種類型 決定長(zhǎng)度 收費(fèi)不一樣 默認(rèn)是A LA收費(fèi)更貴*/final String type = "A";/*** 定義一個(gè)長(zhǎng)度為2的String數(shù)組 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;//聲明數(shù)據(jù)輸出流對(duì)象DataOutputStream out =null;BufferedReader in =null;//1.得到訪問(wèn)地址的URLURL theURL = new URL(gatewayURL+"?");//2.得到網(wǎng)絡(luò)訪問(wèn)對(duì)象java.net.HttpURLConnectionhttpURLConnector = (HttpURLConnection) theURL.openConnection();//3.設(shè)置請(qǐng)求方式為POSThttpURLConnector.setRequestMethod("POST");httpURLConnector.setRequestProperty("Content-Type", "application/x-www-form-urlencoded;charset=UTF-8");//4.設(shè)置請(qǐng)求參數(shù)(過(guò)期時(shí)間,輸入、輸出流、訪問(wèn)方式),以流的形式進(jìn)行連接 //設(shè)置是否從httpUrlConnection讀入httpURLConnector.setDoInput(true);//設(shè)置是否向HttpURLConnection輸出httpURLConnector.setDoOutput(true);//設(shè)置超時(shí)時(shí)間httpURLConnector.setReadTimeout(30000);//30000//連接httpURLConnector.connect();//httpURLConnector.getOutputStream()獲取outputStream//實(shí)例化數(shù)據(jù)輸出流對(duì)象 從流中讀取響應(yīng)信息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"));//這里發(fā)送的信息是自己給的參數(shù)拼裝好的response[1] = "Posting: "+gatewayURL+"?"+sb.toString();//把消息體寫(xiě)給CG,writeBytes()傳的參數(shù)是字符串out.writeBytes(sb.toString());//API提供的是傳入字符串,所以傳入字節(jié)數(shù)組先放在這里//out.write(sb.toString().getBytes());out.close();//這個(gè)判斷是自己加的,默認(rèn)給的api是沒(méi)有這個(gè)判斷的if(httpURLConnector.getResponseCode()==200) {//正常成功是200的,只是不知道提供API會(huì)不會(huì)做特殊處理System.out.println("如果responseCode等于200就說(shuō)明請(qǐng)求成功了");}else {System.out.println("請(qǐng)求失敗");}in = new BufferedReader(new InputStreamReader(httpURLConnector.getInputStream()));//讀取數(shù)據(jù),這里只讀取一行,所以CG只返回一行,不需要循環(huán)讀取response[0] = in.readLine();// 關(guān)閉流in.close();// 斷開(kāi)連接,釋放資源httpURLConnector.disconnect();}catch(Exception e){System.out.println("發(fā)送短信有異常");}}}
public class Test {public static void main(String[] args) {String mobile = "6593872783888";StringBuilder content = new StringBuilder();content.append("UBI");content.append("-");content.append("HeaderQuarter");content.append("-");content.append("G/F");content.append("-");content.append("1.1.2");content.append(" occurs ");content.append("OVP,");content.append(" this message is from ABCD!");String message = content.toString();System.out.println(message);SMSUtils.postNormalMessage(message, mobile);// UBI-HeaderQuarter-G/F-1.1.2 occurs OVP, this message is from ABCD!// 如果responseCode等于200就說(shuō)明請(qǐng)求成功了}
}
?
總結(jié)
- 上一篇: 使用jquery进行多行表格数据验证
- 下一篇: JAVA生成随机字符串方法