基于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());
}
}
相关文章推荐
- 使用CXF开发WebService程序的总结(四):基于bean的客户端和服务端代码的编写
- java调用基于SOAP的CXF 框架的WebService客户端和服务器段helloworld例子
- 使用CXF开发WebService程序的总结(五):基于Map数据类型处理的的客户端和服务端代码的编写
- 基于cxf框架的webservice服务端和客户端编写
- Webservice_23_SOAP的基于契约优先开发用户管理_实现客户端
- 创建基于JAX-WS的WebService的服务端及客户端
- CXF Spring开发WebService,基于SOAP和REST方式
- 基于Java的webservice创建与soap方式调用
- 使用axis编写客户端代码调用webservice的一些问题
- 使用axis编写WebService客户端(客户端自动更新)
- httpclient妙用一 httpclient作为客户端调用soap webservice(转)
- JAVA采用CXF框架开发基于JAX-WS(SOAP)规范的WebService服务器端
- Webservice_20_SOAP的基于契约优先头信息处理(隐式头信息)
- 第二篇:利用shell脚本执行webservice请求——基于soap
- 随便聊聊 SOA & SOAP & WebService 的一些东西,以及客户端开发的代码逻辑解析
- CXF写SOAP WebService服务端与客户端
- 基于Axis服务端的webservice客户端实现
- 基于CXF框架下的SOAP Webservice服务端接口开发
- PHP基于SOAP实现webservice
- webservice cxf -- 基于soap方式开发(一)