|
楼主 |
发表于 2020-3-5 15:45:01
|
显示全部楼层
搞定:
con.call(msg, myEndpoint);方法返回一个服务器响应的SOAPMessage,只要把它给一个SOAPMessage变量就可以了。
代码这样写:msg=con.call(msg, myEndpoint);
整个代码如下:
客户端:
import javax.xml.soap.*;
import javax.xml.messaging.*;
public class CustomSoap {
public static void myCustomSoap(){
try{
URLEndpoint myEndpoint = new URLEndpoint("http://127.0.0.1:8080/axis/HelloService.jws?wsdl");
SOAPConnectionFactory scf = SOAPConnectionFactory.newInstance();
SOAPConnection con = scf.createConnection();
MessageFactory mf = MessageFactory.newInstance();
SOAPMessage msg = mf.createMessage();
SOAPPart sp = msg.getSOAPPart();
SOAPEnvelope envelope = sp.getEnvelope();
SOAPBody bdy = envelope.getBody();
SOAPBodyElement gltp = bdy.addBodyElement(envelope.createName(
"sayHello", "soapenv",
"http://schemas.xmlsoap.org/soap/encoding/"));
gltp.addChildElement("zzm").addTextNode("The Frist MySoap");
con.call(msg, myEndpoint);
System.out.println("传送的Soap消息:");
msg.writeTo(System.out );
System.out.println("");
System.out.println("响应服务器的Soap消息:");
msg=con.call(msg, myEndpoint);
msg.writeTo(System.out);
}
catch(Exception e){
System.out.println(e.toString());}
}
public static void main(String[] args){
myCustomSoap();
}
}
打印在控制面板上的信息如下:
传送的Soap消息:
<?xml version="1.0" encoding="UTF-8"?>
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<soapenv:Body>
<soapenv:sayHello xmlns:soapenv="http://schemas.xmlsoap.org/soap/encoding/">
<soapenv:zzm>The Frist MySoap</soapenv:zzm>
</soapenv:sayHello>
</soapenv:Body>
</soapenv:Envelope>
响应服务器的Soap消息:
<?xml version="1.0" encoding="utf-8"?>
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:xsi="http://www.w3.org/2001/XMLSchemainstance">
<soapenv:Body>
<soapenc:sayHelloResponse soapenv:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/">
<sayHelloReturn xsi:type="xsd:string">Hello:The Frist MySoap</sayHelloReturn>
</soapenc:sayHelloResponse>
</soapenv:Body>
</soapenv:Envelope>
服务器上的服务:
public class HelloService
{
public String sayHello(String username)
{
return "Hello:"+username;
}
} |
|