我创建了一个基于 wsdl 文件的客户端。
我正在使用以下代码调用 Web 服务:
PrivateService ser = new PrivateService();
PrivatePortType port = ser.getPrivateSOAPPort();
BindingProvider bindingProvider = (BindingProvider) port;
bindingProvider.getRequestContext().put(BindingProvider.ENDPOINT_ADDRESS_PROPERTY,
"http://192.168.4.48/PrivateSimulator/PrivateWebService.svc");
bindingProvider.getRequestContext().put(BindingProvider.SOAPACTION_USE_PROPERTY, true);
bindingProvider.getRequestContext().put(BindingProvider.SOAPACTION_URI_PROPERTY, "CreateParty");
CreatePartyRequest createReq = new CreatePartyRequest();
createReq.setParam("123456");
createReq.setCallback("http://192.168.5.106:9999/Service");
try {
CreatePartyResponse resp = port.createParty(createReq);
PartyInfo ci = resp.getPartyInfo();
System.out.println(ci.getPartyId());
} catch (WSException e) {
e.printStackTrace();
}
在 createParty 调用中,我接受“对象引用未设置为对象的实例”异常消息。
这可能是因为 Web 服务是用 C# 编写的,并且很可能需要 oasis 标头,其中存储用户名和密码值。
我现在可以创建的信封是:
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Body>
<CreatePartyRequest xmlns="http://teleParty.QQQ.com/CCGW">
<param>10000903</param>
<callback>http://192.168.5.106:9999/Service</callback>
</CreatePartyRequest>
</soap:Body>
</soap:Envelope>
所需的信封如下:
<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/" xmlns:u="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd">
<s:Header>
<o:Security s:mustUnderstand="1" xmlns:o="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd">
<o:UsernameToken u:Id="uuid-2aae5c05-19af-43e6-8814-7d7e7a306d4d-3">
<o:Username>QQQ</o:Username>
<o:Password Type="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordText">QQQPass</o:Password>
</o:UsernameToken>
</o:Security>
</s:Header>
<s:Body xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<CreatePartyRequest xmlns="http://teleParty.QQQ.com/CCGW">
<param>10000903</param>
<callback>http://192.168.5.106:9999/Service</callback>
</CreatePartyRequest>
</s:Body>
</s:Envelope>
如何将这些标头和属性添加到请求中?