2013-11-20 75 views
1

我很新SOAP WebService开发Java我有以下问题。如何在Java中为此WS创建SOAP请求信封?

我有一个公开2种方法Web服务,第一个(即简单)已经实现(通过其他人),并命名为getVersion(),并且在了SoapUI有以下要求:

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:tem="http://tempuri.org/"> 
    <soapenv:Header/> 
    <soapenv:Body> 
     <tem:getVersion/> 
    </soapenv:Body> 
</soapenv:Envelope> 

然后我有exeute在调用Web服务,并在I类有以前的web服务方法跟随着方法的Java项目:

公共字符串getVersion(){ java.net.URL中的URL = NULL ; java.net.URLConnection conn = null;

java.io.BufferedReader rd = null; 
String soapResponse = ""; 

String risultato = ""; 

// SOAP ENVELOP for the request: 
String soapRequest; 
soapRequest = "<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:tem=\"http://tempuri.org/\">" + "<soapenv:Header/>" + "<soapenv:Body> " + "<tem:getVersion/>" + "</soapenv:Body>" + "</soapenv:Envelope>"; 

try { 

    // Try to open a connection 
    url = new java.net.URL(_webServiceUrl); 
    conn = url.openConnection(); 

    // Set the necessary header fields 
    conn.setRequestProperty("SOAPAction", "http://tempuri.org/IMyService/getVersion"); 
    conn.setRequestProperty("Content-Type", "text/xml; charset=utf-8"); 
    conn.setDoOutput(true); 

    // Send the request: 
    java.io.OutputStreamWriter wr = new java.io.OutputStreamWriter(conn.getOutputStream()); 
    wr.write(soapRequest); 
    wr.flush(); 
    // Read the response 
    rd = new java.io.BufferedReader(new java.io.InputStreamReader(conn.getInputStream())); 

    // Put the entire response into the soapResponse variable: 
    String line; 
    while ((line = rd.readLine()) != null) { 
     //System.out.println(line); 
     soapResponse = soapResponse + line + System.getProperty("line.separator"); 
    } 

    rd.close(); 

    // elaboro la risposta 
    SAXBuilder builder = new SAXBuilder(); 

    org.jdom.Document documentXML = null; 
    documentXML = builder.build(new StringReader(soapResponse)); 

    XPath xPath; 
    Element objectElement; 
    //xPath = XPath.newInstance("s:Envelope/s:Body/getVersionResponse/getVersionResult"); 
    xPath = XPath.newInstance("s:Envelope/s:Body"); 
    xPath.addNamespace("s", "http://schemas.xmlsoap.org/soap/envelope/"); 

    objectElement = (Element) xPath.selectSingleNode(documentXML); 

    if (objectElement != null) { 
     risultato = objectElement.getValue(); 
    } 

} catch (Exception ex) { 
    ex.printStackTrace(); 
} 

return risultato; 

}

现在我必须复制创建一个新的方法(在调用WS方法的Java项目)的第二种方法同样的事情(这是比较复杂的,因为,不像以前的一个,需要传递一些参数),我对此有些怀疑。

因此,情况是以下之一:在了SoapUI有一个名为getConfigSettings这个WS方法和要求是:

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:tem="http://tempuri.org/"> 
     <soapenv:Header/> 
     <soapenv:Body> 
      <tem:getConfigSettings> 
      <!--Optional:--> 
      <tem:login>?</tem:login> 
      <!--Optional:--> 
      <tem:password>?</tem:password> 
      <!--Optional:--> 
      <tem:ipAddress>?</tem:ipAddress> 
      <!--Optional:--> 
      <tem:clientVersion>?</tem:clientVersion> 
      <!--Optional:--> 
      <tem:lastUpdateTime>?</tem:lastUpdateTime> 
      </tem:getConfigSettings> 
     </soapenv:Body> 
    </soapenv:Envelope> 

As you can see it requires some parameters (in SoapUi I have to replace characters with the correct parameter value) 

所以,在Java项目中,我创建了下面的方法执行这个呼吁我的要求创建SOAP信封(但我有关于它的正确性许多doubtsa)

public String authentication(String login, String password, String ipAddress, String clientVersion) { 


     java.net.URL url = null; 
     java.net.URLConnection conn = null; 

     java.io.BufferedReader rd = null; 
     String myResponse = ""; 
     String soapXml; 

     // SOAP ENVELOP for the request: 
     //soapXml = "<s:Envelope xmlns:s=\"http://schemas.xmlsoap.org/soap/envelope/\"> " + "<s:Header>" + "<Action s:mustUnderstand=\"1\" xmlns=\"http://schemas.microsoft.com/ws/2005/05/addressing/none\">http://tempuri.org/IMyService/getVersion</Action>" + "</s:Header>" + "<s:Body>" + "<getVersion xmlns=\"http://tempuri.org/\" />" + "</s:Body>" + "</s:Envelope>"; 
     soapXml = "<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:tem=\"http://tempuri.org/\">" 
        + "<soapenv:Header/>" 
         + "<soapenv:Body> " 
          + "<tem:login>" + login + "</tem:login>" 
          + "<tem:password>" + password + "</tem:password>" 
          + "<tem:ipAddress>" + ipAddress + "</tem:ipAddress>" 
         + "</soapenv:Body>" 
        + "</soapenv:Envelope>"; 
........................................................ 
........................................................ 
........................................................ 
DO SOME OTHER STUFF 
........................................................ 
........................................................ 
........................................................ 
} 

正如你可以看到我已经创造了这个SOAP在我所插入的参数外型对我的要求recived作为方法的输入参数:

soapXml = "<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:tem=\"http://tempuri.org/\">" 
       + "<soapenv:Header/>" 
        + "<soapenv:Body> " 
         + "<tem:login>" + login + "</tem:login>" 
         + "<tem:password>" + password + "</tem:password>" 
         + "<tem:ipAddress>" + ipAddress + "</tem:ipAddress>" 
        + "</soapenv:Body>" 
       + "</soapenv:Envelope>"; 

它是正确的还是我采取了错误的解决办法?一些建议?

TNX

安德烈

回答

0

你的方法看起来简单的,但你需要XML转义要插入的字符串。

否则,如果字符串包含保留的XML字符(如<),则接收方可能不会解析您的请求。只是用</tem:login>作为他的密码:)

库,如番石榴或Apache公地包含XML逃生考虑别人,看到这个线程指针: Best way to encode text data for XML in Java?

或者,你可以只包括你自己:

public static String xmlEscape(String s) { 
    StringBuilder sb = new StringBuilder(); 
    for (int i = 0; i < sb.length(); i++) { 
    char c = s.charAt(i); 
    if ("<&\">'".indexOf(c) != -1) { 
     sb.append("&#" + ((int) c) + ";"); 
    } else { 
     sb.append(c); 
    } 
    } 
    return sb.toString(); 
} 

所以个够代码将类似于此:

soapXml = "<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:tem=\"http://tempuri.org/\">" 
      + "<soapenv:Header/>" 
       + "<soapenv:Body> " 
        + "<tem:login>" + xmlEscape(login) + "</tem:login>" 
        + "<tem:password>" + xmlEscape(password) + "</tem:password>" 

PS 。切勿以明文形式通过网络发送登录数据!您可能希望使用https而不是http为您的服务。