java 根據介面傳送簡訊

來源:互聯網
上載者:User

我做的手機傳送簡訊,是根據別人提供的介面來做的,介面內容如下:

簡訊平台介面說明 1、 概述 1.1 介面描述

各個系統在傳送簡訊時需調用本介面。 1.2 介面協議和資料格式

簡訊平台介面採用HTTP傳輸協議,資料格式為XML,編碼為UTF-8。 1.3 介面地址

遠程服務調用介面地址:http://10.0.7.40:8080/smsHttpServlet.servlet。 2、 介面詳細說明 2.1 傳送簡訊請求的資料內容

名稱

欄位

資料類型

非空

備忘

業務編碼

syscode

varchar2 (10)

非空

請向資訊中心索取

部門編碼

depcode

varchar2 (50)

非空

填寫:00000000

部門名稱

depname

varchar2 (50)

非空

填寫:安華農業保險股份有限公司

密碼

password

varchar2 (50)

非空

請向資訊中心索取,需MD5加密

接收人手機號

mobile

varchar2 (11)

非空

以1開頭的11位手機號

簡訊內容

content

varchar2 (500)

非空

每個英文或漢字為1個單位,最多500

2.2 傳送簡訊請求XML格式範例

<?xml version="1.0" encoding="UTF-8"?>

<xmlRequest>

  <header>

<syscode>(業務編碼請向資訊中心索取)</syscode>

   <depcode>00000000</depcode>

<depname>安華農業保險股份有限公司</depname>

<password>(密碼請向資訊中心索取,請傳輸MD5加密後的密碼)</password>

  </header>

<body>

    <sms>

  <mobile>接收人手機號</mobile>

  <content>簡訊內容</content>

    </sms>

</body>

</xmlRequest> 2.3 擷取發送結果的返回資料內容

名稱

欄位

資料類型

非空

備忘

業務編碼

recode

varchar2 (10)

非空

成功:000,失敗:001

2.4 擷取發送結果返回XML格式範例

<?xml version="1.0" encoding="UTF-8"?>

<xmlResponse>

<header>

<recode>000</recode>

</header>

</xmlResponse> 3、 注意事項

(1)傳送簡訊請求中的業務編碼和密碼請向資訊中心索取,並注意妥善保管,切勿泄露;

(2)簡訊內容中禁止使用各類敏感詞語;

(3)簡訊內容中請不要使用“測試”二字,否則會出現介面返回傳送成功的結果,而該簡訊實際上並未發出的情況;

(4)傳送簡訊為收費業務,請在測試時注意節約使用。

二、我自己寫的代碼如下:

首先需要把commons-httpclient-3.0.jar包匯入到lib目錄下;

其次寫調用方法:

/**
  * 發送單個簡訊
  */
 public String sendMessage(){
  try {              
      StringBuffer bufxml = new StringBuffer();
      bufxml.append("<?xml version=");
      bufxml.append("\"1.0\"");
      bufxml.append(" encoding=");
      bufxml.append("\"UTF-8\"");
      bufxml.append("?><xmlRequest><header><syscode>");
      bufxml.append("業務編碼");
      bufxml.append("</syscode><depcode>");
      bufxml.append("00000000");
      bufxml.append("</depcode><depname>安華農業保險股份有限公司</depname><password>密碼</password></header><body><sms><mobile>");
      bufxml.append(手機號);
      bufxml.append("</mobile><content>");
      bufxml.append(簡訊內容);
      bufxml.append("</content></sms></body></xmlRequest>");
      
      PostMethod post = new PostMethod("http://10.0.7.40:8080/smsHttpServlet.servlet");//請求地址
      //這裡添加xml字串
      post.setRequestBody(bufxml.toString());
      
      // 指定請求內容的類型
      post.setRequestHeader("Content-type", "text/xml; charset=UTF-8");
      HttpClient httpclient = new HttpClient();//建立 HttpClient 的執行個體
      int result;
      try {
       result = httpclient.executeMethod(post);
       System.out.println("Response status code: " + result);//返回200為成功
       System.out.println("Response body: ");
       System.out.println(post.getResponseBodyAsString());//返回的內容
       String responseXml = post.getResponseBodyAsString();
       if(!"".equals(responseXml) && responseXml != null  ){
        if(responseXml.contains("000")){
         system.out.println("發送成功");

        }else if(responseXml.contains("001")){
         system.out.println("發送失敗");        }
       }
       post.releaseConnection();//釋放串連
      } catch (HttpException e) {
      system.out.println("發送失敗");        

           e.printStackTrace();
      } catch (IOException e) {
     system.out.println("發送失敗");        

       e.printStackTrace();
      }

  } catch (Exception e) {
   e.printStackTrace();
   this.message = this.getFailMessage("發送失敗");
  }
  return "json";
 }

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.