我正在使用 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>"当我尝试在控制台中打印出结果时,它会打印出垃圾字符。请找到以下屏幕截图:任何对错误的正确指出都会有所帮助。
1 回答
慕无忌1623718
TA贡献1744条经验 获得超4个赞
我建议改用 SOAP 库,但我想我不知道它们是否支持所需的身份验证方案。
因此,如果 SOAP 库不现实,我建议使用 HTTP 客户端,例如 Apache HttpClient。设置请求要容易得多,它知道解压缩最常见的压缩方案,而无需自己做任何事情。
如果您仍然不想这样做,那么您遇到的问题是发送的响应被压缩了。
因此,您首先需要读取 Content-Encoding 响应标头
String compression = httpConn.getHeaderField("Content-Encoding");
然后根据压缩方法解压你正在阅读的响应:
InflaterInputStream 如果压缩是“放气”
GZIPInputStream 如果它是“gzip”
添加回答
举报
0/150
提交
取消