public class DispatchClient public static void main String args try St

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
public class DispatchClient {
public static void main(String[] args) {
try {
String endpointUrl = "http://localhost:8080/axis2/
services/HelloService";
QName serviceName = new QName("http://apache.org/
types", "HelloService");
QName portName = new QName("http://apache.org/types",
"HelloServiceHttpSoap11Endpoint");
/** Create a service and add at least one port to it. **/
Service service = Service.create(serviceName);
service.addPort(portName, SOAPBinding.SOAP11HTTP_BINDING,
endpointUrl);
/** Create a Dispatch instance from a service.**/
Dispatch < SOAPMessage > dispatch =
service.createDispatch(portName, SOAPMessage.class,
Service.Mode.MESSAGE);
/** Create SOAPMessage request. **/
// compose a request message
MessageFactory mf =
MessageFactory.newInstance(SOAPConstants.SOAP_1_1_ PROTOCOL);
// Create a message. This example works with the
SOAPPART.
SOAPMessage request = mf.createMessage();
SOAPPart part = request.getSOAPPart();
// Obtain the SOAPEnvelope and header and body elements.
SOAPEnvelope env = part.getEnvelope();
SOAPHeader header = env.getHeader();
SOAPBody body = env.getBody();
// Construct the message payload.
SOAPElement operation = body.addChildElement("greet",
"ns2",
"http://apache.org/types");
SOAPElement age = operation.addChildElement("name");
age.addTextNode("Packt");
request.saveChanges();
/** Invoke the service endpoint. **/
SOAPMessage response = dispatch.invoke(request);
System.out.println(response.getSOAPBody().getFirstChild().toString());
} catch (Exception e) {
e.printStackTrace();
}
}
}