java实现手机发送信息(实例) xzhou


分类: 计算机 3025人阅读 评论(9) 收藏 举报

java 手机 sms string service variables

要实现手机发送信息功能首先要具备如下条件

1.有wsdl文件.这里有下载

http://d.download.csdn.net/down/2631194/zhouxiaoxiong828788

2.tomcat 与jdk是不可少的,版本一般分别在5.0与1.5以上

3.Eclipse Java EE IDE for Web Developers

现在开始创建项目:

1.新建动态web项目名:sms

2.在sms目录上右击新建文件夹 wsdl

3.在wsdl文件夹上右击——>import——>General——>file system

4.将刚刚下载的 wsdl文件夹中的

ctcc_sms_types_2_1.xsd
ctcc_sms_send_service_2_1.wsdl
ctcc_sms_send_interface_2_1.wsdl
ctcc_sms_receive_service_2_1.wsdl
ctcc_sms_receive_interface_2_1.wsdl
ctcc_sms_notification_service_2_1.wsdl
ctcc_sms_notification_interface_2_1.wsdl
ctcc_common_types_2_1.xsd
ctcc_common_faults_2_0.wsdl
IsmpSpEngine.wsdl

文件选中并导入。

5.导入成功后,再右击ctcc_sms_send_service_2_1.wsdl——>web Services——>Generate Client

——Next

接下来就是编写类了

新建类名:SendSmsTest.java

package test;

import java.math.BigDecimal;
import java.net.MalformedURLException;
import java.rmi.RemoteException;
import java.text.SimpleDateFormat;
import java.util.Date;
import javax.xml.rpc.ServiceException;
import javax.xml.soap.SOAPException;
import org.apache.axis.message.SOAPHeaderElement;
import org.apache.axis.types.URI.MalformedURIException;
//import cn.com.chinatelecom.util.MD5;
import cn.com.chinatelecom.www.schema.ctcc.common.v2_1.*;
import cn.com.chinatelecom.www.schema.ctcc.sms.v2_1.*;
import cn.com.chinatelecom.www.wsdl.ctcc.sms.send.v2_1._interface.*;
import cn.com.chinatelecom.www.wsdl.ctcc.sms.send.v2_1.service.*;

public class SendSmsTest {

 /**
  * @param args
  */
 public static void main(String[] args) {
  // TODO Auto-generated method stub
  String webserviceurl = "http://localhost:9080/SendSmsService/services/SendSms";// isag地址
  String SPID = "0";// spid
  String Token = "xzhou";// 密钥
  String DestNum = "tel:+86134********";// 发送号码
  String ProductID = ""; // 产品编号
  String ServiceID = ""; // 业务编号
  String TimeStamp = dateString(); // 当前时间
  String senderName = ""; // 短信主叫号码,在浙江ISAG实际无效,ISAG会读 取业务部署时候的接入号

  String message = "测试短信"; // 短信内容
  SOAPHeaderElement SoapHeader = new SOAPHeaderElement(
    "http://www.chinatelecom.com.cn/schema/ctcc/common/v2_1",
    "RequestSOAPHeader");
  try {
   /* 初始化Web Service Client */
   SendSmsServiceLocator ssl = new SendSmsServiceLocator();
   SendSms sendSms = ssl.getSendSms(new java.net.URL(webserviceurl));
   /* 设置SOAP Header */
   SoapHeader.addChildElement("spId").addTextNode(SPID); // SpID
   SoapHeader.addChildElement("timeStamp").addTextNode(TimeStamp);
   // SoapHeader.addChildElement("spPassword").addTextNode(MD5.compile(SPID
   // + Token + TimeStamp).toUpperCase());// MD5加密
   SoapHeader.addChildElement("productId").addTextNode(ProductID);
   SoapHeader.addChildElement("OA").addTextNode(DestNum);
   SoapHeader.addChildElement("FA").addTextNode(DestNum);
   SoapHeader.addChildElement("multicastMessaging").addTextNode(
     "false"); // 是否群发
   ((SendSmsBindingStub) sendSms).setHeader(SoapHeader); // 添加SOAP头
   /* 设置被叫号码 */
   org.apache.axis.types.URI[] addresses = new org.apache.axis.types.URI[1];
   addresses[0] = new org.apache.axis.types.URI(DestNum);
   /* 设置ChargingInformation */
   ChargingInformation charging = new ChargingInformation();
   charging.setDescription("gm");// 描述
   charging.setAmount(new BigDecimal(1));// 扣费数目
   charging.setCode(ServiceID);// 业务代码
   charging.setCurrency("0");
   /* 设置SimpleReference */
   SimpleReference receiptRequest = new SimpleReference();
   receiptRequest.setCorrelator("1001559"); // 序号自己随机增加
   receiptRequest.setInterfaceName("SmsNotificationService");
   receiptRequest.setEndpoint(new org.apache.axis.types.URI(
     "http://localhost:9080/SendSmsService/services/SendSms"));
   /* 发送短信 */

   String result = sendSms.sendSms(addresses, senderName, charging,
     message, receiptRequest);
   System.out.println("result:" + result);
  } catch (ServiceException e) {
   // 鉴权失败
  } catch (PolicyException e) {
   System.out.println("MessageId:" + e.getMessageId());
   System.out.println("Text:" + e.getText());
   String[] variables = e.getVariables();
   for (int i = 0; i < variables.length; i++) {
    System.out.println("Variable:" + variables[i]);
   }
  } catch (MalformedURLException e) {
   e.printStackTrace();
  } catch (SOAPException e) {
   e.printStackTrace();
  } catch (MalformedURIException e) {
   e.printStackTrace();
  } catch (RemoteException e) {
   e.printStackTrace();
  }
 }

 private static String dateString() {
  SimpleDateFormat sdf = new SimpleDateFormat("MMddHHmmss");
  return sdf.format(new Date());
 }
}


你可能感兴趣的:(JAVA)