0

我还是 SOAP 的新手,我无法解决这个问题。基本上,下面的代码创建一个 SOAP 消息发送到 API 服务器。每次我运行这个请求时,程序总是返回 NULL 和错误代码,这些代码未包含在 API 的文档中。我希望这里有人可以提供帮助。

    public class LBSController {
private static final String endpoint = "http://iplaypen.globelabs.com.ph:1881/axis2/services/Platform";

public static void main(String[] args) throws SOAPException {
    //CREATE SOAP MESSAGE
    SOAPMessage message = MessageFactory.newInstance().createMessage();
    SOAPHeader header = message.getSOAPHeader();
    header.detachNode();

    //SOAP SETTINGS
    SOAPEnvelope envelope = message.getSOAPPart().getEnvelope();
    envelope.setEncodingStyle("http://schemas.xmlsoap.org/soap/encoding/");

    SOAPBody body = message.getSOAPBody();
    QName bodyName = new QName("getConsent");
    SOAPBodyElement bodyElement = body.addBodyElement(bodyName);

    //SET CONTENT
    SOAPElement uName = bodyElement.addChildElement("uName");
    uName.addTextNode("k2r2t1zvc");
    SOAPElement uPin = bodyElement.addChildElement("uPin");
    uPin.addTextNode("21737629");
    SOAPElement MSISDN = bodyElement.addChildElement("MSISDN");
    MSISDN.addTextNode("09278328310");

    //CREATE CONNECTION
    SOAPConnection connection = SOAPConnectionFactory.newInstance().createConnection();
    SOAPMessage response = connection.call(message, endpoint);
    connection.close();

    SOAPBody responseBody = response.getSOAPBody();
    SOAPBodyElement responseElement = (SOAPBodyElement)responseBody.getChildElements().next();
    SOAPElement returnElement = (SOAPElement)responseElement.getChildElements().next();

    if(responseBody.getFault()!=null){
        System.out.println(returnElement.getValue()+" "+responseBody.getFault().getFaultString());
    } else {
        System.out.println(returnElement.getValue());
    }

    try {
        System.out.println(getXmlFromSOAPMessage(message));
        System.out.println(getXmlFromSOAPMessage(response));
    } catch (IOException e) {
        e.printStackTrace();
    }
}

private static String getXmlFromSOAPMessage(SOAPMessage msg) throws SOAPException, IOException {
    ByteArrayOutputStream byteArrayOS = new ByteArrayOutputStream();
    msg.writeTo(byteArrayOS);
    return new String(byteArrayOS.toByteArray());
}

}

4

1 回答 1

0

尝试从响应中打印出 RAW xml 代码。

这是代码

    public class LBSController 
{
    private static final String endpoint = "http://iplaypen.globelabs.com.ph:1881/axis2/services/Platform";

public static void main(String[] args) throws SOAPException {
//CREATE SOAP MESSAGE
SOAPMessage message = MessageFactory.newInstance().createMessage();
SOAPHeader header = message.getSOAPHeader();
header.detachNode();

//SOAP SETTINGS
SOAPEnvelope envelope = message.getSOAPPart().getEnvelope();
envelope.setEncodingStyle("http://schemas.xmlsoap.org/soap/encoding/");

SOAPBody body = message.getSOAPBody();
QName bodyName = new QName("getConsent");
SOAPBodyElement bodyElement = body.addBodyElement(bodyName);

//SET CONTENT
SOAPElement uName = bodyElement.addChildElement("uName");
uName.addTextNode("k2r2t1zvc");
SOAPElement uPin = bodyElement.addChildElement("uPin");
uPin.addTextNode("21737629");
SOAPElement MSISDN = bodyElement.addChildElement("MSISDN");
MSISDN.addTextNode("09278328310");

//CREATE CONNECTION
SOAPConnection connection = SOAPConnectionFactory.newInstance().createConnection();
SOAPMessage response = connection.call(message, endpoint);
connection.close();

SOAPBody responseBody = response.getSOAPBody();
SOAPBodyElement responseElement = (SOAPBodyElement)responseBody.getChildElements().next();
SOAPElement returnElement = (SOAPElement)responseElement.getChildElements().next();

if(responseBody.getFault()!=null){
    System.out.println(returnElement.getValue()+" "+responseBody.getFault().getFaultString());
} else {
    System.out.println(returnElement.getValue());
}

//PRINT OUT RAW XML
ByteArrayOutputStream out = new ByteArrayOutputStream();
String xml = "";
try {
    response.writeTo(out);
    xml = out.toString("UTF-8");
} catch (Exception e) 
{
    System.out.println(""+e);
    //log.error(e.getMessage(),e);
}      
System.out.println(""+xml);

/*
try {
    System.out.println(getXmlFromSOAPMessage(message));
    System.out.println(getXmlFromSOAPMessage(response));
} catch (IOException e) {
    e.printStackTrace();
}
* */
}

上面代码的响应值

    run:
    null
    <?xml version='1.0' encoding='utf-8'?><soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
<soapenv:Body><ns:getLocResponse xmlns:ns="http://ESCPlatform/xsd">
<ns:LocationReturn><ns:return>506</ns:return><ns:tranId>23737563092783283102013012302013976</ns:tranId>
</ns:LocationReturn></ns:getLocResponse>
</soapenv:Body>
</soapenv:Envelope>
    BUILD SUCCESSFUL (total time: 3 seconds)
于 2013-01-23T07:03:22.927 回答