cancel
Showing results for 
Search instead for 
Did you mean: 

ISSUE CONSUMING WEB SERVICE SAP FROM JAVA

Former Member
0 Kudos

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>

former_member189220
Active Contributor
0 Kudos

Hello Juan,

If the initial question, in this forum thread, is answered from your perspective, then please click on the “Accept” link to complete this question. More details here.

https://blogs.sap.com/2017/08/15/what-to-do-with-my-answered-questions/

Otherwise, please post more details on the present impediment you do face.

Thank you, in advance, for your contribution to the better administration of the SAP Community!

Regards,

Accepted Solutions (0)

Answers (1)

Answers (1)

former_member189220
Active Contributor
0 Kudos

Helllo Juan,

1.

I do not see defined endpoints in the WSDL declaration, you have posted. In case this web service will be deployed on NW AS Java, in the WSDL definition there should be included WS endpoints.

2.

What part of the WS call is to the AS Java? Where is the WS client and where is the WS provider?

Provided the WS Provider is on the NW AS Java, where (or how) is this web service configured:

* in the Single Service Administration;

* in Group Services

* or in Destinations.

Regards,