2016-01-27 255 views
0

我一直在努力最近創建一個簡單的SOAP Web服務客戶端在Java中。我使用了來自不同jax-ws實現的多個工具來從WSDL生成一個客戶端。即使客戶端得到生成,每當我嘗試調用該服務的方法時,我都不會從該方法接收結果(調用此方法後永遠不會返回)。調用SOAP webservice永遠不會返回

示例代碼:

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(); 
    } 

因此,在這種情況下的println不會被調用作爲service.czyZalogowany()方法的程序stucks。

我想,也許該服務沒有運行,所以我產生了.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

http://stackoverflow.com/questions/38012135/error-with-connecting-to-wsdl – user7172

回答

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); 
} 
相關問題