CXF发布WS,不用tomcat,实现usernametoken认证机制(2)

本文详细介绍了如何通过Java语言使用SOAP消息进行Web Service调用,包括创建服务、生成Dispatch并调用Web Service。演示了两种调用模式:PAYLOAD模式和MESSAGE模式,并提供了XML文件的示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

上面一章已经创建了服务器端的实现,下面是客户端的实现
客户端代码:(在另外一个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>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值