Java SOAP POST 使用垃圾字符调用 returns
Java SOAP POST call returns with junk characters
我正在使用 POST 方法发出 SOAP 请求,该方法在请求中添加了身份验证 header 并且请求负载作为 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());
}
}
当我尝试在控制台中打印出结果时,它打印出垃圾字符。
请查看以下屏幕截图:
任何正确的错误指出都会有所帮助。
我建议改用 SOAP 库,但我想我不知道它们是否支持所需的身份验证方案。
因此,如果 SOAP 库不现实,我建议使用 HTTP 客户端,例如 Apache HttpClient。设置请求要容易得多,而且它知道解压缩最常见的压缩方案,而无需自己做任何事情。
如果您仍然不想这样做,则问题是发送的响应被压缩了。
因此,您需要先阅读 Content-Encoding 回复 header 和
String compression = httpConn.getHeaderField("Content-Encoding");
然后根据压缩方式解压您正在阅读的回复:
- InflaterInputStream 如果压缩是 "deflate"
- GZIPInputStream 如果是 "gzip"
我正在使用 POST 方法发出 SOAP 请求,该方法在请求中添加了身份验证 header 并且请求负载作为 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());
}
}
当我尝试在控制台中打印出结果时,它打印出垃圾字符。
请查看以下屏幕截图:
任何正确的错误指出都会有所帮助。
我建议改用 SOAP 库,但我想我不知道它们是否支持所需的身份验证方案。
因此,如果 SOAP 库不现实,我建议使用 HTTP 客户端,例如 Apache HttpClient。设置请求要容易得多,而且它知道解压缩最常见的压缩方案,而无需自己做任何事情。
如果您仍然不想这样做,则问题是发送的响应被压缩了。
因此,您需要先阅读 Content-Encoding 回复 header 和
String compression = httpConn.getHeaderField("Content-Encoding");
然后根据压缩方式解压您正在阅读的回复:
- InflaterInputStream 如果压缩是 "deflate"
- GZIPInputStream 如果是 "gzip"