Java调用WebService接口

短信调用wsdl地址:

http://127.0.0.1:8080/STEInterface/STEservice?wsdl

在浏览器中打开获取的相关信息如下:

<wsdl:definitions xmlns:ns1="http://schemas.xmlsoap.org/soap/http" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:tns="http://service.hoo.com/" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" name="ISTEServiceService" targetNamespace="http://service.hoo.com/">
<wsdl:message name="SMSTestResponse">
<wsdl:part name="return" type="xsd:string"></wsdl:part>
</wsdl:message>
<wsdl:message name="OrderTest">
<wsdl:part name="xml" type="xsd:string"></wsdl:part>
</wsdl:message>
<wsdl:message name="SMSTest">
<wsdl:part name="xml" type="xsd:string"></wsdl:part>
</wsdl:message>
<wsdl:message name="OrderTestResponse">
<wsdl:part name="return" type="xsd:string"></wsdl:part>
</wsdl:message>
<wsdl:portType name="ISTEService">
<wsdl:operation name="OrderTest">
<wsdl:input message="tns:OrderTest" name="OrderTest"></wsdl:input>
<wsdl:output message="tns:OrderTestResponse" name="OrderTestResponse"></wsdl:output>
</wsdl:operation>
<wsdl:operation name="SMSTest">
<wsdl:input message="tns:SMSTest" name="SMSTest"></wsdl:input>
<wsdl:output message="tns:SMSTestResponse" name="SMSTestResponse"></wsdl:output>
</wsdl:operation>
</wsdl:portType>
<wsdl:binding name="ISTEServiceServiceSoapBinding" type="tns:ISTEService">
<soap:binding style="rpc" transport="http://schemas.xmlsoap.org/soap/http"/>
<wsdl:operation name="OrderTest">
<soap:operation soapAction="" style="rpc"/>
<wsdl:input name="OrderTest">
<soap:body namespace="http://service.hoo.com/" use="literal"/>
</wsdl:input>
<wsdl:output name="OrderTestResponse">
<soap:body namespace="http://service.hoo.com/" use="literal"/>
</wsdl:output>
</wsdl:operation>
<wsdl:operation name="SMSTest">
<soap:operation soapAction="" style="rpc"/>
<wsdl:input name="SMSTest">
<soap:body namespace="http://service.hoo.com/" use="literal"/>
</wsdl:input>
<wsdl:output name="SMSTestResponse">
<soap:body namespace="http://service.hoo.com/" use="literal"/>
</wsdl:output>
</wsdl:operation>
</wsdl:binding>
<wsdl:service name="ISTEServiceService">
<wsdl:port binding="tns:ISTEServiceServiceSoapBinding" name="ISTEServicePort">
<soap:address location="http://127.0.0.1:8080/STEInterface/STEservice"/>
</wsdl:port>
</wsdl:service>
</wsdl:definitions>

 

从这个wsdl文件中,我们只能看到返回的方法是 SMSTest 返回的参数是 String xml

Java的调用代码方式:

package com.tydic.web.cust.common;

import java.io.InputStream;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Properties;

import javax.xml.namespace.QName;
import javax.xml.rpc.ParameterMode;
import javax.xml.rpc.encoding.XMLType;

import org.apache.axis.client.Call;
import org.apache.axis.client.Service;
import org.apache.log4j.Logger;
import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;

import com.tydic.base.util.IMapEntry;
import com.tydic.base.util.Maps;
/**
 * 短信发送接口
 * @author liure
 *
 */
public class SMSSendUtil {
	 private static final Logger LOGGER = Logger.getLogger(MsgSendUtil.class);
	 private static Properties properties = new Properties();
     private static String propertyName = "mail_msg.properties";
	 public final static String FILE_SEP = System.getProperty("file.separator");//文件分隔符(在 UNIX 系统中是“/”)
	 private static String wsdlUrl;
	 private static String wsdlQName;
	 private static String wsdlMethod;
	 
	 static {
	        loadConfigProperties();
	    }
	 /**
	  * @Title: getPropertyPath  
	  * @Description: 获取配置文件
	  * @param @return    设定文件  
	  * @return String    返回类型  
	  * @throws
	  */
	 public static String getPropertyPath(){
			String path = System.getProperty("user.dir")+FILE_SEP+"config"+FILE_SEP+propertyName;
			LOGGER.info(path);
			return propertyName;
		}
	 /**
	     * Load configuration properties to initialize attributes.
	     * @throws Exception 
	     */
	    private static void loadConfigProperties() {
	    	
	    	
			try {
				Properties prop = new Properties();    
				InputStream inStream =  MsgSendUtil.class.getClassLoader().getResourceAsStream(getPropertyPath());//包内配置文件
				prop.load(inStream);  
				wsdlUrl = prop.getProperty("WSDLURL");
				wsdlQName = prop.getProperty("QNAME");
				wsdlMethod = prop.getProperty("METHOD");
			} catch (Exception e) {
				e.printStackTrace();
			}
	    	
	    }
	public static void main(String[] args) {
		String format_time = getTime();
		System.out.println(format_time);
		String SMS_ID = "123456";
		String worksheet_person = "张三"; // 短信发送方信息
		String sms_info = "欠费了";//短信内容
		List<String> phoneNumList = new ArrayList<String>();
		phoneNumList.add("1878xxxxxxx");
		phoneNumList.add("1868xxxxxxx");
		for (String mdn : phoneNumList) {
			String xmlString = NamespaceURI_R(SMS_ID, mdn, format_time, worksheet_person, sms_info);
			sendSMS(xmlString);
		}

	}

	/**
	 * 发送短信的wsdl接口
	 * 
	 * @param xmlString
	 */
	public static IMapEntry<String,Object> sendSMS(String xmlString) {
		IMapEntry<String,Object> resultMap = Maps.newMapEntry();
		try {
			String msg = "";
			Map<String,Object> map_read = new HashMap<String, Object>();
			Service service = new Service();
			Call call = (Call) service.createCall();
			call.setTargetEndpointAddress(wsdlUrl);
			// WSDL里面描述的接口名称(要调用的方法)
			call.setOperationName(new QName(wsdlQName, wsdlMethod));
			call.addParameter("xml", XMLType.XSD_STRING, ParameterMode.IN);
			// 封装参数
			// 设置被调用方法的返回值类型
			call.setReturnType(XMLType.XSD_STRING);
			// 设置方法中参数的值
			Object[] paramValues = new Object[] { xmlString };
			// 给方法传递参数,并且调用方法
			String result = (String) call.invoke(paramValues);
			resultMap = dom4jXML(result);
			if (!resultMap.isEmpty()) {
				Iterator<String> iters = resultMap.keySet().iterator();
				while (iters.hasNext()) {
					String key = iters.next().toString(); // 拿到键
					String val = resultMap.get(key).toString(); // 拿到值
					System.out.println(key + "=" + val);
					if (key.equals("RESP_TYPE")) {
						String value = resultMap.get("RESP_TYPE").toString();
						if (value.equals("1")) {
							System.out.println("++++++++++++++++++++++++++++++++短信发送成功");
							LOGGER.info("++++++++++++++++++++++++++++++++短信发送成功");
						}else {
							String errorInfo = resultMap.get("ERROR_DESC").toString();
							String sms_id = resultMap.get("SMS_ID").toString();
							String resp_type = resultMap.get("RESP_TYPE").toString();
							System.out.println("发送失败者的ID:"+sms_id+"发送结果状态:"+resp_type+"发送失败的描述:"+errorInfo);
							LOGGER.info("++++++++++++++++++++++++++++++++短信发送成功"+"发送失败者的ID:"+sms_id+"发送结果状态:"+resp_type+"发送失败的描述:"+errorInfo);
						}
					}
				}
				return resultMap;
			} else {
				System.out.println("map为空值");
				LOGGER.info("++++++++++++++++++++++++++++++++map为空值");
				return null;
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return resultMap;
	}

	/**
	 * dom4j解析短信发送返回的xml信息
	 * 
	 * @param xml
	 * @return
	 * @throws Exception
	 */
	@SuppressWarnings("unchecked")
	private static IMapEntry<String, Object> dom4jXML(String xml) throws Exception {
		IMapEntry<String, Object> map = Maps.newMapEntry();
		Document doc = DocumentHelper.parseText(xml);
		Element root = doc.getRootElement();
		List<Element> childElements = root.elements();
		for (Element child : childElements) {
			List<Element> elementList = child.elements();
			if (elementList.isEmpty()) {
				System.out.println(child.getName() + ": " + child.getText());
				map.put(child.getName(), child.getText());
			} else {
				for (Element ele : elementList) {
					map.put(child.getName(), child.getText());
					System.out.println(ele.getName() + ": " + ele.getText());
				}
			}

		}
		return map;
	}

	/**
	 * 初始化时间格式
	 * 
	 * @return
	 */
	private static String getTime() {
		Date date = new Date();
		SimpleDateFormat sft = new SimpleDateFormat("yyyyMMddHHmmss");
		String time = sft.format(date);
		return time;
	}

	/**
	 * 封装短信发送的xml报文
	 * 
	 * @param SMS_ID
	 *            发送短信的ID;数据类型:String;长度:100
	 * @param MDN
	 *            接收短信的手机号码;数据类型:String;长度:11
	 * @param time
	 *            短信派发时间,格式为:yyyyMMddHHmmss;数据类型:String;长度:14
	 * @param phoneNum
	 * @param msg
	 * @return
	 */
	public static String NamespaceURI_R(String SMS_ID, String mdn, String time, String worksheet_person,
			String sms_info) {

		StringBuffer buffer = new StringBuffer();

		buffer.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
		buffer.append("<SMSReq>");
		buffer.append("<SMS_ID>");
		buffer.append(SMS_ID);
		buffer.append("</SMS_ID>");
		buffer.append("<MDN>");
		buffer.append(mdn);
		buffer.append("</MDN>");
		buffer.append("<ASSIGN_TIME>");
		buffer.append(time);
		buffer.append("</ASSIGN_TIME>");
		buffer.append("<WORKSHEET_PERSON>");
		buffer.append(worksheet_person);
		buffer.append("</WORKSHEET_PERSON>");
		buffer.append("<SMS_INFO>");
		buffer.append(sms_info);
		buffer.append("</SMS_INFO>");
		buffer.append("</SMSReq>");
		return buffer.toString();
	}

}

然后就是发送的报文,具体发送报文可根据提供的文档自己拼装

转载于:https://my.oschina.net/u/1399599/blog/914880

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值