作者热门文章
- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我提供了一个 Java 代码,我在堆栈溢出的某个地方找到了它,我想为我的目的使用类似的东西。这段代码工作正常,但我面临一个持续存在的问题。检查我在代码中完成的系统输出。我为获取响应的 SOAP 主体而打印的前 2 个系统输出返回空值,而当我尝试打印整个响应时,它会准确打印。很奇怪。
有谁知道为什么会发生这种情况以及如何解决它。我正在使用 JDK 1.7
package trials;
import javax.xml.soap.MessageFactory;
import javax.xml.soap.MimeHeaders;
import javax.xml.soap.SOAPBody;
import javax.xml.soap.SOAPConnection;
import javax.xml.soap.SOAPConnectionFactory;
import javax.xml.soap.SOAPElement;
import javax.xml.soap.SOAPEnvelope;
import javax.xml.soap.SOAPMessage;
import javax.xml.soap.SOAPPart;
public class SOAPClientSAAJ {
public static void main(String args[]) throws Exception {
// Create SOAP Connection
SOAPConnectionFactory soapConnectionFactory = SOAPConnectionFactory.newInstance();
SOAPConnection soapConnection = soapConnectionFactory.createConnection();
// Send SOAP Message to SOAP Server
String url = "http://ws.cdyne.com/emailverify/Emailvernotestemail.asmx";
SOAPMessage soapResponse = soapConnection.call(createSOAPRequest(), url);
System.out.println("Body");
// print SOAP Response
System.out.print("Response SOAP Message:");
System.out.println("SOAP Body 2= " + soapResponse.getSOAPBody());
System.out.println("SOAP Body 2=" + soapResponse.getSOAPPart().getEnvelope().getBody());
soapResponse.writeTo(System.out);
soapConnection.close();
}
private static SOAPMessage createSOAPRequest() throws Exception {
MessageFactory messageFactory = MessageFactory.newInstance();
SOAPMessage soapMessage = messageFactory.createMessage();
SOAPPart soapPart = soapMessage.getSOAPPart();
String serverURI = "http://ws.cdyne.com/";
// SOAP Envelope
SOAPEnvelope envelope = soapPart.getEnvelope();
envelope.addNamespaceDeclaration("example", serverURI);
/*
Constructed SOAP Request Message:
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:example="http://ws.cdyne.com/">
<SOAP-ENV:Header/>
<SOAP-ENV:Body>
<example:VerifyEmail>
<example:email>mutantninja@gmail.com</example:email>
<example:LicenseKey>123</example:LicenseKey>
</example:VerifyEmail>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>
*/
// SOAP Body
SOAPBody soapBody = envelope.getBody();
SOAPElement soapBodyElem = soapBody.addChildElement("VerifyEmail", "example");
SOAPElement soapBodyElem1 = soapBodyElem.addChildElement("email", "example");
soapBodyElem1.addTextNode("mutantninja@gmail.com");
SOAPElement soapBodyElem2 = soapBodyElem.addChildElement("LicenseKey", "example");
soapBodyElem2.addTextNode("123");
MimeHeaders headers = soapMessage.getMimeHeaders();
headers.addHeader("SOAPAction", serverURI + "VerifyEmail");
soapMessage.saveChanges();
/* Print the request message */
System.out.print("Request SOAP Message:");
soapMessage.writeTo(System.out);
System.out.println();
return soapMessage;
}
这是我在控制台上得到的输出
Request SOAP Message:<SOAP-ENV:Envelope xmlns:SOAP- ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:example="http://ws.cdyne.com/"><SOAP-ENV:Header/><SOAP-ENV:Body><example:VerifyEmail><example:email>mutantninja@gmail.com</example:email><example:LicenseKey>123</example:LicenseKey></example:VerifyEmail></SOAP-ENV:Body></SOAP-ENV:Envelope>
Body
Response SOAP Message:SOAP Body 2= [soap:Body: null]
SOAP Body 2=[soap:Body: null]
<?xml version="1.0" encoding="utf-8"?><soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"><soap:Body><VerifyEmailResponse xmlns="http://ws.cdyne.com/"><VerifyEmailResult><ResponseText>Mail Server will accept email</ResponseText><ResponseCode>3</ResponseCode><LastMailServer>gmail-smtp-in.l.google.com</LastMailServer><GoodEmail>true</GoodEmail></VerifyEmailResult></VerifyEmailResponse></soap:Body></soap:Envelope>
最佳答案
Node
的
toString()
实现为 ["+getNodeName()+": "+getNodeValue()+"]
,对于元素节点,getNodeValue()
将为 null。因此,您将得到 [soap:Body: null]
。
但是,实体就在那里,您可以像往常一样使用它。
尝试以下操作以查看所有元素是否都存在:
SOAPBody body = soapResponse.getSOAPBody();
System.out.println(body.getElementsByTagName("ResponseText").item(0).getTextContent());
System.out.println(body.getElementsByTagName("ResponseCode").item(0).getTextContent());
System.out.println(body.getElementsByTagName("GoodEmail").item(0).getTextContent());
关于java - getSOAPBody 返回 NULL 而 SOAPResponse.writeTo 打印整个消息,奇怪吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26709562/
解码 Soap 响应时 getSOAPBody 返回 null。 我尝试过使用 XMLStreamReader 在这种情况下有效。但我想知道一种通过 ByteArrayInputStream 做到这一
我提供了一个 Java 代码,我在堆栈溢出的某个地方找到了它,我想为我的目的使用类似的东西。这段代码工作正常,但我面临一个持续存在的问题。检查我在代码中完成的系统输出。我为获取响应的 SOAP 主体而
我是一名优秀的程序员,十分优秀!