I'm trying to send files with soap. but somehow I failed. Help me please gave me the codes below and the error I received.
public void SendTest() {
try {
String filePath = "C:/test/fb344a10-713a-4e45-8810-6a82237947af.zip";
String host = "https://test.efatura.gov.tr/earsiv/services/EArsivWsPort?wsdl";
SOAPConnectionFactory soapConnectionFactory = SOAPConnectionFactory.newInstance();
SOAPConnection connection = soapConnectionFactory.createConnection();
java.net.URL endpoint = new URL(host);
MessageFactory messageFactory = MessageFactory.newInstance();
SOAPMessage message = messageFactory.createMessage();
SOAPPart soapPart = message.getSOAPPart();
SOAPEnvelope soapEnvelope = soapPart.getEnvelope();
soapEnvelope.addNamespaceDeclaration("EArsivWsService", "http://earsiv.vedop3.ggm.gov.org/");
SOAPHeader header = message.getSOAPHeader();
SOAPBody body = message.getSOAPBody();
// QName qNameMethod=new QName("","sendDocumentFile","tns");
// SOAPBodyElement bodyElement = (SOAPBodyElement)
// body.addChildElement(qNameMethod);
SOAPFactory soapFactory = SOAPFactory.newInstance();
Name bodyName = soapFactory.createName("http://earsiv.vedop3.ggm.gov.org/", "tns", "sendDocumentFile");
body.addChildElement(new QName("name","fb344a10-713a-4e45-8810-6a82237947af.zip"));
URL url = new File(filePath).toURI().toURL();
DataHandler dataHandler = new DataHandler(url);
AttachmentPart attachment = message.createAttachmentPart(dataHandler);
// attachment.setContentId("");
message.addAttachmentPart(attachment);
message.saveChanges();
SOAPMessage response = connection.call(message, endpoint);
ByteArrayOutputStream bOutput = new ByteArrayOutputStream(8192);
response.writeTo(bOutput);
String strResponse = bOutput.toString();// TODO use encoding for turkish
System.out.println(strResponse);
} catch (SOAPException | IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
Error response:
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Body>
<soap:Fault>
<faultcode xmlns:ns1="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd">ns1:InvalidSecurity</faultcode>
<faultstring>An error was discovered processing the <wsse:Security> header</faultstring>
</soap:Fault>
</soap:Body>
</soap:Envelope>
SOAP message should be signed with Wss
Related
POST /webservices/producao/cdc/cdc.asmx HTTP/1.1
Host: www.soawebservices.com.br
Content-Type: text/xml; charset=utf-8
Content-Length: length
SOAPAction: "SOAWebServices/PessoaFisicaSimplificada"
<?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
<PessoaFisicaSimplificada xmlns="SOAWebServices">
<Credenciais>
<Email>string</Email>
<Senha>string</Senha>
</Credenciais>
<Documento>string</Documento>
<DataNascimento>string</DataNascimento>
</PessoaFisicaSimplificada>
/soap:Body
/soap:Envelope
i have this code i been trying to build:
public void start(){
try {
SOAPConnectionFactory soapConnectionFactory = SOAPConnectionFactory.newInstance();
SOAPConnection soapConnection = soapConnectionFactory.createConnection();
String url = "www.soawebservices.com.br";
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 = "SOAWebServices/PessoaFisicaSimplificada";
SOAPEnvelope envelope = soapPart.getEnvelope();
envelope.addNamespaceDeclaration("soap", serverURI);
SOAPBody soapBody = envelope.getBody();
SOAPElement soapBodyElem = soapBody.addChildElement("PessoaFisicaSimplificada", "","SOAWebServices");
SOAPElement soapBodyElemC = soapBodyElem.addChildElement("Credenciais");
SOAPElement soapBodyElem1 = soapBodyElemC.addChildElement("Email");
soapBodyElem1.addTextNode("EMAIL");
SOAPElement soapBodyElem2 = soapBodyElemC.addChildElement("Senha");
soapBodyElem2.addTextNode("PASSWORD");
SOAPElement soapBodyElem3 = soapBodyElem.addChildElement("Documento");
soapBodyElem3.addTextNode("CPF");
SOAPElement soapBodyElem4 = soapBodyElem.addChildElement("DataNascimento");
soapBodyElem4.addTextNode("Date");
MimeHeaders headers = soapMessage.getMimeHeaders();
headers.addHeader("SOAPAction", serverURI);
soapMessage.saveChanges();
System.out.print("Request SOAP Message = ");
soapMessage.writeTo(System.out);
System.out.println();
return soapMessage;
}
however it throws: (javax.xml.soap.SOAPException: JBWS024004: SOAP message could not be sent), i'd guess it's either the uri or the url that i'm passing wrong or i'm not creating the header correctly
the current request it's producing:
SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:soap="SOAWebServices/PessoaFisicaSimplificada"
SOAP-ENV:Header/
SOAP-ENV:Body
<PessoaFisicaSimplificada xmlns="SOAWebServices">
<Credenciais>
<Email>EMAIL</Email>
<Senha>PASSWORD</Senha>
</Credenciais>
<Documento>CPF</Documento>
<DataNascimento>Date</DataNascimento>
</PessoaFisicaSimplificada>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>
Ok, it seems you are not providing SOAP Endpoint correctly.
Find below working code. Have just specified full SOAP Endpoint.
public static void main(String[] args) {
try {
SOAPConnectionFactory soapConnectionFactory = SOAPConnectionFactory.newInstance();
SOAPConnection soapConnection = soapConnectionFactory.createConnection();
String url = "http://www.soawebservices.com.br/webservices/producao/cdc/cdc.asmx";
SOAPMessage soapResponse = soapConnection.call(createSOAPRequest(), url);
soapResponse.writeTo(System.out);
//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 = "SOAWebServices/PessoaFisicaSimplificada";
SOAPEnvelope envelope = soapPart.getEnvelope();
envelope.addNamespaceDeclaration("soap", serverURI);
SOAPBody soapBody = envelope.getBody();
SOAPElement soapBodyElem = soapBody.addChildElement("PessoaFisicaSimplificada", "","SOAWebServices");
SOAPElement soapBodyElemC = soapBodyElem.addChildElement("Credenciais");
SOAPElement soapBodyElem1 = soapBodyElemC.addChildElement("Email");
soapBodyElem1.addTextNode("EMAIL");
SOAPElement soapBodyElem2 = soapBodyElemC.addChildElement("Senha");
soapBodyElem2.addTextNode("PASSWORD");
SOAPElement soapBodyElem3 = soapBodyElem.addChildElement("Documento");
soapBodyElem3.addTextNode("CPF");
SOAPElement soapBodyElem4 = soapBodyElem.addChildElement("DataNascimento");
soapBodyElem4.addTextNode("Date");
MimeHeaders headers = soapMessage.getMimeHeaders();
headers.addHeader("SOAPAction", serverURI);
soapMessage.saveChanges();
System.out.print("Request SOAP Message = ");
soapMessage.writeTo(System.out);
System.out.println();
return soapMessage;
}
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 wrote method, which generate soap message from java string:
private SOAPMessage createRequest(String msg) {
SOAPMessage request = null;
try {
MessageFactory msgFactory = MessageFactory.newInstance();
request = factory.createMessage();
SOAPPart msgPart = request.getSOAPPart();
SOAPEnvelope envelope = msgPart.getEnvelope();
SOAPBody body = envelope.getBody();
StreamSource _msg = new StreamSource(new StringReader(msg));
msgPart.setContent(_msg);
request.saveChanges();
} catch(Exception ex) {
ex.printStackTrace();
}
}
And, after that, I try generate some message. For example:
createRequest("test message");
But here - request.saveChanges(); I catch this exception:
com.sun.xml.internal.messaging.saaj.SOAPExceptionImpl: Error during saving a multipart message
Where is my mistake?
That is because you are not passing a correct protocol formatted message.
Your code doesn't specify which SOAP protocol you want to use, that means it creates a message factory for SOAP 1.1 messages.
Thus, you would need to pass a correct SOAP1.1 message.
I replicated your method like this:
private static SOAPMessage createRequest(String msg) {
SOAPMessage request = null;
try {
MessageFactory msgFactory = MessageFactory
.newInstance(SOAPConstants.SOAP_1_1_PROTOCOL);
request = msgFactory.createMessage();
SOAPPart msgPart = request.getSOAPPart();
SOAPEnvelope envelope = msgPart.getEnvelope();
SOAPBody body = envelope.getBody();
javax.xml.transform.stream.StreamSource _msg = new javax.xml.transform.stream.StreamSource(
new java.io.StringReader(msg));
msgPart.setContent(_msg);
request.saveChanges();
} catch (Exception ex) {
ex.printStackTrace();
}
return request;
}
and I call it using this string:
String soapMessageString = "<SOAP-ENV:Envelope xmlns:SOAP-ENV=\"http://schemas.xmlsoap.org/soap/envelope/\"><SOAP-ENV:Header/><SOAP-ENV:Body></SOAP-ENV:Body></SOAP-ENV:Envelope>";
createRequest(soapMessageString);
and It works.
I am trying to add a XML Declaration for ex: <?xml version="1.0" encoding="utf-8"?> to the top of this soap message. Can anyone assist me on how this can be done?
try {
// 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);
// 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();
SOAPHeader header = soapMessage.getSOAPHeader();
You need to set a property to the SOAP message.
soapMessage.setProperty(SOAPMessage.WRITE_XML_DECLARATION, "true")
The above code should do that.
Here is the link to setProperty(String property, Object value) documentation.
soapMessage.setProperty(SOAPMessage.CHARACTER_SET_ENCODING, "UTF-8");
soapMessage.setProperty(SOAPMessage.WRITE_XML_DECLARATION, "true");
I am working on SOAP client. My WSDL URL is http://localhost:8080/soap/getMessage?wsdl.
This requires the the following header to specify the username and password.
<wsdl:Envelope xmlns:soap="..."
xmlns:wsse="..." >
<wsdl:Header>
<wsse:Security>
<wsse:UsernameToken>
<wsse:Username>admin</wsse:Username>
<wsse:Password>password</wsse:Password>
</wsse:UsernameToken>
</wsse:Security>
</wsdl:Header>
</wsdl:Envelope>
I have to write a program for it.
Can some one help me.
Thanks.
here is my past program for soap. I already modified it to your case.
//create SOAP
SOAPConnectionFactory sfc = SOAPConnectionFactory.newInstance();
SOAPConnection connection = sfc.createConnection();
SOAPMessage soapMessage = MessageFactory.newInstance().createMessage();
SOAPPart soapPart = soapMessage.getSOAPPart();
SOAPEnvelope soapEnvelope = soapPart.getEnvelope();
SOAPBody soapBody = soapEnvelope.getBody();
SOAPElement Header = soapBody.addBodyElement(new QName("Header"));
//attribute
SOAPElement Security= Header.addChildElement(new QName("Security"));
SOAPElement UsernameToken= Security.addChildElement(new QName("UsernameToken"));
SOAPElement Username= UsernameToken.addChildElement(new QName("Username"));
SOAPElement Password= UsernameToken.addChildElement(new QName("Password"));
//enter the username and password
Username.addTextNode("username");
Password.addTextNode("password");
//send the soap and print out the result
URL endpoint = "http://localhost:8080/soap/getMessage?wsdl";
SOAPMessage response = connection.call(soapMessage, endpoint);
ByteArrayOutputStream out = new ByteArrayOutputStream();
String xml = "";
try {
response.writeTo(out);
xml = out.toString("UTF-8");
} catch (Exception e)
{
System.out.println(""+e);
//log.error(e.getMessage(),e);
}
System.out.println(""+xml);
for further information you can search the google for using SOAP in JDK 1.6