如何使用 JAVA 库更改 XML SOAP 信封?
How to change XML SOAP envelop by using JAVA library?
我已经使用 javax.xml.soap 库来调用 soap 请求和响应。我有 wsdl 端点 url,它在 SOAP-UI 中工作正常。我需要使用 java 验证相同的内容,并且我创建了一个 soap 请求,它看起来与实际请求不同,如下所示
JAVA 请求负载:
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:web="http://www.webservicex.com/">
<SOAP-ENV:Header/>
<SOAP-ENV:Body>
<web:GetCitiesByCountry>
<web:CountryName>US</web:CountryName>
</web:GetCitiesByCountry>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>
SOAP 请求负载:
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:web="http://www.webserviceX.NET">
<soapenv:Header/>
<soapenv:Body>
<web:GetCitiesByCountry>
<!--Optional:-->
<web:CountryName>US</web:CountryName>
</web:GetCitiesByCountry>
</soapenv:Body>
</soapenv:Envelope>
JAVA 请求负载未按预期工作,它抛出以下错误响应。
<?xml version="1.0" encoding="utf-8"?><soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"><soap:Body><soap:Fault><faultcode>soap:Client</faultcode><faultstring>System.Web.Services.Protocols.SoapException: Server did not recognize the value of HTTP Header SOAPAction: http://www.webservicex.com/GetCitiesByCountry.
at System.Web.Services.Protocols.Soap11ServerProtocolHelper.RouteRequest()
at System.Web.Services.Protocols.SoapServerProtocol.RouteRequest(SoapServerMessage message)
at System.Web.Services.Protocols.SoapServerProtocol.Initialize()
at System.Web.Services.Protocols.ServerProtocolFactory.Create(Type type, HttpContext context, HttpRequest request, HttpResponse response, Boolean& abortProcessing)</faultstring><detail /></soap:Fault></soap:Body></soap:Envelope>
除了信封外,我没有发现两个请求有效负载之间有任何区别,我尝试使用
修改信封
SOAPConstants.SOAP_1_1_PROTOCOL and 1_2 PROTOCOL but no luck.
目前我试过以下代码:
private static SOAPMessage createSOAPRequest() throws Exception
{
MessageFactory messageFactory = MessageFactory.newInstance();
SOAPMessage soapMessage = messageFactory.createMessage();
//Tried to change the envelop which is not working
/*MessageFactory messageFactory = MessageFactory.newInstance(SOAPConstants.SOAP_1_1_PROTOCOL);
SOAPMessage soapMessage = messageFactory.createMessage();
soapMessage.getSOAPPart().setPrefix("soapenv");
soapMessage.getSOAPPart().removeMimeHeader("SOAP-ENV");
soapMessage.getSOAPHeader().setPrefix("soapenv");
soapMessage.getSOAPBody().setPrefix("soapenv");
messageFactory.createMessage();*/
SOAPPart soapPart = soapMessage.getSOAPPart();
String serverURI = "http://www.webservicex.com/";
// SOAP Envelope
SOAPEnvelope envelope = soapPart.getEnvelope();
envelope.addNamespaceDeclaration("web", serverURI);
// SOAP Body
SOAPBody soapBody = envelope.getBody();
SOAPElement soapBodyElem = soapBody.addChildElement(
"GetCitiesByCountry", "web");
SOAPElement soapBodyElem1 soapBodyElem.addChildElement("CountryName",
"web");
soapBodyElem1.addTextNode("US");
MimeHeaders headers = soapMessage.getMimeHeaders();
headers.addHeader("SOAPAction", serverURI + "GetCitiesByCountry");
System.out.println(soapMessage.getSOAPBody());
soapMessage.saveChanges();
/* Print the request message */
System.out.print("Request SOAP Message:");
soapMessage.writeTo(System.out);
System.out.println();
return soapMessage;
}
我复制了请求负载并将信封从 SOAP-ENV 更改为 sopaenv 并在 SOAPUI 中调用,这是 return 适当的响应。那么有人可以告诉我如何使用 java 库更改信封标签。
你能试试吗
String serverURI = "http://www.webserviceX.NET";
而不是
String serverURI = "http://www.webservicex.com/";
我已经使用 javax.xml.soap 库来调用 soap 请求和响应。我有 wsdl 端点 url,它在 SOAP-UI 中工作正常。我需要使用 java 验证相同的内容,并且我创建了一个 soap 请求,它看起来与实际请求不同,如下所示
JAVA 请求负载:
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:web="http://www.webservicex.com/">
<SOAP-ENV:Header/>
<SOAP-ENV:Body>
<web:GetCitiesByCountry>
<web:CountryName>US</web:CountryName>
</web:GetCitiesByCountry>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>
SOAP 请求负载:
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:web="http://www.webserviceX.NET">
<soapenv:Header/>
<soapenv:Body>
<web:GetCitiesByCountry>
<!--Optional:-->
<web:CountryName>US</web:CountryName>
</web:GetCitiesByCountry>
</soapenv:Body>
</soapenv:Envelope>
JAVA 请求负载未按预期工作,它抛出以下错误响应。
<?xml version="1.0" encoding="utf-8"?><soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"><soap:Body><soap:Fault><faultcode>soap:Client</faultcode><faultstring>System.Web.Services.Protocols.SoapException: Server did not recognize the value of HTTP Header SOAPAction: http://www.webservicex.com/GetCitiesByCountry.
at System.Web.Services.Protocols.Soap11ServerProtocolHelper.RouteRequest()
at System.Web.Services.Protocols.SoapServerProtocol.RouteRequest(SoapServerMessage message)
at System.Web.Services.Protocols.SoapServerProtocol.Initialize()
at System.Web.Services.Protocols.ServerProtocolFactory.Create(Type type, HttpContext context, HttpRequest request, HttpResponse response, Boolean& abortProcessing)</faultstring><detail /></soap:Fault></soap:Body></soap:Envelope>
除了信封外,我没有发现两个请求有效负载之间有任何区别,我尝试使用
修改信封SOAPConstants.SOAP_1_1_PROTOCOL and 1_2 PROTOCOL but no luck.
目前我试过以下代码:
private static SOAPMessage createSOAPRequest() throws Exception
{
MessageFactory messageFactory = MessageFactory.newInstance();
SOAPMessage soapMessage = messageFactory.createMessage();
//Tried to change the envelop which is not working
/*MessageFactory messageFactory = MessageFactory.newInstance(SOAPConstants.SOAP_1_1_PROTOCOL);
SOAPMessage soapMessage = messageFactory.createMessage();
soapMessage.getSOAPPart().setPrefix("soapenv");
soapMessage.getSOAPPart().removeMimeHeader("SOAP-ENV");
soapMessage.getSOAPHeader().setPrefix("soapenv");
soapMessage.getSOAPBody().setPrefix("soapenv");
messageFactory.createMessage();*/
SOAPPart soapPart = soapMessage.getSOAPPart();
String serverURI = "http://www.webservicex.com/";
// SOAP Envelope
SOAPEnvelope envelope = soapPart.getEnvelope();
envelope.addNamespaceDeclaration("web", serverURI);
// SOAP Body
SOAPBody soapBody = envelope.getBody();
SOAPElement soapBodyElem = soapBody.addChildElement(
"GetCitiesByCountry", "web");
SOAPElement soapBodyElem1 soapBodyElem.addChildElement("CountryName",
"web");
soapBodyElem1.addTextNode("US");
MimeHeaders headers = soapMessage.getMimeHeaders();
headers.addHeader("SOAPAction", serverURI + "GetCitiesByCountry");
System.out.println(soapMessage.getSOAPBody());
soapMessage.saveChanges();
/* Print the request message */
System.out.print("Request SOAP Message:");
soapMessage.writeTo(System.out);
System.out.println();
return soapMessage;
}
我复制了请求负载并将信封从 SOAP-ENV 更改为 sopaenv 并在 SOAPUI 中调用,这是 return 适当的响应。那么有人可以告诉我如何使用 java 库更改信封标签。
你能试试吗
String serverURI = "http://www.webserviceX.NET";
而不是
String serverURI = "http://www.webservicex.com/";