2

我想通过这段代码在 Java 中发出 Soap 请求,但响应完全为 null 有什么问题?代码

private static String Url = "http://xxx.xxx.xxx.xxx:8737/SoapSmsReceiver.asmx";
private static String Action = "http://xxx.com/Service/Deliver"
public static String SendSoapRequest(String methodName, String base64Data) {

    try {

        MessageFactory mf = MessageFactory.newInstance();
        SOAPMessage sm = mf.createMessage();


        SOAPEnvelope envelope = sm.getSOAPPart().getEnvelope();
        envelope.setAttribute("xmlns:xsi", "http://www.w3.org/2001/XMLSchema-instance");
        envelope.setAttribute("xmlns:xsd", "http://www.w3.org/2001/XMLSchema");         
        // envelope.setAttribute("xmlns:soap","http://schemas.xmlsoap.org/soap/envelope/");

        MimeHeaders mimeheaders = sm.getMimeHeaders();
        mimeheaders.setHeader("SOAPAction", Action);
        mimeheaders.setHeader("Content-Type", "text/xml; charset=utf-8");
        mimeheaders.setHeader("Host", "xxx.xxx.xxx.xxx");

        SOAPHeader sh = sm.getSOAPHeader();                     
        SOAPBody sb = sm.getSOAPBody();
        // sh.detachNode();
        QName bodyName = new QName(Action, Method, "");
        SOAPBodyElement bodyElement = sb.addBodyElement(bodyName);
        QName qn = new QName("data");
        SOAPElement quotation = bodyElement.addChildElement(qn);

        quotation.addTextNode(base64Data);

        System.out.println("\n Soap Request:\n");
        sm.writeTo(System.out);
        System.out.println();

        URL endpoint = new URL(Url);

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

        SOAPMessage response = connection.call(sm, endpoint);
                    // PRINTS null ////////////////   
        System.out.println(response.getContentDescription());

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

    return "";

}

以下是一个示例 SOAP 1.1 请求

POST /SoapSmsReceiver.asmx HTTP/1.1
Host: xxx.xxx.xxx.xxx
Content-Type: text/xml; charset=utf-8
Content-Length: length
SOAPAction: "http://xxx.com/Service/Deliver"

<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"   xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
   <soap:Body>
    <Deliver xmlns="http://xxx.com/Service/Deliver">
     <data>base64Binary</data>
   </Deliver>
 </soap:Body>
</soap:Envelope>
4

0 回答 0