问题可能是由于 SOAP 请求的标头无效或格式无效,您可以尝试如下代码
1 你需要HeaderHandlerResolver
public class HeaderHandlerResolver implements HandlerResolver {
public List<Handler> getHandlerChain(PortInfo portInfo) {
List<Handler> handlerChain = new ArrayList<Handler>();
HeaderHandler hh = new HeaderHandler();
handlerChain.add(hh);
return handlerChain;
}
}
然后你需要添加HeaderHandler类
public class HeaderHandler implements SOAPHandler<SOAPMessageContext> {
public boolean handleMessage(SOAPMessageContext smc) {
Boolean outboundProperty = (Boolean) smc.get(MessageContext.MESSAGE_OUTBOUND_PROPERTY);
if (outboundProperty.booleanValue()) {
SOAPMessage message = smc.getMessage();
try {
SOAPEnvelope envelope = smc.getMessage().getSOAPPart().getEnvelope();
SOAPHeader header = envelope.getHeader();
header.setPrefix("soapenv");
header.setAttribute("xmlns:wsa", "http://www.w3.org/2005/08/addressing");
SOAPElement security =
header.addChildElement("Security", "wsse", "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd");
SOAPElement usernameToken =
security.addChildElement("UsernameToken", "wsse");
usernameToken.addAttribute(new QName("xmlns:wsu"), "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd");
SOAPElement username =
usernameToken.addChildElement("Username", "wsse");
username.addTextNode("USERNAME");
SOAPElement password =
usernameToken.addChildElement("Password", "wsse");
password.setAttribute("Type", "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordText");
password.addTextNode("PASSWORD");
SOAPElement encode =
usernameToken.addChildElement("Nonce", "wsse");
encode.setAttribute("EncodingType", "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-soap-message-security-1.0#Base64Binary");
encode.addTextNode(generateNonce());
Calendar createdTime = new GregorianCalendar(TimeZone.getTimeZone("IST"));
Date todayDate = createdTime.getTime();
todayDate.setTime(todayDate.getTime()-20000000);
SOAPElement created = usernameToken.addChildElement("Created", "wsu");
created.addTextNode(new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.sss'Z'").format(todayDate));
SOAPElement action = header.addChildElement("Action", "wsa");
//YOUR ACTION URL SHOULD BE in BELOW Text Content
action.setTextContent("SET HERE YOUR ACTION URL");
message.saveChanges();
message.writeTo(System.out);
System.out.println("");
} catch (Exception e) {
e.printStackTrace();
}
} else {
try {
//This handler does nothing with the response from the Web Service so
//we just print out the SOAP message.
SOAPMessage message = smc.getMessage();
message.writeTo(System.out);
System.out.println("");
} catch (Exception ex) {
ex.printStackTrace();
}
}
return outboundProperty;
}
public Set getHeaders() {
return null;
}
public boolean handleFault(SOAPMessageContext context) {
return true;
}
public void close(MessageContext context) {
}
private static String generateNonce() throws NoSuchAlgorithmException, NoSuchProviderException, UnsupportedEncodingException {
String dateTimeString = Long.toString(new Date().getTime());
byte[] nonceByte = dateTimeString.getBytes();
return Base64.encodeBase64String(nonceByte);
}
}
现在终于是调用 SOAP 服务的主类
public class SoapClientClass {
public static void main(String[] args) {
ImplService service = new ImplService();
HeaderHandlerResolver handlerResolver = new HeaderHandlerResolver();
service.setHandlerResolver(handlerResolver);
ResponseClass port = service.getPortClass();
Response response = null;
try {
response = port.getServerMehotd("Params");
} catch (PolicyException_Exception e) {
e.printStackTrace();
} catch (ServiceException_Exception e) {
e.printStackTrace();
}
}
}
}
此外,请确保您从 wsdl 文件更新生成的代码和服务器位置 url 也正确。
希望它能解决你的问题