您的位置:首页 > 其它

基于SOAP编写WebService客户端(一)

2014-04-25 19:23 316 查看
本DEMO的

URLEndpoint
引入了AXIS2的axis-jaxrpc-1.4.jar,但未成功,需要反思。

package com.hikvision.cms.webservcie.client.util;

import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;

import javax.xml.messaging.URLEndpoint;
import javax.xml.soap.MessageFactory;
import javax.xml.soap.SOAPConnection;
import javax.xml.soap.SOAPConnectionFactory;
import javax.xml.soap.SOAPConstants;
import javax.xml.soap.SOAPException;
import javax.xml.soap.SOAPMessage;

import org.apache.log4j.Logger;

/**
* SOAP底层方式实现与Web Service通讯。
*
* @author Alex Woo
*
*/
public class DynamicSoapCall {

private static final Logger log = Logger.getLogger(DynamicSoapCall.class);

private String wsdlURI = null;

public DynamicSoapCall(String wsdlURI) throws Exception {
log.info("Reading WSDL document from '" + wsdlURI + "'");

this.wsdlURI = wsdlURI;
}

public SOAPMessage soapCall(String xmlString, String charsetName, String soapVersion) throws Exception {
if (null == xmlString)
xmlString = "";

byte[] bytes = null;
if (null == charsetName || charsetName.trim().equals("")) {
bytes = xmlString.getBytes();
} else {
try {
bytes = xmlString.getBytes(charsetName);
} catch (UnsupportedEncodingException e) {
log.warn("不支持指定的字符集,将按照操作系统默认字符集转换。可能会导致发送数据(特别是中文字符等数据)乱码。");
bytes = xmlString.getBytes();
}
}

InputStream is = new ByteArrayInputStream(bytes);

return soapCall(is, soapVersion);
}

public SOAPMessage soapCall(InputStream is, String soapVersion) throws Exception {
MessageFactory mf = null;
if (null != soapVersion && soapVersion.trim().equals("1.2")) {
mf = MessageFactory.newInstance(SOAPConstants.SOAP_1_2_PROTOCOL);
} else {// SOAP 1.1 is default.
mf = MessageFactory.newInstance();
}
SOAPMessage message = null;

try {
message = mf.createMessage(null, is);
} catch (SOAPException e) {
throw new Exception("根据Request XML数据创建的SOAP Message消息无效。错误原因:" + e.getCause());
}

SOAPConnectionFactory soapFactory = SOAPConnectionFactory.newInstance();
SOAPConnection con = soapFactory.createConnection();

URLEndpointendpoint = new URLEndpoint(wsdlURI);
SOAPMessage response = con.call(message, endpoint);

return response;
}

private static String myWsdl = "http://www.wll.com/ws/test?wsdl";

public static void main(String[] args) throws Exception {
File xml = new File("D:/getTokenInfo.xml");
DynamicSoapCall call = new DynamicSoapCall(myWsdl);
SOAPMessage result = call.soapCall(new FileInputStream(xml), null);
System.out.println(result.getSOAPHeader());
System.out.println(result.getSOAPBody());
}

public static void main1(String[] args) throws Exception {
String xmlString = "<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:web=\"http://webservice.ws.filemgr.hikvision.com/\">"
+ "<soapenv:Header/>"
+ "<soapenv:Body>"
+ "<web:getTokenInfo>"
+ "<username>administrator</username>"
+ "<password>123456</password>" + "</web:getTokenInfo>" + "</soapenv:Body>" + "</soapenv:Envelope>";

DynamicSoapCall call = new DynamicSoapCall(myWsdl);
SOAPMessage result = call.soapCall(xmlString, "utf-8", null);
System.out.println(result.getSOAPHeader());
System.out.println(result.getSOAPBody());
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: