2013-11-20 2 views
1

나는 매우 새로운 SOAP WebService에서 개발 자바 다음과 같은 문제점이 있습니다.Java에서이 WS에 대한 SOAP 요청 봉투를 만드는 방법은 무엇입니까?

나는이 방법을 노출시킨 Web이의 (AN 다른 사람) (즉 간단하다) 이미 구현되어 제는 SoapUI에, getVersion() 명명하고 다음과 같은 요청이 있습니다

<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 자바 프로젝트가 :

공공 문자열하는 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 메소드를 호출 자바 프로젝트에서) 새로운 방법을 만들어 같은 일을 복제해야합니다 (즉, 더 복잡하기 때문에 이전과는 달리 , 몇 가지 매개 변수를 전달해야합니다) 나는 그것에 대해 몇 가지 의구심이 있습니다. 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) 

그래서, 자바 프로젝트에서, 나는 실행 다음과 같은 방법을 만들 수 있습니다 (그러나 나는 그것의 정확성에 대해 많은 doubtsa이) 내 요청에 대한 SOAP 봉투을 만드는이 호출은

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 문자가 포함되어 있으면 수신자가 요청을 구문 분석하지 못할 수 있습니다.그냥 자신의 비밀번호 : 같은 구아바 또는 아파치 평민 같은

라이브러리는 XML의 escapers을 포함하기 </tem:login>를 사용하는 사람을 고려, 포인터에 대한이 스레드 참조 : 또는 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 :

그래서 당신의 채우기 코드는 다음과 유사 것이다 . 일반 텍스트로 그물을 통해 로그인 데이터를 보내지 마십시오! 서비스 대신 http 대신 https를 사용하려고합니다.

관련 문제