How can I get userID and password tag name and value from soap request Header.
My request xml
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ws="http://WS.com/">
<soapenv:Header>
<userID>34</userID>
<password>test</password>
</soapenv:Header>
<soapenv:Body>
</soapenv:Body>
</soapenv:Envelope>
My java code
MessageFactory messageFactory = MessageFactory.newInstance();
SOAPMessage message = messageFactory.createMessage();
SOAPPart soapPart = message.getSOAPPart();
SOAPEnvelope envelope = soapPart.getEnvelope();
SOAPHeader header = envelope.getHeader();
i want to get userID and password to validate the request.
Please help
Thanks
Try using this code:
// raw SOAP input as String
String input = "<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:ws=\"http://WS.com/\">"
+ "<soapenv:Header>"
+ "<userID>34</userID>"
+ "<password>test</password>"
+ "</soapenv:Header>"
+ "<soapenv:Body>"
+ "</soapenv:Body>"
+ "</soapenv:Envelope>";
// Use MessageFactory with raw input as byte array
InputStream is = new ByteArrayInputStream(input.getBytes());
SOAPMessage message = MessageFactory.newInstance().createMessage(null, is);
SOAPPart soapPart = message.getSOAPPart();
SOAPEnvelope envelope = soapPart.getEnvelope();
SOAPHeader header = envelope.getHeader();
// obtain all Nodes tagged 'userID' or 'password'
NodeList userIdNode = header.getElementsByTagNameNS("*", "userID");
NodeList passwordNode = header.getElementsByTagNameNS("*", "password");
// extract the username and password
String userId = userIdNode.item(0).getChildNodes().item(0).getNodeValue();
String password = passwordNode.item(0).getChildNodes().item(0).getNodeValue();
System.out.println("userID: " + userId);
System.out.println("password: " + password);
Output:
userID: 34
password: test
Related
I want to consume Business Central Web service so I can add it to an existing web application.
I have tried:
Testing with Postman using Authorization header (works)
Testing SoapUI (works)
But I am getting this error:
com.sun.xml.messaging.saaj.client.p2p.HttpSOAPConnection post
SEVERE: SAAJ0008: Bad Response; Unauthorized
Exception in thread "main" com.sun.xml.messaging.saaj.SOAPExceptionImpl: java.security.PrivilegedActionException: com.sun.xml.messaging.saaj.SOAPExceptionImpl: Bad response: (401Unauthorized
at com.sun.xml.messaging.saaj.client.p2p.HttpSOAPConnection.call(HttpSOAPConnection.java:171)
Code:
String webPage = "https://api.businesscentral.dynamics.com/v2.0/<tenant>/...";
String name = "username";
String password = "password";
String authString = name + ":" + password;
String authEncBytes = new String(Base64.getEncoder().encode(authString.getBytes(StandardCharsets.UTF_8)));
String authStringEnc = new String(authEncBytes);
URL url = new URL(webPage);
URLConnection urlConnection = url.openConnection();
urlConnection.setRequestProperty("Authorization", "Basic " + authStringEnc);
MessageFactory messageFactory = MessageFactory.newInstance();
SOAPMessage soapMessage = messageFactory.createMessage();
SOAPPart soapPart = soapMessage.getSOAPPart();
// SOAP Envelope
SOAPEnvelope envelope = soapPart.getEnvelope();
envelope.getHeader().detachNode();
envelope.addHeader();
MimeHeaders hd = soapMessage.getMimeHeaders();
hd.addHeader("Authorization", "Basic " + authEncBytes);
SOAPBody soapBodytxaber = envelope.getBody();
envelope.addNamespaceDeclaration("myNamespace", "uri");
SOAPElement soapBodyElem0txa = soapBodytxaber.addChildElement("Read", "myNamespace");
SOAPElement soapBodyElem1txa = soapBodyElem0txa.addChildElement("No", "myNamespace");
soapBodyElem1txa.addTextNode("00000058");
SOAPConnectionFactory soapConnectionFactory = OAPConnectionFactory.newInstance();
SOAPConnection soapConnection = soapConnectionFactory.createConnection();
soapMessage.saveChanges();
SOAPMessage soapResponse = soapConnection.call(soapMessage, url);
You need to make sure that the user you are trying to authenticate with has a Web Services Access Key.
Then you must use the Web Services Access Key as the password when authenticating.
It does also appear that you are missing a space after Basic in this line of code:
hd.addHeader("Authorization", "Basic" + authEncBytes);
It should probably be
hd.addHeader("Authorization", "Basic " + authEncBytes);
SOLUTION
Okay so after receiving a course, they told me that Basic Authorization is deprecated and they use now OAuth 2...
Thanks for replying
Greetings
I need to generate a Soap request in Java. This is the xml file that I need to generate and pass through:
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:ns="website"
xmlns:com="website/Common"
xmlns:xm="http://www.w3.org/2005/05/xmlmime">
<soapenv:Header/>
<soapenv:Body>
<ns:RequestName>
<ns:model>
<ns:keys query="myquery;" ></ns:keys>
<ns:instance></ns:instance>
</ns:model>
</ns:RequestName>
</soapenv:Body>
</soapenv:Envelope>
I am aware that there are other methods of doing this, such as wsimport, but I would like to know how to do it this way. My this way way, I mean what is the correct Java syntax in create the xml file for a Soap Request. Here is some very basic syntax:
SOAPMessage message = messageFactory.createMessage();
SOAPHeader header = message.getSOAPHeader();
SOAPBody body = message.getSOAPBody();
// Here is the XML it produces:
<SOAP-ENV:Envelope
xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/">
<SOAP-ENV:Header/>
<SOAP-ENV:Body>
...
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>
You can try with following code:
MessageFactory messageFactory = MessageFactory.newInstance();
SOAPMessage soapMessage = messageFactory.createMessage();
SOAPPart soapPart = soapMessage.getSOAPPart();
SOAPEnvelope envelope = soapPart.getEnvelope();
envelope.addNamespaceDeclaration("ns", "website");
envelope.addNamespaceDeclaration("com", "website/Common");
envelope.addNamespaceDeclaration("xm", "http://www.w3.org/2005/05/xmlmime");
SOAPBody soapBody = envelope.getBody();
SOAPElement element = soapBody.addChildElement("RequestName", "ns");
SOAPElement modelElement = element.addChildElement("model", "ns");
SOAPElement soapElement = modelElement.addChildElement("keys", "ns");
soapElement.addAttribute(envelope.createName("query"), "myquery;");
modelElement.addChildElement("instance", "ns");
soapMessage.saveChanges();
soapMessage.writeTo(System.out);
This will produce following output:
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:com="website/Common"
xmlns:ns="website"
xmlns:xm="http://www.w3.org/2005/05/xmlmime">
<SOAP-ENV:Header/>
<SOAP-ENV:Body>
<ns:RequestName>
<ns:model>
<ns:keys query="myquery;"/>
<ns:instance/>
</ns:model>
</ns:RequestName>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>
I need to write a SOAP client to send a request message, I could successfully send the request but I need to amend the message, the only required modification is to add prefix to the child elements.
Once I've used the following code but nothing happen.
WebsiteConfigID.addNamespaceDeclaration("v3", "http://tnwebservices.ticketnetwork.com/tnwebservice/v3.2");
Current output
<env:Envelope xmlns:env="http://www.w3.org/2003/05/soap-envelope"
xmlns:v3="http://tnwebservices.ticketnetwork.com/tnwebservice/v3.2">
<env:Header/>
<env:Body>
<GetEvents>
<websiteConfigID>1111</websiteConfigID>
<cityZip>Paris</cityZip>
</GetEvents>
</env:Body>
</env:Envelope>
Expected output
<env:Envelope xmlns:env="http://www.w3.org/2003/05/soap-envelope"
xmlns:v3="http://tnwebservices.ticketnetwork.com/tnwebservice/v3.2">
<env:Header/>
<env:Body>
<v3:GetEvents> <<prefix is added
<v3:websiteConfigID>1111</v3:websiteConfigID> <<prefix is added
<v3:cityZip>Paris</v3:cityZip> <<prefix is added
</v3:GetEvents>
</env:Body>
</env:Envelope>
Code
SOAPConnectionFactory soapConnectionFactory = SOAPConnectionFactory.newInstance();
SOAPConnection connection = soapConnectionFactory.createConnection();
SOAPMessage message = MessageFactory.newInstance(SOAPConstants.SOAP_1_2_PROTOCOL).createMessage();
SOAPBody body = message.getSOAPBody();
SOAPPart part = message.getSOAPPart();
SOAPEnvelope envelope = part.getEnvelope();
envelope.addNamespaceDeclaration("v3", "http://tnwebservices.ticketnetwork.com/tnwebservice/v3.2");
SOAPFactory soapFactory = SOAPFactory.newInstance();
Name bodyName;
bodyName = soapFactory.createName("GetEvents");
SOAPBodyElement getEvents = body.addBodyElement(bodyName);
Name childName = soapFactory.createName("websiteConfigID");
SOAPElement WebsiteConfigID = getEvents.addChildElement(childName);
WebsiteConfigID.addTextNode("1111");
childName = soapFactory.createName("cityZip");
SOAPElement CityZip = getEvents.addChildElement(childName);
CityZip.addTextNode("Paris");
message.writeTo(System.out);
Use the SOAPFactory methods which take QName, or prefix and uri information. For example, instead of calling bodyName = soapFactory.createName("GetEvents");, it should be
bodyName = soapFactory.createName("GetEvents", "v3",
"http://tnwebservices.ticketnetwork.com/tnwebservice/v3.2");
Refer to here for more details on createName method
I'm trying to call a webservice which expects an array as parameter but I've no idea how to do that, I have no wsdl and I'd like to use javax to do that.
String operation = "TheOperationName";
String urn = "TheWebService";
String destination = "http://my-server/something.php";
// First create the connection
SOAPConnectionFactory soapConnFactory = SOAPConnectionFactory.newInstance();
SOAPConnection connection = soapConnFactory.createConnection();
// Next, create the actual message
MessageFactory messageFactory = MessageFactory.newInstance();
SOAPMessage message = messageFactory.createMessage();
SOAPPart soapPart = message.getSOAPPart();
SOAPEnvelope envelope = soapPart.getEnvelope();
envelope.addNamespaceDeclaration("SOAP-ENC", "http://schemas.xmlsoap.org/soap/encoding/");
// Create and populate the body
SOAPBody body = envelope.getBody();
// Create the main element and namespace
SOAPElement operationItem = body.addChildElement(
envelope.createQName(operation, "ns1"));
//Here I'm lost, I should create an array
param0 SOAP-ENC:arrayType="SOAP-ENC:Struct[1]" xsi:type="SOAP-ENC:Array"
which contains my items array
SOAPElement itemElement = myarray.addChildElement("item");
// Add parameters
itemElement.addChildElement("ID_Bug").addTextNode("ID");
itemElement.addChildElement("ID_Release").addTextNode("RELEASE");
// Save the message
message.saveChanges();
// Send the message and get the reply
SOAPMessage reply = connection.call(message, destination);
reply.toString();
// Close the connection
connection.close();
any suggestions on how to achieve that?
I am creating a Java client for a SOAP service that takes an attachment. I'm using java.xml.soap classes, which I have uses before, but not with attachments. The server claims that my attachment is not included.
I used SoapUI, which works, and wireshark to compare my SOAP message to a working SOAP message. One big difference is that my header does not include "start=".
The working Content-Type looks like this:
Content-Type: multipart/related; type="text/xml"; start=""; boundary="----=_Part_23_6341950.1286312374228"
The Content-Type I get from my Java code is like this:
Content-Type: multipart/related; type="text/xml"; boundary="----=_Part_23_6341950.1286312374228"
No start= even when the content ID is set on the root element. The working and failing SOAP messages are otherwise nearly identical. How can I get the start tag generated, or what are other reasons the server might not see the attachment?
Thanks
SOAPMessage soapMessage =
MessageFactory.newInstance().createMessage();
SOAPPart soapPart = soapMessage.getSOAPPart();
SOAPEnvelope soapEnvelope = soapPart.getEnvelope();
SOAPBody body = soapEnvelope.getBody();
SOAPHeader header = soapMessage.getSOAPHeader();
soapPart.setContentId("<rootpart#here.com>");
MimeHeaders mimeHeaders = soapMessage.getMimeHeaders();
mimeHeaders.addHeader("SOAPAction", "addDocument");
mimeHeaders.addHeader("Accept-Encoding", "gzip,deflate");
Name bodyName = soapEnvelope.createName("Document", "doc",
"http://ns/Document");
SOAPBodyElement document = body.addBodyElement(bodyName);
Name filenameName = soapEnvelope.createName("Filename", "doc",
"http://ns/Document");
SOAPElement filename = document.addChildElement(filenameName);
filename.setValue("filename.txt");
AttachmentPart attachment = soapMessage.createAttachmentPart();
attachment.setContent("Some text", "application/octet-stream");
attachment.setMimeHeader("Content-Transfer-Encoding", "binary");
soapMessage.addAttachmentPart(attachment);
SOAPConnectionFactory scf = SOAPConnectionFactory.newInstance();
SOAPConnection soapConnection = scf.createConnection();
URL url = new URL("http://host/Service");
SOAPMessage reply = soapConnection.call(soapMessage, url);
This works for me:
soapMessage.getMimeHeaders().setHeader("Content-Type",
soapMessage.getMimeHeaders().getHeader("Content-Type")[0]+
"; start=\"<rootpart#here.com>\"");