首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >Java SOAP POST调用返回垃圾字符

Java SOAP POST调用返回垃圾字符
EN

Stack Overflow用户
提问于 2018-07-30 00:21:16
回答 1查看 296关注 0票数 0

我正在使用POST方法发出SOAP请求,该方法在请求报头中添加了身份验证,并将请求有效负载作为XML字符串传递。

参考代码如下:

代码语言:javascript
复制
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());
    }


}

当我尝试在控制台中打印结果时,它打印出垃圾字符。

请查看以下截图:

对这个错误的任何正确的指出都是有帮助的。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-07-30 02:05:48

我建议使用SOAP库,但我想我不知道它们是否支持所需的身份验证方案。

因此,如果SOAP库不现实,我建议使用HTTP客户端,例如Apache HttpClient。设置请求要容易得多,而且它知道如何解压缩最常见的压缩方案,而不必自己做任何事情。

如果您仍然不想这样做,那么您遇到的问题是发送的响应是压缩的。

因此,您首先需要使用以下命令读取Content-Encoding响应头

代码语言:javascript
复制
String compression = httpConn.getHeaderField("Content-Encoding");

然后根据压缩方法对正在读取的响应进行解压缩:

如果压缩为"deflate"

  • GZIPInputStream,则为
  • InflaterInputStream;如果压缩为“gzip”,则为
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/51582277

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档