简体   繁体   中英

Java Programatically Request SOAP Web Service Issue

I'm attempting to make a SOAP request to an end point programmatically through Java. I'm relatively new to Java and web services so I'm not sure what I'm doing wrong here.

Also I print out the SOAP message and can paste that into a tool like postman and enter the end point and a post is successful. So i think something with my request is not correct here.

Here is my code:

    System.out.println("hey now!!!!");

    try {
        SOAPConnectionFactory scf = SOAPConnectionFactory.newInstance();
        SOAPConnection connection = scf.createConnection();     

        MessageFactory mf = MessageFactory.newInstance();
        SOAPMessage message = mf.createMessage();
        SOAPBody body = message.getSOAPBody();  

        SOAPHeader header = message.getSOAPHeader();

        SOAPElement getOpenPOs = body.addChildElement("GetOpenPOs", "", "https://www.autocrib.net");

        SOAPElement U = getOpenPOs.addChildElement("U");
        U.addTextNode("u");
        SOAPElement P = getOpenPOs.addChildElement("P");
        P.addTextNode("p");
        SOAPElement N = getOpenPOs.addChildElement("N");
        N.addTextNode("n");         
        SOAPElement Processed = getOpenPOs.addChildElement("Processed");
        Processed.addTextNode("false");
        SOAPElement StationEnd = getOpenPOs.addChildElement("StationEnd");
        StationEnd.addTextNode("");         
        SOAPPart sp = message.getSOAPPart();
        SOAPEnvelope envelope = sp.getEnvelope();

        //MimeHeaders headers = message.getMimeHeaders();
        //header.setHeader("Content-Type", "text/xml");
        //message.getMimeHeaders().addHeader("SOAPAction", "GetOpenPOs");
        message.getMimeHeaders().addHeader("Content-Type", "text/xml");
        header.setAttribute("Content-Type", "text/xml");

        message.saveChanges();

        System.out.println("Envelope Body");

        message.writeTo(System.out);
        System.out.println();

        SOAPMessage reply = connection.call(message, 
                "https://www24.autocrib.net/WebServices/AutoCribWS.asmx");

        //String reply2 = connection.call(message, "https://www24.autocrib.net/WebServices/AutoCribWS.asmx").toString();

        //sp = reply.getSOAPPart();
        //envelope = sp.getEnvelope();
        //body = envelope.getBody();

        //System.out.println(body.toString());
        System.out.println("Done!!!!!!!!!!!!!!!!!!!");

    } catch (Throwable t) {
        System.out.println("Something went wrong!!! " + t.toString());
    }
}

I get this error when I run this code:

Oct 24, 2016 1:56:57 PM com.sun.xml.internal.messaging.saaj.soap.MessageImpl identifyContentType SEVERE: SAAJ0537: Invalid Content-Type. Could be an error message instead of a SOAP message Something went wrong!!! com.sun.xml.internal.messaging.saaj.SOAPExceptionImpl: com.sun.xml.internal.messaging.saaj.SOAPExceptionImpl: Invalid Content-Type:text/html. Is this an error message instead of a SOAP response?

I'm guessing I need to add the Content-Type header. Am I doing this incorrectly? Any guidance would be great.

Thanks, Tim

com.sun.xml.internal.messaging.saaj.SOAPExceptionImpl: Invalid Content-Type:text/html.

The SAAJ API throws an exception because it considers that you web service returns as response text/html content instead of soap/xml content.

So, one advise : study the content returned by postman. Are you sure it is soap/xml format ? It you notice that is not soap/xml content, work on the implementation of your WS and if needed adapt the return to be compliant with the SOAP norm.

Wilco I would like to give you credit for the answer but I don't think I can do that for comments. Your tip helped me figure out that it was indeed returning text/html because of the user agent header I had.

THanks again!!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM