我正在通过POST方法发出SOAP请求,该方法在请求标头中添加了身份验证,并且请求有效负载作为XML字符串传递。
下面是参考代码:
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());
}
}
当我尝试在控制台中打印出结果时,它会显示垃圾字符。
请在下面的屏幕截图中找到
:任何正确指出错误的方法都会有所帮助。
答案 0 :(得分:1)
我建议改用SOAP库,但我想我不知道它们是否支持所需的身份验证方案。
因此,如果SOAP库不切实际,我建议使用HTTP客户端,例如Apache HttpClient。设置请求要容易得多,而且它知道可以解压缩大多数常见的压缩方案,而无需您自己做任何事情。
如果您仍然不想这样做,则问题在于发送的响应已压缩。
因此,您需要首先使用以下内容读取Content-Encoding响应标头
String compression = httpConn.getHeaderField("Content-Encoding");
然后根据压缩方法解压缩您正在读取的响应: