Java-使用SOAPMessage发送SOAP请求

时间:2019-03-21 10:07:31

标签: java soap

我正在尝试使用SOAPConnectionFactory发送/接收SOAP请求。我需要发送如下的正文请求,

<soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope" xmlns:int="http://apiDoc.com/integration/">
   <soap:Header>
      <int:Options>
         <int:ICD>false</int:ICD>
         <int:UpdateLastModified>true</int:UpdateLastModified>
      </int:Options>
   </soap:Header>
   <soap:Body>
      <int:LogIn>
         <int:username>AAA</int:username>
         <int:password>pasword</int:password>
      </int:LogIn>
   </soap:Body>
</soap:Envelope>

我尝试过的代码:

尝试{

SOAPConnectionFactory sfc = SOAPConnectionFactory.newInstance();
SOAPConnection connection = sfc.createConnection();

MessageFactory mf = MessageFactory.newInstance();
String soapRequestXml ="pasting the request body here ";
SOAPMessage sm = mf.createMessage(new MimeHeaders(),
                  new ByteArrayInputStream(soapRequestXml.getBytes()));
sm.setProperty("Content-Type", "application/soap+xml");
sm.saveChanges();
QName bodyName = new QName("Response");
URL endpoint = new URL("http://MyIP/wsdk/service.asmx");
SOAPMessage response = connection.call(sm, endpoint);
System.out.println((response.getSOAPBody()));
SOAPBody sb = response.getSOAPBody();
Iterator iterator = sb.getChildElements(bodyName);
    while (iterator.hasNext()) {
    SOAPBodyElement bodyElement = (SOAPBodyElement) iterator.next();
    String val = bodyElement.getValue();
    System.out.println("I want to print each element's value like Id,TypeId,Success" + val);
}
} catch (Exception ex) {
    ex.printStackTrace();

}

OP是:

[soap:Body: null]

实际OP进入POSTMAN:

<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
    <soap:Body>
        <Response xmlns="http://intergratedservice.com/integration/">
            <Result xsi:type="LoginResponse">
                <Id>1402</Id>
                <TypeId>1</TypeId>
                <Success>true</Success>
            </Result>
        </Response>
    </soap:Body>
</soap:Envelope>

有人告诉我我在哪里做错了?

0 个答案:

没有答案