短信接口
阿新 • • 發佈:2017-10-08
短信接口 提交 參考 arr pass end sms ioe orm
第一步.首先我們得先找一個第三方的運營商.鄙人使用的是互億的 (原因是該運營商註冊就提供了50條免費的短信 .)
第二步.因為我使用的是互億的,所以我有寫好的接口 可以直接使用,
那麽廢話不多說直接上接口
package com.bitboss.util.sendsms; import java.io.IOException; import org.apache.commons.httpclient.HttpClient; import org.apache.commons.httpclient.HttpException; import org.apache.commons.httpclient.NameValuePair;import org.apache.commons.httpclient.methods.PostMethod; import org.dom4j.Document; import org.dom4j.DocumentException; import org.dom4j.DocumentHelper; import org.dom4j.Element; public class SendSms { private static String Url = "http://106.ihuyi.cn/webservice/sms.php?method=Submit"; publicstatic int sendsms(String phone){ HttpClient client = new HttpClient(); PostMethod method = new PostMethod(Url); client.getParams().setContentCharset("GBK"); method.setRequestHeader("ContentType","application/x-www-form-urlencoded;charset=GBK"); int mobile_code = (int)((Math.random()*9+1)*100000); String content = new String("您的驗證碼是:" + mobile_code + "。請不要把驗證碼泄露給其他人。"); NameValuePair[] data = {//提交短信 new NameValuePair("account", "******"), //查看用戶名是登錄用戶中心->驗證碼短信->產品總覽->APIID new NameValuePair("password", "*********"), //查看密碼請登錄用戶中心->驗證碼短信->產品總覽->APIKEY //new NameValuePair("password", util.StringUtil.MD5Encode("密碼")), new NameValuePair("mobile", phone), new NameValuePair("content", content), }; method.setRequestBody(data); try { client.executeMethod(method); String SubmitResult =method.getResponseBodyAsString(); //System.out.println(SubmitResult); Document doc = DocumentHelper.parseText(SubmitResult); Element root = doc.getRootElement(); String code = root.elementText("code"); String msg = root.elementText("msg"); String smsid = root.elementText("smsid"); System.out.println(code); System.out.println(msg); System.out.println(smsid); if("2".equals(code)){ System.out.println("短信提交成功"); return mobile_code; } } catch (HttpException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (DocumentException e) { // TODO Auto-generated catch block e.printStackTrace(); } return 0; } }
//接口類型:互億無線觸發短信接口,支持發送驗證碼短信、訂單通知短信等。 // 賬戶註冊:請通過該地址開通賬戶http://sms.ihuyi.com/register.html // 註意事項: //(1)調試期間,請用默認的模板進行測試,默認模板詳見接口文檔; //(2)請使用APIID(查看APIID請登錄用戶中心->驗證碼短信->產品總覽->APIID)及 APIkey來調用接口; //(3)該代碼僅供接入互億無線短信接口參考使用,客戶可根據實際需要自行編寫; package com.bitboss.util.sendsms; import java.security.MessageDigest; public class StringUtil { public static String str; public static final String EMPTY_STRING = ""; private final static String[] hexDigits = { "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "A", "B", "C", "D", "E", "F" }; private static String byteToHexString(byte b) { int n = b; if (n < 0) n = 256 + n; int d1 = n / 16; int d2 = n % 16; return hexDigits[d1] + hexDigits[d2]; } /** * 轉換字節數組為16進制字串 * @param b 字節數組 * @return 16進制字串 */ public static String byteArrayToHexString(byte[] b) { StringBuffer resultSb = new StringBuffer(); for (int i = 0; i < b.length; i++) { resultSb.append(byteToHexString(b[i])); } return resultSb.toString(); } public static String MD5Encode(String origin) { String resultString = null; try { resultString = new String(origin); MessageDigest md = MessageDigest.getInstance("MD5"); resultString = byteArrayToHexString(md.digest(resultString .getBytes())); } catch (Exception ex) { } return resultString; } }
PS:新手一定要註意添加jar包,jar何處來呢? 這個就直接在互億下載就好.把jar放在項目裏面 然後把代碼復制,更改
APIID 和
APIKEY 即可
因為我是封裝好的 .返回了一個短信的驗證碼.直接使用就好了 .免去了很多步驟.希望能幫到各位
短信接口