I am doing a project which has to access to a given web service and send data to requested parameters and get the response from service. Currently I do something which I found from a forum and it parse the data to service, but unable to get the response since its giving an error.
My sample code is below.
package webserviceexample;
import javax.xml.soap.*;
import javax.xml.transform.*;
import javax.xml.transform.stream.*;
public class aaaa {
public static void main(String args[]) {
try {
SOAPConnectionFactory soapConnectionFactory = SOAPConnectionFactory.newInstance();
SOAPConnection soapConnection = soapConnectionFactory.createConnection();
String url = "https://vlmt.XXX.lk:0111/VendorRecharge/services/RechargeVendorService/wsdl/";
SOAPMessage soapResponse = soapConnection.call(createSOAPRequest(), url);
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 soapMessage = messageFactory.createMessage();
SOAPPart soapPart = soapMessage.getSOAPPart();
String serverURI = "http://vendor.XXX.ZZZ.com/";
SOAPEnvelope envelope = soapPart.getEnvelope();
envelope.addNamespaceDeclaration("wsdl", serverURI);
SOAPBody soapBody1 = envelope.getBody();
SOAPElement soapBodyElemX = soapBody1.addChildElement("performRecharge", "wsdl");
SOAPElement soapBodyElem1 = soapBodyElemX.addChildElement("storeId", "wsdl");
soapBodyElem1.addTextNode("LA19BNPX");
SOAPElement soapBodyElem2 = soapBodyElemX.addChildElement("password", "wsdl");
soapBodyElem2.addTextNode("thilanka#456A");
SOAPElement soapBodyElem3 = soapBodyElemX.addChildElement("mobileNo", "wsdl");
soapBodyElem3.addTextNode("751238456");
SOAPElement soapBodyElem4 = soapBodyElemX.addChildElement("Amount", "wsdl");
soapBodyElem4.addTextNode("12");
SOAPElement soapBodyElem5 = soapBodyElemX.addChildElement("vendorTransactionId", "wsdl");
soapBodyElem5.addTextNode("15");
MimeHeaders headers = soapMessage.getMimeHeaders();
headers.addHeader("performRecharge", serverURI + "performRecharge");
System.out.println("");
soapMessage.saveChanges();
System.out.print("Request SOAP Message = ");
soapMessage.writeTo(System.out);
System.out.println();
return soapMessage;
}
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);
}
}
The error I am getting is:
Request SOAP Message
<SOAP-ENV:Envelope xmlns:SOAP- ENV="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:wsdl="http://vendor.virtualization.ibm.com/">
<SOAP-ENV:Header/>
<SOAP-ENV:Body>
<wsdl:performRecharge>
<wsdl:storeId>LA19BNPX</wsdl:storeId>
<wsdl:password>thilanka#456A</wsdl:password>
<wsdl:mobileNo>751238456</wsdl:mobileNo>
<wsdl:Amount>12</wsdl:Amount>
<wsdl:vendorTransactionId>15</wsdl:vendorTransactionId>
</wsdl:performRecharge>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>
Response (error)
<?xml version="1.0" encoding="UTF-8"?><soapenv:Envelope
xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<soapenv:Header/>
<soapenv:Body>
<soapenv:Fault xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
<faultcode>soapenv:Server.generalException</faultcode>
<faultstring>javax.xml.rpc.JAXRPCException: WSWS3122E: Error: Could not find service services/RechargeVendorService/wsdl/ referenced in URI /VendorRecharge/services/RechargeVendorService/wsdl/</faultstring>
</soapenv:Fault>
</soapenv:Body>
</soapenv:Envelope>
I have to call the "performRecharge" in the web service. Please help me.
A summary of the web service is below.
Please help me overcome this. It's very important.
Thank you.
WSDL FILE
Make sure that this is the correct service URL.
Seems like you specified the wsdl location and not registered service url. Set the request url specified in the wsdl file for the service.
String url = "https://vlmt.XXX.lk:0111/VendorRecharge/services/RechargeVendorService/wsdl/";
See the location tag in your wsdl
<service name="Hello_Service">
<documentation>WSDL File for HelloService</documentation>
<port binding="tns:Hello_Binding" name="Hello_Port">
<soap:address
location="**http://www.examples.com/SayHello/**">
</port>
</service>
Related
I am new to SOAP. I want to create a soap request of given below xml.
xml
<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<soap:Body>
<SendRequest xmlns="http://tempuri.org/">
<request xsi:type="RegisterCheckRequest" Id="7a646d45-ee2f-4b1c-8de8-780c416fbbd0" Service="42" xmlns="http://paygo24.com/v3/protocol">
<PaymentParameters xmlns="">
<Parameter Name="account" Value="08374829" />
</PaymentParameters>
</request>
<pointId>46</pointId>
<password>4QrcOUm6Wau+VuBX8g+IPg==</password>
</SendRequest>
</soap:Body>
</soap:Envelope>
I was using below sample java file, to create soap request but I am not able to do so, can anyone give me support.
Sample Java File to create soap request
import javax.xml.soap.*;
public class SOAPClientSAAJ {
public static void main(String args[]) throws Exception {
// Create SOAP Connection
SOAPConnectionFactory soapConnectionFactory = SOAPConnectionFactory.newInstance();
SOAPConnection soapConnection = soapConnectionFactory.createConnection();
// Send SOAP Message to SOAP Server
String url = "http://ws.cdyne.com/emailverify/Emailvernotestemail.asmx";
SOAPMessage soapResponse = soapConnection.call(createSOAPRequest(), url);
// print SOAP Response
System.out.print("Response SOAP Message:");
soapResponse.writeTo(System.out);
soapConnection.close();
}
private static SOAPMessage createSOAPRequest() throws Exception {
MessageFactory messageFactory = MessageFactory.newInstance();
SOAPMessage soapMessage = messageFactory.createMessage();
SOAPPart soapPart = soapMessage.getSOAPPart();
String serverURI = "http://ws.cdyne.com/";
// SOAP Envelope
SOAPEnvelope envelope = soapPart.getEnvelope();
envelope.addNamespaceDeclaration("example", serverURI);
/*
Constructed SOAP Request Message:
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:example="http://ws.cdyne.com/">
<SOAP-ENV:Header/>
<SOAP-ENV:Body>
<example:VerifyEmail>
<example:email>mutantninja#gmail.com</example:email>
<example:LicenseKey>123</example:LicenseKey>
</example:VerifyEmail>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>
*/
// SOAP Body
SOAPBody soapBody = envelope.getBody();
SOAPElement soapBodyElem = soapBody.addChildElement("VerifyEmail", "example");
SOAPElement soapBodyElem1 = soapBodyElem.addChildElement("email", "example");
soapBodyElem1.addTextNode("mutantninja#gmail.com");
SOAPElement soapBodyElem2 = soapBodyElem.addChildElement("LicenseKey", "example");
soapBodyElem2.addTextNode("123");
MimeHeaders headers = soapMessage.getMimeHeaders();
headers.addHeader("SOAPAction", serverURI + "VerifyEmail");
soapMessage.saveChanges();
/* Print the request message */
System.out.print("Request SOAP Message:");
soapMessage.writeTo(System.out);
System.out.println();
return soapMessage;
}
}
This is how you build soap request
private static SOAPMessage createSOAPRequest() throws Exception {
MessageFactory messageFactory = MessageFactory.newInstance();
SOAPMessage soapMessage = messageFactory.createMessage();
SOAPPart soapPart = soapMessage.getSOAPPart();
SOAPEnvelope envelope = soapPart.getEnvelope();
envelope.removeNamespaceDeclaration(envelope.getPrefix());
envelope.addNamespaceDeclaration("soap","http://schemas.xmlsoap.org/soap/envelope/");
envelope.setPrefix("soap");
envelope.addNamespaceDeclaration("xsi","http://www.w3.org/2001/XMLSchema-instance");
envelope.addNamespaceDeclaration("xsd","http://www.w3.org/2001/XMLSchema");
SOAPHeader header=soapMessage.getSOAPHeader();
header.setPrefix("soap");
SOAPBody soapBody = envelope.getBody();
soapBody.setPrefix("soap");
SOAPElement root=soapBody.addChildElement(new QName("http://tempuri.org/","SendRequest"));
SOAPElement request=root.addChildElement(new QName("http://paygo24.com/v3/protocol","request"));
request.setAttribute("xsi:type", "RegisterCheckRequest");
request.setAttribute("Id","7a646d45-ee2f-4b1c-8de8-780c416fbbd0");
request.setAttribute("Service","42");
SOAPElement paymentParameters =request.addChildElement(new QName(" ","PaymentParameters"));
SOAPElement parameter=paymentParameters.addChildElement("Parameter");
parameter.setAttribute("Name","account");
parameter.setAttribute("Value", "08374829");
root.addChildElement("pointId").setValue("46");
root.addChildElement("password").setValue("4QrcOUm6Wau+VuBX8g+IPg==");
soapMessage.saveChanges();
soapMessage.writeTo(System.out);
return soapMessage;
}
You can generate XML from objects using annotations to tie class members to XML elements/attributes. A popular way is using JAX-B. See this tutorial: http://www.vogella.com/tutorials/JAXB/article.html
Okay, so I'm totally new to webservices and for a project I'm working on I'm trying to wrap my head around the whole SOAP thing. I think I have a vague understanding of what is going on, but I'm missing some specific information and I simply can't find anything helpful by googling.
I've read the questions others asked like this one SOAP request to WebService with java but I still can't fully figure out what is going on.
Specifically I'm trying to use the service provided here http://ec.europa.eu/taxation_customs/vies/vatRequest.html with its wsdl file here http://ec.europa.eu/taxation_customs/vies/checkVatService.wsdl
I tried adapting the example given in the above question, but I just can't figure out what values to add where so it works with this specific service. All I get is a "405 Method not allowed" response. Here is my attempted adaption:
package at.kmds.soaptest;
import javax.xml.soap.*;
import javax.xml.transform.Source;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.stream.StreamResult;
public class Main {
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 = "http://ec.europa.eu/taxation_customs/vies/vatRequest.html";
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 soapMessage = messageFactory.createMessage();
SOAPPart soapPart = soapMessage.getSOAPPart();
String serverURI = "http://ec.europa.eu/";
// SOAP Envelope
SOAPEnvelope envelope = soapPart.getEnvelope();
envelope.addNamespaceDeclaration("example", serverURI);
// SOAP Body
SOAPBody soapBody = envelope.getBody();
SOAPElement soapBodyElem = soapBody.addChildElement("checkVat");
SOAPElement soapBodyElem1 = soapBodyElem.addChildElement("countryCode");
soapBodyElem1.addTextNode("...");
SOAPElement soapBodyElem2 = soapBodyElem.addChildElement("vatNumber");
soapBodyElem2.addTextNode("...");
MimeHeaders headers = soapMessage.getMimeHeaders();
headers.addHeader("SOAPAction", serverURI + "checkVat");
soapMessage.saveChanges();
/* Print the request message */
System.out.print("Request SOAP Message = ");
soapMessage.writeTo(System.out);
System.out.println();
return soapMessage;
}
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);
}
}
If someone could explain to me what exactly I'm doing wrong and how to fix it or maybe even give me a working example I'd be eternally grateful...
The code has to be slightly modified to hit the service.
String url = "http://ec.europa.eu/taxation_customs/vies/services/checkVatService";
is the endpoint you have to hit (this is from the wsdl)
<wsdlsoap:address location="http://ec.europa.eu/taxation_customs/vies/services/checkVatService"/>
Note that when i hit this , i get a soap fault.Looks like the SOAPBody constructed will have to be checked again.
Request SOAP Message = <SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:example="http://ec.europa.eu/"><SOAP-ENV:Header/><SOAP-ENV:Body><checkVat><countryCode>...</countryCode><vatNumber>...</vatNumber></checkVat></SOAP-ENV:Body></SOAP-ENV:Envelope>
Response SOAP Message = <?xml version="1.0" encoding="UTF-8"?><soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"><soap:Body><soap:Fault><faultcode>soap:Client</faultcode><faultstring>Unexpected wrapper element checkVat found. Expected {urn:ec.europa.eu:taxud:vies:services:checkVat:types}checkVat.</faultstring></soap:Fault></soap:Body></soap:Envelope>
Edit a full program that works (looks like) , gives me invalid input because i am passing dots (...).
import javax.xml.namespace.QName;
import javax.xml.soap.*;
import javax.xml.transform.Source;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.stream.StreamResult;
public class Main {
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 = "http://ec.europa.eu/taxation_customs/vies/services/checkVatService";
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 soapMessage = messageFactory.createMessage();
SOAPPart soapPart = soapMessage.getSOAPPart();
String serverURI = "http://ec.europa.eu/";
// SOAP Envelope
SOAPEnvelope envelope = soapPart.getEnvelope();
envelope.addNamespaceDeclaration("tns1", "urn:ec.europa.eu:taxud:vies:services:checkVat:types");
envelope.addNamespaceDeclaration("impl", "urn:ec.europa.eu:taxud:vies:services:checkVat");
// SOAP Body
SOAPBody soapBody = envelope.getBody();
QName bodyQName = new QName("urn:ec.europa.eu:taxud:vies:services:checkVat:types",
"checkVat", "tns1");
SOAPElement soapBodyElem = soapBody.addChildElement(bodyQName);
SOAPElement soapBodyElem1 = soapBodyElem.addChildElement(new QName("urn:ec.europa.eu:taxud:vies:services:checkVat:types",
"countryCode", "tns1"));
soapBodyElem1.addTextNode("...");
SOAPElement soapBodyElem2 = soapBodyElem.addChildElement(new QName("urn:ec.europa.eu:taxud:vies:services:checkVat:types",
"vatNumber", "tns1"));
soapBodyElem2.addTextNode("...");
MimeHeaders headers = soapMessage.getMimeHeaders();
headers.addHeader("SOAPAction", serverURI + "checkVat");
soapMessage.saveChanges();
/* Print the request message */
System.out.print("Request SOAP Message = ");
soapMessage.writeTo(System.out);
System.out.println();
return soapMessage;
}
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);
}
}
Gives back
<?xml version="1.0" encoding="UTF-8"?>
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Body>
<soap:Fault>
<faultcode>soap:Server</faultcode>
<faultstring>INVALID_INPUT</faultstring>
</soap:Fault>
</soap:Body>
</soap:Envelope>
I'm trying to interact with a SOAP service.
I'm able to get the SOAP response and by using Source sourceContent = soapResponse.getSOAPPart().getContent(); and transformer.transform(sourceContent, result); , I'm able to see what the output/response is and displaying it in the console.
But, I need to extract sessionID from the response and send that sessionID in a different SOAP request.
Please suggest me the extraction method, building a new SOAP request
Parsing is what I need to do!!
Below is the code for sending the request to the SOAP service:
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 = "https://WWW.DUMMYURL.COM";
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 soapMessage = messageFactory.createMessage();
SOAPPart soapPart = soapMessage.getSOAPPart();
String serverURN = "urn:DUMMYURL.COM";
String serverNS0 = "http://WWW.DUMMYURL.COM";
// SOAP Envelope
SOAPEnvelope envelope = soapPart.getEnvelope();
envelope.addNamespaceDeclaration("urn", serverURN);
envelope.addNamespaceDeclaration("ns0", serverNS0);
// SOAP Body
SOAPBody soapBody = envelope.getBody();
SOAPElement soapBodyElem1 = soapBody.addChildElement("login","urn");
SOAPElement soapBodyElem2 = soapBodyElem1.addChildElement("username","urn");
#SuppressWarnings("unused")
SOAPElement soapBodyElem3 = soapBodyElem2.addTextNode("USERNAME");
SOAPElement soapBodyElem4 = soapBodyElem1.addChildElement("password","urn");
#SuppressWarnings("unused")
SOAPElement soapBodyElem5 = soapBodyElem4.addTextNode("PASSWORD");
MimeHeaders headers = soapMessage.getMimeHeaders();
headers.addHeader("SOAPAction", "https://WWW.DUMMYURL.COM" + "login");
soapMessage.saveChanges();
//Print the request message
System.out.print("Request SOAP Message = ");
soapMessage.writeTo(System.out);
System.out.println();
return soapMessage;
}
private static void printSOAPResponse(SOAPMessage soapResponse) throws Exception {
TransformerFactory transformerFactory = TransformerFactory.newInstance();
Transformer transformer = transformerFactory.newTransformer();
Source sourceContent = soapResponse.getSOAPPart().getContent();
String source = sourceContent.toString();
System.out.print("\nResponse SOAP Message = ");
// Format it
transformer.setOutputProperty(OutputKeys.INDENT, "yes");
transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2");
StreamResult result = new StreamResult(System.out);
transformer.transform(sourceContent, result);
System.out.println(transformer.toString());
}
Can someone please suggest me a snippet on how should I save the response I'm getting to a file locally?
Currently in the above code the response is getting displayed in the console.
You can use the writeTo method of the SOAPMessage interface to do the same, for example:
FileOutputStream out = new FileOutputStream("somefile");
soapResponse.writeTo(out);
Vinod.
I'm trying to call a SOAP webservice using SAAJ for this WSDL. I submitted the request for this WSDL here http://www.webservicex.net/ws/WSDetails.aspx?WSID=64and it generated the following SOAP requests..
<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Body>
<GetGeoIP xmlns="http://www.webservicex.net/">
<IPAddress>string</IPAddress>
</GetGeoIP>
</soap:Body>
</soap:Envelope>
Using SAAJ I generated the same SOAP requests and submitted it but get this error - Server did not recognize the value of HTTP Header SOAPAction: .
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema"><SOAP-ENV:Header/>
<SOAP-ENV:Body>
<GetGeoIP xmlns="http://www.webservicex.net/">
<IPAddress>SUNW</IPAddress>
</GetGeoIP>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>
Java Code:
package newpackage;
import javax.xml.namespace.QName;
import javax.xml.soap.*;
public class SOAPClientSAAJ {
public static void main(String args[]) throws Exception {
// Create SOAP Connection
SOAPConnectionFactory soapConnectionFactory = SOAPConnectionFactory.newInstance();
SOAPConnection soapConnection = soapConnectionFactory.createConnection();
// Send SOAP Message to SOAP Server
String url = "http://www.webservicex.net/geoipservice.asmx";
SOAPMessage soapResponse = soapConnection.call(createSOAPRequest(), url);
// print SOAP Response
System.out.print("Response SOAP Message:");
soapResponse.writeTo(System.out);
soapConnection.close();
}
private static SOAPMessage createSOAPRequest() throws Exception {
MessageFactory messageFactory = MessageFactory.newInstance();
SOAPMessage soapMessage = messageFactory.createMessage();
SOAPPart soapPart = soapMessage.getSOAPPart();
String serverURI = "http://www.w3.org/2001/XMLSchema";
// SOAP Envelope
SOAPEnvelope envelope = soapPart.getEnvelope();
envelope.addNamespaceDeclaration("xsd", serverURI);
SOAPBody body = soapMessage.getSOAPBody();
QName bodyName = new QName("http://www.webservicex.net/", "GetGeoIP" );
SOAPBodyElement bodyElement = body.addBodyElement(bodyName);
QName name = new QName("IPAddress");
SOAPElement symbol = bodyElement.addChildElement(name);
symbol.addTextNode("SUNW");
soapMessage.saveChanges();
/* Print the request message */
System.out.print("Request SOAP Message:");
soapMessage.writeTo(System.out);
System.out.println();
return soapMessage;
}
}
Add
MimeHeaders headers = soapMessage.getMimeHeaders();
headers.addHeader("SOAPAction", "http://www.webservicex.net/" + "GetGeoIP");
If you are using Marshelling and Unmarsheling, Just implement WebseviceMessageCallback interface like:
public class SOAPConnector extends WebServiceGatewaySupport
{
public Object callWebService( String url, Object request )
{
return getWebServiceTemplate().marshalSendAndReceive( url, request,
webServiceMessage -> {
(( SoapMessage )webServiceMessage).setSoapAction(
"https://www.w3schools.com/xml/CelsiusToFahrenheit" );
} );
}
}
Otherwise proceed with: https://stackoverflow.com/a/26253141/6097074
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);
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);
}
}