SmsSendUtils 短信發送接口調用



/**
 * 短信接口調用
 */
package com.daoyou91.common.util;

import java.io.IOException;
import java.io.StringReader;
import java.net.URI;
import java.net.URISyntaxException;

import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpException;
import org.apache.commons.httpclient.HttpMethod;
import org.apache.commons.httpclient.NameValuePair;
import org.apache.commons.httpclient.methods.PostMethod;
import org.jdom.Document;
import org.jdom.Element;
import org.jdom.JDOMException;
import org.jdom.input.SAXBuilder;
import org.xml.sax.InputSource;

/**
 * 短信發送工具類
 *
 * @author zhangxujun
 *
 *
 */
public class SmsSendUtils {

 // Inner class for UTF-8 support
 public static class UTF8PostMethod extends PostMethod {
  public UTF8PostMethod(String url) {
   super(url);
  }

  @Override
  public String getRequestCharSet() {
   // return super.getRequestCharSet();
   return "utf-8";
  }
 }

 /**
  * 發送短信
  *
  * @param mobile
  *   手機號碼
  * @param content
  *   發送內容
  * @return 發送狀態
  * @throws IOException
  * @throws HttpException
  * @throws URISyntaxException
  */
 public static SmsSendStatus sendSms(String mobile, String content)
   throws HttpException, IOException, URISyntaxException {

  String url = PropertitiesManagement.getResouceString("sms_url");
  URI uri = new URI(url);
  // post請求 222.76.210.200
  HttpClient client = new HttpClient();

  client.getHostConfiguration().setHost(uri.getHost(), uri.getPort(),
    "http");
  HttpMethod method = getPostMethod(mobile, content, uri.getPath());
  client.executeMethod(method);
  // 打印服務器返回狀態
  System.out.println(method.getStatusLine());
  // 打印結果頁面
  String response = new String(method.getResponseBodyAsString().getBytes(
    "utf-8"));
  System.out.println(response);
  method.releaseConnection();
  client.endSession();
  return (xmlElements(response));

 }

 /**
  * 使用 POST 方式提交數據
  *
  * @return
  * @throws IOException
  */
 private static HttpMethod getPostMethod(String mobile, String content,
   String s) throws IOException {
  UTF8PostMethod post = new UTF8PostMethod(s);
  // 填入各個表單域的值
  NameValuePair action = new NameValuePair("action", "send");
  NameValuePair userid = new NameValuePair("userid",
    PropertitiesManagement.getResouceString("sms_userid"));
  NameValuePair account = new NameValuePair("account",
    PropertitiesManagement.getResouceString("sms_account"));
  NameValuePair password = new NameValuePair("password",
    PropertitiesManagement.getResouceString("sms_password"));
  NameValuePair mobiles = new NameValuePair("mobile", mobile);
  NameValuePair contents = new NameValuePair("content", content);
  NameValuePair[] data = { action, userid, account, password, mobiles,
    contents };
  post.setRequestBody(data);
  return post;
 }

 /**
  * 解析返回的結果
  *
  * @param args
  * @throws HttpException
  * @throws IOException
  */
 public static SmsSendStatus xmlElements(String xmlDoc) {
  // 創建一個新的字符串
  StringReader read = new StringReader(xmlDoc);
  // 創建新的輸入源SAX 解析器將使用 InputSource 對象來確定如何讀取 XML 輸入
  InputSource source = new InputSource(read);
  // 創建一個新的SAXBuilder
  SAXBuilder sb = new SAXBuilder();
  SmsSendStatus smsSendStatus = new SmsSendStatus();
  try {
   // 通過輸入源構造一個Document
   Document doc = sb.build(source);
   // 取的根元素
   Element root = doc.getRootElement();

   // System.out.println(root.getName());//輸出根元素的名稱(測試)
   if (root.getChild("returnstatus").getText().equals("Faild")) {
    smsSendStatus.setSuccess(false);
   } else {
    smsSendStatus.setSuccess(true);
   }
   System.out.println(root.getChild("returnstatus").getText());
   System.out.println(root.getChild("message").getText());
   smsSendStatus.setMessage(root.getChild("message").getText());
   ;

  } catch (JDOMException e) {
   e.printStackTrace();
  } catch (IOException e) {
   e.printStackTrace();
  }

  return smsSendStatus;
 }

 /**
  * 創建指定數量的隨機字符串
  *
  * @param numberFlag
  *            是否是數字
  * @param length
  * @return
  */
 public static String createRandom(boolean numberFlag, int length) {
  String retStr = "";
  String strTable = numberFlag ? "1234567890"
    : "1234567890abcdefghijkmnpqrstuvwxyz";
  int len = strTable.length();
  boolean bDone = true;
  do {
   retStr = "";
   int count = 0;
   for (int i = 0; i < length; i++) {
    double dblR = Math.random() * len;
    int intR = (int) Math.floor(dblR);
    char c = strTable.charAt(intR);
    if (('0' <= c) && (c <= '9')) {
     count++;
    }
    retStr += strTable.charAt(intR);
   }
   if (count >= 2) {
    bDone = false;
   }
  } while (bDone);

  return retStr;
 }

 public static void main(String args[]) throws HttpException, IOException,
   URISyntaxException {
  // 中國移動
   long startTime = System.currentTimeMillis(); // 獲取開始時間
   String content = "感謝你使用遊必應手機認證功能,你的驗證碼是:";
   sendSms("13880745410", content+createRandom(true,4));
   long endTime = System.currentTimeMillis(); // 獲取結束時間
   System.out.println("電信所用時間: " + (endTime - startTime) + "ms");

  // 中國聯通
  long startTime1 = System.currentTimeMillis(); // 獲取開始時間
  sendSms("18602891236", content + createRandom(true, 4));
  long endTime1 = System.currentTimeMillis(); // 獲取結束時間
  System.out.println("聯通所用時間: " + (endTime1 - startTime1) + "ms");
  // // 中國電信
  long startTime2 = System.currentTimeMillis(); // 獲取開始時間
  sendSms("18980436853", content + createRandom(true, 4));
  long endTime2 = System.currentTimeMillis(); // 獲取結束時間
  System.out.println("電信所用時間: " + (endTime2 - startTime2) + "ms");
 }

}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章