我最近一直在努力用Java创建一个简单的soap web服务客户端。我使用了来自jax-ws的不同实现的几个工具来从WSDL生成客户端。即使客户端生成了,每当我尝试在该服务上调用一个方法时,我都不会从该方法中获得结果(在调用这种方法之后永远不会返回)。
示例代码:
try {
TerytWs1Locator locator = new TerytWs1Locator();
ITerytWs1 service = locator.getcustom();
// If authorization is required
((CustomStub)service).setUsername("XXX");
((CustomStub)service).setPassword("YYY");
// invoke business method
System.out.println(service.czyZalogowany());
} catch (javax.xml.rpc.ServiceException ex) {
ex.printStackTrace();
} catch (java.rmi.RemoteException ex) {
ex.printStackTrace();
}
因此,在这种情况下,由于程序停留在service.czyZalogowany()方法上,因此永远不会调用println。
我认为服务可能没有运行,所以我在.NET中生成了示例客户端:
try
{
var proxy = new ChannelFactory<ServiceReference1.ITerytWs1>("custom");
proxy.Credentials.UserName.UserName = "XXX";
proxy.Credentials.UserName.Password = "YYY";
var result = proxy.CreateChannel();
var test = result.CzyZalogowany();
}catch (Exception ex) { }
我可以轻松地调用任何具有所需结果的方法。
我的Java代码有什么问题?
这是该服务的wsdl: https://uslugaterytws1test.stat.gov.pl/wsdl/terytws1.wsdl
答案 0 :(得分:0)
这个适合我。
private String user = "XXX";
private String passwordSTR = "XXX";
private final String WSSE = "wsse";
private final String WSA = "wsa";
private final String TEM_URI="http://tempuri.org/";
private final String TEM="tem";
private final String IS_LOGGED = "CzyZalogowany";
private String SERVICE_PATH = "https://uslugaterytws1test.stat.gov.pl/TerytWs1.svc";
TerytServiceWS(){
}
public boolean isLogged(){
WebServiceTemplate wsTempl = new WebServiceTemplate();
wsTempl.setDefaultUri(SERVICE_PATH);
String result = "false";
WebServiceMessage message;
try {
message = (WebServiceMessage) wsTempl.sendAndReceive(new WebServiceMessageCallback() {
public void doWithMessage(WebServiceMessage message) {
try {
SaajSoapMessage soapMessage = (SaajSoapMessage) message;
SOAPMessage smc = soapMessage.getSaajMessage();
SOAPHeader header = smc.getSOAPHeader();
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(user);
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(passwordSTR);
SOAPElement action =
header.addChildElement("Action", WSA, "http://www.w3.org/2005/08/addressing");
action.addTextNode("http://tempuri.org/ITerytWs1/"+IS_LOGGED);
SOAPBody soapBody = smc.getSOAPBody();
QName isLoggedQName = new QName(TEM_URI, IS_LOGGED, TEM);
soapBody.addChildElement(isLoggedQName);
((SaajSoapMessage) message).setSaajMessage(smc);
} catch (Exception e) {
}
}
}, new WebServiceMessageExtractor() {
public Object extractData(WebServiceMessage message) throws IOException {
return message;
}
});
SOAPMessage saajMessage = ((SaajSoapMessage) message).getSaajMessage();
SOAPBody soapBody = saajMessage.getSOAPBody();
NodeList responseNodeList = soapBody.getChildNodes();
for (int count = 0; count < responseNodeList.getLength(); count++) {
org.w3c.dom.Node tempNode = responseNodeList.item(count);
if (tempNode.getNodeType() == org.w3c.dom.Node.ELEMENT_NODE) {
if (tempNode.getLocalName().equals("CzyZalogowanyResponse")) {
org.w3c.dom.Node responseNode = tempNode.getFirstChild();
if (responseNode.getLocalName().equals("CzyZalogowanyResult")) {
if (responseNode.getFirstChild()!=null)
result = responseNode.getFirstChild().getTextContent();
}
}
}
else{
return false;
}
}
}catch (Exception e){
}
return Boolean.valueOf(result);
}