我希望通过此代码在Java中创建Soap请求,但响应完全为空 怎么了 ? 代码
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>