Question

I have a working soaprequest in PhP and i'm trying to create a java program that requires the same call, However i am really struggeling to find the way to create the below php code in java, i have found numerus websites explaining soap requests in java but i cant seem to work how how to send the $param_auth array.

Any help would be most appreciated as i've been stuck on this for a while.

Thanks in advance.

$param_auth=array(  
 'user'=>$username,
 'id'=>$userID,
 'message'=>$userMessage
);
$soapclient = new soapclient(WebsiteAddress);   
$data->_db = $soapclient->call('uploadMessage',$param_auth);
Was it helpful?

Solution

Finally solved my issue (Ive changed Element names etc.), using eclipse shows you the Soap envelope and XML response which i found useful for debugging the envelope). Hope this helps anyone trying to do similar.

The "UploadMessage" is the "Login" string and the

$param_auth=array(  
 'user'=>$username,
 'id'=>$userID,
 'message'=>$userMessage
);

is the Username and Password, (Left out the Message part).

This code sends an Envelope like this the server:-

    <SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:Login="Your URL"><SOAP-ENV:Header/><SOAP-ENV:Body><Login><UserName>YourUserName</UserName><Password>YourPassword</Password></Login></SOAP-ENV:Body></SOAP-ENV:Envelope>

Code To create the above Envelope is below:-

import javax.xml.soap.*;
import javax.xml.transform.*;
import javax.xml.transform.stream.*;

public class SoapCall  {

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

        // Send SOAP Message to SOAP Server.
        String url = "Your URL";
        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 {

    String YourUsername = "UserName";
    String YourPassword = "Password";

    MessageFactory messageFactory = MessageFactory.newInstance();
    SOAPMessage soapMessage = messageFactory.createMessage();
    SOAPPart soapPart = soapMessage.getSOAPPart();

    String serverURI = "Your URL";

    // SOAP Envelope
    SOAPEnvelope envelope = soapPart.getEnvelope();
    envelope.addNamespaceDeclaration("Login", serverURI);


    SOAPBody soapBody = envelope.getBody();

    SOAPElement soapBodyElem = soapBody.addChildElement("Login");

    SOAPElement soapBodyElem2 = soapBodyElem.addChildElement("UserName");
    soapBodyElem2.addTextNode(YourUserName);
    SOAPElement soapBodyElem3 = soapBodyElem.addChildElement("Password");
    soapBodyElem3.addTextNode(YourPassword);

    MimeHeaders headers = soapMessage.getMimeHeaders();
    headers.addHeader("SOAPAction", serverURI  + "Login");

    soapMessage.saveChanges();

    /* Print the request message */
    System.out.print("Request SOAP Message = ");
    soapMessage.writeTo(System.out);
    System.out.println();

    return soapMessage;
}

/**
 * Method used to print the SOAP Response
 */
private static void printSOAPResponse(SOAPMessage soapResponse) throws Exception {
    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);
}

}

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top