Skip to Content
0
Former Member
Apr 18, 2018 at 10:11 PM

ISSUE CONSUMING WEB SERVICE SAP FROM JAVA

639 Views Last edit Apr 19, 2018 at 06:15 AM 2 rev

Hi People.

I want to consume a web service SAP from Java, but I cannot. The web service works in SOAP UI , but it does not work on Java. This is my code:


import java.util.Base64;
import javax.xml.namespace.QName;
import javax.xml.soap.*;
import javax.xml.transform.*;
import javax.xml.transform.stream.*;


public class SOAPClientSAAJ {

    public static void main(String args[]) {
    	
        try {
            SOAPConnectionFactory soapConnectionFactory = SOAPConnectionFactory.newInstance();
            SOAPConnection soapConnection = soapConnectionFactory.createConnection();
            

            String url = "http://vhicedesci.hec.ice.go.cr:8000/sap/bc/srt/wsdl/flv_10002A111AD1/bndg_url/sap/bc/srt/rfc/sap/z_wsdl_suma/100/z_wsdl_suma/z_wsdl_suma?sap-client=100";


            SOAPMessage soapResponse = soapConnection.call(createSOAPRequest(), url);

            // Process the SOAP Response
            printSOAPResponse(soapResponse);


            soapConnection.close();


        } catch (Exception e) {
            System.err.println("Error occurred while sending SOAP Request to Server");
            e.printStackTrace();
        }
    }


    private static SOAPMessage createSOAPRequest() throws Exception {


        MessageFactory messageFactory = MessageFactory.newInstance();
        
        SOAPMessage message = messageFactory.createMessage();
    
        SOAPHeader header = message.getSOAPHeader();
        SOAPBody body = message.getSOAPBody();
        SOAPPart part = message.getSOAPPart();
        
 
        
        
        SOAPEnvelope envelope = part.getEnvelope();
        
        String PREFERRED_PREFIX = "soapenv";
        String SOAP_ENV_NAMESPACE= "soapenv";
        
        MimeHeaders headers = message.getMimeHeaders();
        headers.addHeader("SOAPAction", "ZFmPruebaSumaWsdl");
        
        
        envelope.removeNamespaceDeclaration(envelope.getPrefix());
        envelope.addNamespaceDeclaration(PREFERRED_PREFIX, SOAP_ENV_NAMESPACE);
        envelope.setPrefix(PREFERRED_PREFIX);
        header.setPrefix(PREFERRED_PREFIX);
        body.setPrefix(PREFERRED_PREFIX);
        
        
        
        String username = "_USER_";
        String password= "_PASSWORD_";
        
        String userAndPassword = String.format("%s:%s",username, password);


        String basicAuth = Base64.getEncoder().encodeToString(userAndPassword.getBytes());
        MimeHeaders mimeHeaders = message.getMimeHeaders();
        mimeHeaders.addHeader("Authorization", "Basic " + basicAuth);
       
        String urn = "urn:sap-com:document:sap:soap:functions:mc-style";

        // SOAP Envelope
        envelope.addNamespaceDeclaration("urn", urn);
       
        QName pruebaSuma = new QName("urn:ZFmPruebaSumaWsdl");
        SOAPElement element = body.addChildElement(pruebaSuma);
        
        QName childName = new QName("ImVValor_1");
        SOAPElement item1 = element.addChildElement(childName);
        item1.addTextNode("1");
        
        QName childName2 = new QName("ImVValor_2");
        SOAPElement item2 = element.addChildElement(childName2);
        item2.addTextNode("2");


        
        message.saveChanges();
        System.out.print("Request SOAP Message = ");
        message.writeTo(System.out);
        System.out.println();
        System.out.print("Getting SOAP response");
        return message;

    }


    /**
     * Method used to print the SOAP Response
     */


    private static void printSOAPResponse(SOAPMessage soapResponse) throws Exception {

    System.out.print("Getting SOAP response");
        TransformerFactory transformerFactory = TransformerFactory.newInstance();
        Transformer transformer = transformerFactory.newTransformer();
        Source sourceContent = soapResponse.getSOAPPart().getContent();
        System.out.print("\nResponse SOAP Message = ");
        StreamResult result = new StreamResult(System.out);
        transformer.transform(sourceContent, result);
    }


}

It always returns error : com.sun.xml.internal.messaging.saaj.SOAPExceptionImpl: Bad response: (307Temporary Redirect

I would really appreciate any help.


and this is my wsdl

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:urn="urn:sap-com:document:sap:soap:functions:mc-style">
    <soapenv:Header>
      <urn:OTAuthentication>
         <urn:AuthenticationToken>TOKEN</urn:AuthenticationToken>
      </urn:OTAuthentication>
   </soapenv:Header>
   <soapenv:Body>
      <urn:ZFmPruebaSumaWsdl>
         <ImVValor_1>1</ImVValor_1>
         <ImVValor_2>2</ImVValor_2>
      </urn:ZFmPruebaSumaWsdl>
   </soapenv:Body>
</soapenv:Envelope>