当前位置: 代码迷 >> Web前端 >> WebService客户端调用不写javabean直接用String的步骤
  详细解决方案

WebService客户端调用不写javabean直接用String的步骤

热度:108   发布时间:2012-09-25 09:55:59.0
WebService客户端调用不写javabean直接用String的方法
1、Axis1.4 传输用SOAPBodyElement解析用Vector
参考url:http://www.blogjava.net/robin/archive/2006/01/02/26385.html

Service service = new Service();   
Call call = new Call(service);
String endpoint = "http://127.0.0.1:80/XXXX/services/HelloService";
call.setTargetEndpointAddress(new java.net.URL(endpoint));
String namespace = "http://xxx.xxxx.com/webservice";
String method = "methodA";
call.setSOAPActionURI(method);
call.setOperationName(method);
call.setProperty(Call.OPERATION_STYLE_PROPERTY, Style.DOCUMENT.getName());
call.setPortName(new QName(namespace, "HelloServicePort"));
call.setPortTypeName(new QName(namespace, "HelloServicePortType"));
InputStream in = new ByteArrayInputStream(aaa.getBytes());
Object[] obj = new Object[] { new SOAPBodyElement(in) };
Vector result = (Vector)call.invoke(obj);
SOAPBodyElement element = (SOAPBodyElement) result.get(0);



2、Axis2.0 传输解析都是OMElement

参考自axis2官网样例quickstartaxiom

String reqXml="<?xml version=\"1.0\" encoding=\"UTF-8\"?>XXXXX";
String xmlNamespace = "http://xxx.xxxx.com/webservice";
String xmlMethod = "methodA";
String xmlHead = "methodARequest";
String intUrl = "http://127.0.0.1:80/xxxxxx/services/HelloService";
String encoding = "UTF-8";    	
EndpointReference targetEPR = new EndpointReference(intUrl);
OMFactory fac = OMAbstractFactory.getOMFactory();
//前缀prefix必须设置空串
OMNamespace ns = fac.createOMNamespace(xmlNamespace, "");
//请求对象
OMElement requestElement = fac.createOMElement(xmlHead, ns);
//报文
OMElement value = toOMElement(reqXml, encoding);
requestElement.addChild(value);
    	
Options options = new Options();
ServiceClient client = new ServiceClient();
//options.setTimeOutInMilliSeconds(60000L);
options.setTo(targetEPR);
options.setAction(xmlMethod);
client.setOptions(options);
        
//调用服务端
OMElement result = client.sendReceive(requestElement);
//这个result带命名空间,但不带xml声明
System.out.println("result : " + result.toString());

public static OMElement toOMElement(String xmlStr, String encoding) {
OMElement xmlValue;
try {
    xmlValue = new StAXOMBuilder(new ByteArrayInputStream(xmlStr
    .getBytes(encoding))).getDocumentElement();
    return xmlValue;
}catch (Exception e) {
    return null;
}
}
  相关解决方案