短信接口java代碼 Java接口代碼

手機短信驗證碼java接口怎么寫

這個首先你要確定一下短信平臺,他們會給你提供短信實現(xiàn)的接口文檔。

成都創(chuàng)新互聯(lián)堅持“要么做到,要么別承諾”的工作理念,服務領域包括:網(wǎng)站建設、成都網(wǎng)站設計、企業(yè)官網(wǎng)、英文網(wǎng)站、手機端網(wǎng)站、網(wǎng)站推廣等服務,滿足客戶于互聯(lián)網(wǎng)時代的訥河網(wǎng)站設計、移動媒體設計的需求,幫助企業(yè)找到有效的互聯(lián)網(wǎng)解決方案。努力成為您成熟可靠的網(wǎng)絡建設合作伙伴!

比如:

public static String doPost(String reqUrl, Map parameters, String recvEncoding)

{

HttpURLConnection url_con = null;

String responseContent = null;

try

{

StringBuffer params = new StringBuffer();

Iterator iter = parameters.entrySet().iterator();

while (iter

.hasNext())

{

Map.Entry element = (Map.Entry)iter.next();

params.append(element.getKey().toString());

params.append("=");

params.append(URLEncoder.encode(element.getValue().toString(),

requestEncoding));

params.append("");

}

if (params.length() 0)

{

params = params.deleteCharAt(params.length() - 1);

}

URL url = new URL(reqUrl);

url_con = (HttpURLConnection)url.openConnection();

url_con.setRequestMethod("POST");

System.setProperty("sun.net.client.defaultConnectTimeout",

String.valueOf(connectTimeOut));

System.setProperty("sun.net.client.defaultReadTimeout",

String.valueOf(readTimeOut));

url_con.setDoOutput(true);

byte[] b = params.toString().getBytes();

url_con.getOutputStream().write(b, 0, b.length);

url_con.getOutputStream().flush();

url_con.getOutputStream().close();

InputStream in = url_con.getInputStream();

BufferedReader rd = new BufferedReader(

new InputStreamReader(in,

recvEncoding));

String tempLine = rd.readLine();

StringBuffer tempStr = new StringBuffer();

String crlf = System.getProperty("line.separator");

while (tempLine != null)

{

tempStr.append(tempLine);

tempStr.append(crlf);

tempLine = rd.readLine();

}

responseContent = tempStr.toString();

rd.close();

in.close();

}

catch (IOException localIOException)

{

}

finally

{

if (url_con != null)

{

url_con.disconnect();

}

}

return responseContent;

}

public static String sendTelCode(String mobile,String telcode){

MapString ,String map = new HashMapString ,String();

map.put("account", "Babo");

map.put("mobile", mobile);

map.put("pswd", "D3dddD");

try {

map.put("msg", java.net.URLEncoder.encode("您的驗證碼是"+telcode+",若非本人操作請忽略","utf-8"));

} catch (UnsupportedEncodingException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

String getValue = doPost("", map, "UTF-8");

System.out.println(getValue);

return getValue;

}

java如何實現(xiàn)發(fā)送短信驗證碼功能?

1、創(chuàng)建一個Http的模擬請求工具類,然后寫一個POST方法或者GET方法

/** * 文件說明 * @Description:擴展說明 * @Copyright: XXXX dreamtech.com.cn Inc. All right reserved * @Version: V6.0 */package com.demo.util; import java.io.IOException;import java.util.Map; import org.apache.commons.httpclient.HttpClient;import org.apache.commons.httpclient.HttpException;import org.apache.commons.httpclient.SimpleHttpConnectionManager;import org.apache.commons.httpclient.methods.GetMethod;import org.apache.commons.httpclient.methods.PostMethod; /** * @Author: feizi * @Date: XXXX年XX月XX日 XX:XX:XX * @ModifyUser: feizi * @ModifyDate: XXXX年XX月XX日 XX:XX:XX * @Version:V6.0 */public class HttpRequestUtil { /** * HttpClient 模擬POST請求 * 方法說明 * @Discription:擴展說明 * @param url * @param params * @return String * @Author: feizi * @Date: XXXX年XX月XX日 XX:XX:XX * @ModifyUser:feizi * @ModifyDate: XXXX年XX月XX日 XX:XX:XX */ public static String postRequest(String url, MapString, String params) { //構(gòu)造HttpClient的實例 HttpClient httpClient = new HttpClient(); //創(chuàng)建POST方法的實例 PostMethod postMethod = new PostMethod(url); //設置請求頭信息 postMethod.setRequestHeader("Connection", "close"); //添加參數(shù) for (Map.EntryString, String entry : params.entrySet()) { postMethod.addParameter(entry.getKey(), entry.getValue()); } //使用系統(tǒng)提供的默認的恢復策略,設置請求重試處理,用的是默認的重試處理:請求三次 httpClient.getParams().setBooleanParameter("http.protocol.expect-continue", false); //接收處理結(jié)果 String result = null; try { //執(zhí)行Http Post請求 httpClient.executeMethod(postMethod); //返回處理結(jié)果 result = postMethod.getResponseBodyAsString(); } catch (HttpException e) { // 發(fā)生致命的異常,可能是協(xié)議不對或者返回的內(nèi)容有問題 System.out.println("請檢查輸入的URL!"); e.printStackTrace(); } catch (IOException e) { // 發(fā)生網(wǎng)絡異常 System.out.println("發(fā)生網(wǎng)絡異常!"); e.printStackTrace(); } finally { //釋放鏈接 postMethod.releaseConnection(); //關閉HttpClient實例 if (httpClient != null) { ((SimpleHttpConnectionManager) httpClient.getHttpConnectionManager()).shutdown(); httpClient = null; } } return result; } /** * HttpClient 模擬GET請求 * 方法說明 * @Discription:擴展說明 * @param url * @param params * @return String * @Author: feizi * @Date: XXXX年XX月XX日 XX:XX:XX * @ModifyUser:feizi * @ModifyDate: XXXX年XX月XX日 XX:XX:XX */ public static String getRequest(String url, MapString, String params) { //構(gòu)造HttpClient實例 HttpClient client = new HttpClient(); //拼接參數(shù) String paramStr = ""; for (String key : params.keySet()) { paramStr = paramStr + "" + key + "=" + params.get(key); } paramStr = paramStr.substring(1); //創(chuàng)建GET方法的實例 GetMethod method = new GetMethod(url + "?" + paramStr); //接收返回結(jié)果 String result = null; try { //執(zhí)行HTTP GET方法請求 client.executeMethod(method); //返回處理結(jié)果 result = method.getResponseBodyAsString(); } catch (HttpException e) { // 發(fā)生致命的異常,可能是協(xié)議不對或者返回的內(nèi)容有問題 System.out.println("請檢查輸入的URL!"); e.printStackTrace(); } catch (IOException e) { // 發(fā)生網(wǎng)絡異常 System.out.println("發(fā)生網(wǎng)絡異常!"); e.printStackTrace(); } finally { //釋放鏈接 method.releaseConnection(); //關閉HttpClient實例 if (client != null) { ((SimpleHttpConnectionManager) client.getHttpConnectionManager()).shutdown(); client = null; } } return result; }}

2、在創(chuàng)建一個類,生成驗證碼,然后傳遞相應的參數(shù)(不同的短信平臺接口會有不同的參數(shù)要求,這個一般短信平臺提供的接口文檔中都會有的,直接看文檔然后按要求來即可)

/** * 文件說明 * @Description:擴展說明 * @Copyright: XXXX dreamtech.com.cn Inc. All right reserved * @Version: V6.0 */package com.demo.util; import java.net.URLEncoder;import java.util.HashMap;import java.util.Map; /** * @Author: feizi * @Date: XXXX年XX月XX日 XX:XX:XX * @ModifyUser: feizi * @ModifyDate: XXXX年XX月XX日 XX:XX:XX * @Version:V6.0 */public class SendMsgUtil { /** * 發(fā)送短信消息 * 方法說明 * @Discription:擴展說明 * @param phones * @param content * @return * @return String * @Author: feizi * @Date: 2015年4月17日 下午7:18:08 * @ModifyUser:feizi * @ModifyDate: 2015年4月17日 下午7:18:08 */ @SuppressWarnings("deprecation") public static String sendMsg(String phones,String content){ //短信接口URL提交地址 String url = "短信接口URL提交地址"; MapString, String params = new HashMapString, String(); params.put("zh", "用戶賬號"); params.put("mm", "用戶密碼"); params.put("dxlbid", "短信類別編號"); params.put("extno", "擴展編號"); //手機號碼,多個號碼使用英文逗號進行分割 params.put("hm", phones); //將短信內(nèi)容進行URLEncoder編碼 params.put("nr", URLEncoder.encode(content)); return HttpRequestUtil.getRequest(url, params); } /** * 隨機生成6位隨機驗證碼 * 方法說明 * @Discription:擴展說明 * @return * @return String * @Author: feizi * @Date: 2015年4月17日 下午7:19:02 * @ModifyUser:feizi * @ModifyDate: 2015年4月17日 下午7:19:02 */ public static String createRandomVcode(){ //驗證碼 String vcode = ""; for (int i = 0; i 6; i++) { vcode = vcode + (int)(Math.random() * 9); } return vcode; } /** * 測試 * 方法說明 * @Discription:擴展說明 * @param args * @return void * @Author: feizi * @Date: XXXX年XX月XX日 XX:XX:XX * @ModifyUser:feizi * @ModifyDate: XXXX年XX月XX日 XX:XX:XX */ public static void main(String[] args) {// System.out.println(SendMsgUtil.createRandomVcode());// System.out.println("ecb=12".substring(1)); System.out.println(sendMsg("18123456789,15123456789", "尊敬的用戶,您的驗證碼為" + SendMsgUtil.createRandomVcode() + ",有效期為60秒,如有疑慮請詳詢XXX-XXX-XXXX【XXX中心】")); }

然后執(zhí)行一下,一般的情況下參數(shù)傳遞正確,按照接口文檔的規(guī)范來操作的話,都會發(fā)送成功的,手機都能收到驗證碼的,然后可能會出現(xiàn)的問題就是:發(fā)送的短信內(nèi)容有可能會出現(xiàn)中文亂碼,然后就會發(fā)送不成功,按照短信平臺的要求進行相應的編碼即可。一般都會是UTF-8編碼。

用Java調(diào)用短信接口給手機發(fā)送短信怎么寫代碼??接口已經(jīng)有了,是一個url.

那你要做的就是向這個URL發(fā)送數(shù)據(jù)就好,給個例子:

public???class??Httptest1??{

8??

9???????public???static???void??main(String[]?args)??{

10??

11?????????URL?url??=???null?;

12?????????HttpURLConnection?conn??=???null?;

13?????????OutputStream?outStream??=???null?;

14?????????InputStream?inStream??=???null?;

15?????????

16???????????try???{

17?????????????url??=???new??URL(?"??"?);

18?????????????conn??=??(HttpURLConnection)?url.openConnection();

19?????????????conn.setDoOutput(?true?);

20?????????????conn.setDoInput(?true?);

21??

22?????????????String?sendXml??=???"??xml?version=\?"?1.0?\?"??encoding=\?"?UTF?-?16?\?"??!DOCTYPE?SigMailCommand?SYSTEM?\?"?SigMail.dtd\?"?"?;??//?XML數(shù)據(jù)?????

23??????????????sendXml??+=???"?SigMailCommand?Name=\?"?checkuser\?"?Param?Name=\?"?domainname\?"?test.com.cn/Param?"?;

24?????????????sendXml??+=???"?Param?Name=\?"?username\?"?admin/Param/SigMailCommand?"?;

25??

26?????????????outStream??=??conn.getOutputStream();

27??

28??????????????//?準備通過CONN對象寫入XML數(shù)據(jù)?

29??????????????BufferedWriter?bw??=???new??BufferedWriter(?new??java.io.OutputStreamWriter(outStream,

30??????????????"?UTF-16?"?));

31?????????????bw.write(sendXml);

32?????????????bw.flush();

33?????????????bw.close();

34?????????????

35??????????????//?DataOutputStream?dataOutStream?=?new?DataOutputStream(outStream);

36??????????????//?dataOutStream.writeChars(xml);

37??????????????//?dataOutStream.flush();

38??????????????//?dataOutStream.close();

39?????????????

40??

41??????????????//?準備通過CONN對象讀取返回的XML數(shù)據(jù)?

42??

43?????????????inStream??=??conn.getInputStream();

44?????????????StringBuffer?returnXml??=???new??StringBuffer(?""?);

45?????????????BufferedReader?rd??=???new??BufferedReader(?new??InputStreamReader(inStream,

46??????????????????????"?UTF-16?"?));

47???????????????for??(String?line??=???null?;?(line??=??rd.readLine())??!=???null?;)??{

48?????????????????returnXml.append(line);

49?????????????}?

50?????????????System.out.println(returnXml.toString());

51?????????????rd.close();

52??????????}???catch??(IOException?ex)??{

53?????????????ex.printStackTrace();

54??????????}?finally?{

55???????????????try???{

56??????????????????if??(outStream??!=???null?)

57?????????????????????outStream.close();

58??????????????????if??(inStream??!=???null?)

59?????????????????????inStream.close();

60??????????????????if??(conn??!=???null?)

61?????????????????????conn.disconnect();

62??????????????}???catch??(IOException?e)??{

63??????????????????//??TODO?自動生成?catch?塊?

64??????????????????e.printStackTrace();

65?????????????}?

66?????????}?

67??

68?????}?

69??

70?}

標題名稱:短信接口java代碼 Java接口代碼
網(wǎng)頁路徑:http://www.muchs.cn/article18/hhoodp.html

成都網(wǎng)站建設公司_創(chuàng)新互聯(lián),為您提供定制開發(fā)、品牌網(wǎng)站建設、云服務器做網(wǎng)站、企業(yè)網(wǎng)站制作、移動網(wǎng)站建設

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

營銷型網(wǎng)站建設