1. 程式人生 > >【Java分享】3分鐘接入簡訊驗證碼介面的全過程,只需3步。

【Java分享】3分鐘接入簡訊驗證碼介面的全過程,只需3步。

簡訊驗證碼是目前使用者身份驗證最有效的手段,具有便捷和唯一性,是識別使用者身份最快速的方式。其實接入簡訊驗證碼也不是很麻煩和費時間的事情,按照我說的做,只需要大約3分鐘、3個步驟就輕鬆完成。

  1. 首先去簡訊平臺申請賬號,獲取介面地址和介面文件以及返回值。這都是接入簡訊驗證碼前的準備。

2、以post方式提交 ,介面地址:http://106.veesing.com/webservice/sms.php?method=Submit

這個是平臺需要傳的引數

account  提交賬戶

password  提交賬戶密碼(可以明文密碼或使用32位MD5加密)

mobile  接收號碼,只能提交1個號碼

content  資訊內容,通常為67漢字以內,超過限制字數會被分拆,同時扣費會被累計,具體由平臺內部決定

 示例程式碼

public class sendsms {
	
	private static String Url = "http://121.199.16.178/webservice/sms.php?method=Submit";
	public static void main(String [] args) {
		HttpClient client = new HttpClient(); 
		PostMethod method = new PostMethod(Url); 
			
		//client.getParams().setContentCharset("GBK");		
		client.getParams().setContentCharset("UTF-8");
	method.setRequestHeader("ContentType","application/x-www-form-urlencoded;charset=UTF-8");
            //此驗證碼為隨機數
	    String content = new String("您的驗證碼是:7528。請不要把驗證碼洩露給其他人。"); 
	    
		NameValuePair[] data = {//提交簡訊
		new NameValuePair("account", "使用者名稱"), 
		new NameValuePair("password", "密碼"), //密碼可以使用明文密碼或使用32位MD5加密
			    //new 
		NameValuePair("password", util.StringUtil.MD5Encode("密碼")),
		new NameValuePair("mobile", "手機號碼"), 
		 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(code == "2"){
				System.out.println("簡訊提交成功");
			}
		} 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();
		}	
		

	}

3、這個是介面返回的值

code

int

返回值為2時,表示提交成功

smsid

string

僅當提交成功後,此欄位值才有意義(訊息ID)

msg

string

提交結果描述

就這樣,3步3分鐘就可以快速接入簡訊驗證碼,是不是很簡單。有什麼疑問可在留言區提出來。