gpt4 book ai didi

Java 创建的 Soap 客户端没有得到响应,但 SOAPUI 也能正常工作

转载 作者:太空宇宙 更新时间:2023-11-04 13:28:44 27 4
gpt4 key购买 nike

我正在java中创建一个简单的soap客户端有效负载,但服务器回复“服务器无法识别http header 的值”。我复制了创建的 SOAP 有效负载并粘贴到 SOAP-UI 中并执行了请求,有效负载完美运行。

我还仔细检查了 SOAP 头信息。一切都是正确的。但我仍然没有得到我的 java 客户端的响应。

有人可以给我任何关于这个的想法吗?

public class SoapClientClass {

public static void main(String[] args) throws UnsupportedOperationException, SOAPException, IOException, TransformerException {
// TODO Auto-generated method stub

//Getting an instance of Soap Connection factory
SOAPConnectionFactory myFct = SOAPConnectionFactory.newInstance();
//Getting a Soap Connection from the soap connection factory object
SOAPConnection myCon = myFct.createConnection();
//Getting a message factory object to create a message
MessageFactory myMsgFct = MessageFactory.newInstance();
//Using the message factory to create a message
SOAPMessage message = myMsgFct.createMessage();
//Adding message to the body

SOAPPart mySPART = message.getSOAPPart();
SOAPEnvelope myEnvp = mySPART.getEnvelope();
myEnvp.addNamespaceDeclaration("web","http://www.webservicex.net/");
//SOAPHeader header = myEnvp.getHeader();
//javax.xml.soap.Name HeaderName = myEnvp.createName("GetGeoIP", "web","http://www.webservicex.net/");
//SOAPHeaderElement hltp = header.addHeaderElement(HeaderName);
SOAPBody body = myEnvp.getBody();
javax.xml.soap.Name bodyName = myEnvp.createName("GetGeoIP", "web","http://www.webservicex.net/");
SOAPBodyElement gltp = body.addBodyElement(bodyName);

javax.xml.soap.Name myContent = myEnvp.createName("IPAddress","web", "http://www.webservicex.net/");
SOAPElement mySymbol = gltp.addChildElement(myContent);
mySymbol.addTextNode("192.128.62.43");
message.saveChanges();
message.writeTo(System.out);
URLEndpoint endPt = new URLEndpoint("http://www.webservicex.net//geoipservice.asmx");
SOAPMessage reply = myCon.call(message, endPt);
//reply.writeTo(System.out);
System.out.println("\n");
TransformerFactory tff = TransformerFactory.newInstance();
Transformer tf = tff.newTransformer();

Source sc = reply.getSOAPPart().getContent();
StreamResult result = new StreamResult(System.out);
tf.transform(sc, result);
System.out.println("test");
//conn.close();

SOAPBody body1 = reply.getSOAPBody();
if ( body1.hasFault() ) {
SOAPFault newFault = body1.getFault();
QName code = newFault.getFaultCodeAsQName();
String string = newFault.getFaultString();
String actor = newFault.getFaultActor();
System.out.println(string);
}

myCon.close();
//System.out.println(reply);
}

最佳答案

这是一个非常古老的请求,但如果有人可能遇到类似的问题,仍然会回答。根据错误,它需要 HTTP header SOAPAction。上面的代码需要两行HTTP header设置代码。

SOAPEnvelope myEnvp = mySPART.getEnvelope();

MimeHeaders mheader=message.getMimeHeaders();
mheader.setHeader("SOAPAction", "http://www.webservicex.net/GetGeoIP");

myEnvp.addNamespaceDeclaration("web","http://www.webservicex.net/");

关于Java 创建的 Soap 客户端没有得到响应,但 SOAPUI 也能正常工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32405343/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com