上面一章已经创建了服务器端的实现,下面是客户端的实现
客户端代码:(在另外一个eclipse中创建)
package Test.ws.payload;
import java.io.BufferedReader;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.StringReader;
import java.net.URL;
import javax.xml.namespace.QName;
import javax.xml.soap.MessageFactory;
import javax.xml.soap.SOAPMessage;
import javax.xml.soap.SOAPPart;
import javax.xml.transform.Source;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.stream.StreamResult;
import javax.xml.transform.stream.StreamSource;
import javax.xml.ws.Dispatch;
import javax.xml.ws.Service;
public class WSClient {
private static String wsdlurl = "http://127.0.0.1:8888/helloServer?wsdl"; // wsdl地址
private static String targetnamespace = "http://service.test/"; //对应wsdl中的targetNameSpace
private static String serviceStr = "HelloService"; //对应wsdl中的<wsdl:service name="HelloService">
private static String portStr = "HelloPort"; //对应wsdl中的<wsdl:service name="HelloService"> - <wsdl:port binding="tns:HelloServiceSoapBinding" name="HelloPort">
public static void main(String[] args) throws Exception {
WSClient client=new WSClient();
client.test();//测试PAYLOAD模式,不使用userNameToken身份验证机制
//client.testUserNameToken();//测试MESSAGE模式,使用userNameToken身份验证机制
}
public void test() throws Exception {
String bodystr = getFileContent("./body.xml");
StreamSource xmlSource = new StreamSource(new StringReader(bodystr));
// 生成 Service
URL wsdlURL = new URL(wsdlurl);
QName serviceQName = new QName(targetnamespace, serviceStr);
Service service = Service.create(wsdlURL, serviceQName);
// 生成 Dispatch<Source>
QName portQName = new QName(targetnamespace, portStr);
Dispatch<Source> dispatch = service.createDispatch(portQName, Source.class, Service.Mode.PAYLOAD);
// 调用WS
Source orderSource = dispatch.invoke(xmlSource);
// 处理返回结果
StreamResult result = new StreamResult(new ByteArrayOutputStream());
Transformer trans = TransformerFactory.newInstance().newTransformer();
trans.transform(orderSource, result);
ByteArrayOutputStream baos = (ByteArrayOutputStream) result.getOutputStream();
// 转换为字符串
String responseContent = new String(baos.toByteArray());
System.out.println(responseContent);
}
public void testUserNameToken() throws Exception {
String soapXml = getFileContent("./soap.xml");
StreamSource xmlSource1 = new StreamSource(new StringReader(soapXml));
// 生成 Service
URL wsdlURL = new URL(wsdlurl);
QName serviceQName = new QName(targetnamespace, serviceStr);
Service service = Service.create(wsdlURL, serviceQName);
// 生成 Dispatch<Source>
QName portQName = new QName(targetnamespace, portStr);
Dispatch<SOAPMessage> dispatch = service.createDispatch(portQName, SOAPMessage.class, Service.Mode.MESSAGE);
// 设置soap信封
MessageFactory factory = MessageFactory.newInstance();
SOAPMessage message = factory.createMessage();
message.getSOAPPart().setContent(xmlSource1);
message.saveChanges();
// 调用WS
SOAPMessage response = dispatch.invoke(message);
SOAPPart sp = response.getSOAPPart();
Source resp = sp.getContent();
// 处理返回结果
StreamResult result = new StreamResult(new ByteArrayOutputStream());
Transformer trans = TransformerFactory.newInstance().newTransformer();
trans.transform(resp, result);
ByteArrayOutputStream baos = (ByteArrayOutputStream) result.getOutputStream();
// 转换为字符串
String responseContent = new String(baos.toByteArray());
System.out.println(responseContent);
}
public String getFileContent(String file) {
StringBuffer sb = new StringBuffer();
File f = new File(file);
if (f.exists() && f.isFile()) {
try {
FileInputStream fi = new FileInputStream(f);
InputStreamReader fis = new InputStreamReader(fi);
BufferedReader in = new BufferedReader(fis);
String line = null;
while ((line = in.readLine()) != null) {
sb.append(line);
}
in.close();
fis.close();
fi.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
return sb.toString();
}
}
并且在项目下创建body.xml及soap.xml
内容分别为
body.xml
<dlwmin:getPerson xmlns:dlwmin="http://service.test/"><arg0><age>10</age><userName>jack</userName></arg0></dlwmin:getPerson>
soap.xml
<?xml version="1.0" encoding="utf-8" ?>
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<SOAP-ENV:Header>
<wsse:Security xmlns:wsse="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd">
<wsse:UsernameToken>
<wsse:Username>leezuu</wsse:Username>
<wsse:Password>mima</wsse:Password>
</wsse:UsernameToken>
</wsse:Security>
</SOAP-ENV:Header>
<SOAP-ENV:Body>
<dlwmin:getPerson xmlns:dlwmin="http://service.test/">
<arg0>
<age>10</age>
<userName>JACK</userName>
</arg0>
</dlwmin:getPerson>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>