Java Created Soap Client没有得到响应,但是同样的工作来自SOAPUI

时间:2015-09-04 19:34:41

标签: java web-services soap

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

我还仔细检查了肥皂标题信息。一切都是正确的。但我仍然没有得到我的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);            
}

1 个答案:

答案 0 :(得分:0)

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

SOAPEnvelope myEnvp = mySPART.getEnvelope();

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

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