我正在测试通过wsimport导入的Web服务,WSDL是一个具有以下结构的简单Web服务:
$("#scheduler").kendoScheduler({
dataSource: {
transport: {
read: {
url: "https://demos.telerik.com/kendo-ui/service/meetings",
dataType: "jsonp"
},
... other code...
问题是我在使用以下代码调用此Web服务时遇到一些问题,因为Header params,I
<soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope" xmlns:tem="http://tempuri.org/">
<soap:Header>
<tem:authUser>
<!--Optional:-->
<tem:userName></tem:userName>
<!--Optional:-->
<tem:password></tem:password>
<!--Optional:-->
<tem:authenticationToken></tem:authenticationToken>
</tem:authUser>
</soap:Header>
<soap:Body>
<tem:authenticateUser/>
</soap:Body>
</soap:Envelope>
我已经使用正确的用户和密码测试了这个网络服务,我得到了积极的回应,问题是我猜测如何正确设置标题参数。
这是我的回答:
public static void main(String[] args) {
// write your code here
System.out.println("Hello world");
WsTest wsTEST = new WsTest();
WsTestSoap client = wsTest.getWsTestSoap();
Map<String,Object> request = ((WSBindingProvider) client).getRequestContext();
Map<String,List<String>> headers = new HashMap<String, List<String>>();
headers.put("userName", Collections.singletonList("xxxxx"));
headers.put("password", Collections.singletonList("xxxxx"));
request.put(MessageContext.HTTP_REQUEST_HEADERS, headers);
String re1 = client.authenticateUser();
String re2 = client.existencias("xxxx");
System.out.println(re1);
System.out.println(re2);
}
任何帮助将不胜感激。
答案 0 :(得分:0)
这是解决方案......
package com.company;
import javax.xml.namespace.QName;
import javax.xml.soap.*;
import javax.xml.ws.handler.MessageContext;
import javax.xml.ws.handler.soap.SOAPHandler;
import javax.xml.ws.handler.soap.SOAPMessageContext;
import java.util.Collections;
import java.util.Set;
import java.util.logging.Level;
import java.util.logging.Logger;
public class AuthenticationHandler implements SOAPHandler<SOAPMessageContext> {
@Override
public Set<QName> getHeaders() {
return Collections.EMPTY_SET;
}
@Override
public boolean handleMessage(SOAPMessageContext context) {
SOAPMessage msg = context.getMessage();
try {
if((Boolean) context.get(MessageContext.MESSAGE_OUTBOUND_PROPERTY)){
//System.out.println("Intercepting outbound message");
SOAPEnvelope envelope = msg.getSOAPPart().getEnvelope();
envelope.addNamespaceDeclaration("tem","http://tempuri.org/");
if(envelope.getHeader() != null){
envelope.getHeader().detachNode();
}
SOAPHeader header = envelope.addHeader();
SOAPHeaderElement element = header.addHeaderElement(envelope.createName("authUser","tem","http://tempuri.org/"));
SOAPElement userName = element.addChildElement("userName","tem");
userName.setTextContent("xxxxx");
SOAPElement password = element.addChildElement("password","tem");
password.setTextContent("xxxxx");
msg.saveChanges();
//msg.writeTo(System.out);
}
} catch (Exception ex) {
Logger.getLogger(AuthenticationSFEHandler.class.getName()).log(Level.SEVERE, null, ex);
}
return true;
}
@Override
public boolean handleFault(SOAPMessageContext context) {
return true;
}
@Override
public void close(MessageContext context) {
}
}
&#13;