Reputation: 821
I am making SOAP request with POST method which is having authentication added in the request header and request payload is being passed as XML String.
Below is the reference code:
package org.ecommerce.integration;
import java.io.BufferedReader;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import javax.net.ssl.HttpsURLConnection;
import org.apache.commons.io.IOUtils;
public class SoapRequestTest2 {
public static void main(String args[]) throws MalformedURLException,
IOException {
//Code to make a webservice HTTP request
String responseString = "";
String outputString = "";
String wsURL = "https://example.com:443/fscmService/ItemServiceV2";
URL url = new URL(wsURL);
URLConnection connection = url.openConnection();
HttpsURLConnection httpConn = (HttpsURLConnection)connection;
ByteArrayOutputStream bout = new ByteArrayOutputStream();
String xmlInput =
"<soap:Envelope xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\">"
+"<soap:Body>"
+"<ns1:findItem xmlns:ns1=\"http://xmlns.oracle.com/apps/scm/productModel/items/itemServiceV2/types/\">"
+ "<ns1:findCriteria xmlns:ns2=\"http://xmlns.oracle.com/adf/svc/types/\">"
+"<ns2:fetchStart>0</ns2:fetchStart>"
+"<ns2:fetchSize>100</ns2:fetchSize>"
+"<ns2:filter>"
+"<ns2:conjunction></ns2:conjunction>"
+"<ns2:group>"
+"<ns2:conjunction></ns2:conjunction>"
+"<ns2:upperCaseCompare>false</ns2:upperCaseCompare>"
+"<ns2:item>"
+"<ns2:conjunction></ns2:conjunction>"
+"<ns2:upperCaseCompare>false</ns2:upperCaseCompare>"
+"<ns2:attribute>ItemNumber</ns2:attribute>"
+"<ns2:operator>=</ns2:operator>"
+"<ns2:value>Test MR Item</ns2:value>"
+"</ns2:item>"
+"</ns2:group>"
+"</ns2:filter>"
+"<ns2:excludeAttribute>false</ns2:excludeAttribute>"
+"</ns1:findCriteria>"
+" <ns1:findControl xmlns:ns2=\"http://xmlns.oracle.com/adf/svc/types/\">"
+"<ns2:retrieveAllTranslations>true</ns2:retrieveAllTranslations>"
+"</ns1:findControl>"
+"</ns1:findItem>"
+"</soap:Body>"
+"</soap:Envelope>";
byte[] buffer = new byte[xmlInput.length()];
buffer = xmlInput.getBytes();
bout.write(buffer);
byte[] b = bout.toByteArray();
String SOAPAction =
"http://example.com/apps/scm/productModel/items/itemServiceV2/findItem";
// Set the appropriate HTTP parameters.
httpConn.setRequestProperty("Content-Length",
String.valueOf(b.length));
httpConn.setRequestProperty("Content-Type", "text/xml; charset=utf-8");
httpConn.setRequestProperty("Accept-Encoding", "gzip,deflate");
httpConn.setRequestProperty("Authorization", "Basic [encoded_value]");
httpConn.setRequestProperty("Host", "egvl-test.scm.us2.oraclecloud.com:443");
httpConn.setRequestProperty("SOAPAction", SOAPAction);
httpConn.setRequestMethod("POST");
httpConn.setDoOutput(true);
httpConn.setDoInput(true);
OutputStream out = httpConn.getOutputStream();
//Write the content of the request to the outputstream of the HTTP Connection.
out.write(b);
out.close();
//Ready with sending the request.
System.out.println(IOUtils.toString(httpConn.getInputStream()));
//Read the response.
InputStreamReader isr =
new InputStreamReader(httpConn.getInputStream());
BufferedReader in = new BufferedReader(isr);
//Write the SOAP message response to a String.
while ((responseString = in.readLine()) != null) {
// System.out.println("new Line : "+in.readLine());
outputString = outputString + responseString;
}
//Parse the String output to a org.w3c.dom.Document and be able to reach every node with the org.w3c.dom API.
//Write the SOAP message formatted to the console.
// System.out.println(outputString.toString());
}
}
When I try print out the result in the console, it prints with junk characters.
Please find below screenshot:
Any correct point out on the mistake would be helpful.
Upvotes: 0
Views: 486
Reputation: 2490
I'd recommend using a SOAP library instead, but I guess I have no idea if they support the authentication scheme wanted.
So, if SOAP library isn't realistic, I recommend using an HTTP client, such as Apache HttpClient. It's much easier to setup requests, and it knows to decompress most common compression schemes, without having to do anything yourself.
If you still don't want to do that, the problem you have is that the response sent is compressed.
So, you need first to read the Content-Encoding response header with
String compression = httpConn.getHeaderField("Content-Encoding");
And then decompress the response you're reading according to the compression method:
Upvotes: 1