web服务—java中的soap请求api调用提供二进制垃圾响应,但在soapui中工作

ee7vknir  于 2021-07-06  发布在  Java
关注(0)|答案(0)|浏览(348)

我试图通过java代码调用soap api。它是通过soapui工作的,但不是通过java代码。

public class SoapExecutor {
public static void main(String args[]) throws Exception{

    File file = new File("soap_request.xml");

    BufferedReader br = new BufferedReader(new FileReader(file));
    String soapXmlLine;
    StringBuilder soapXml = new StringBuilder();
    while ((soapXmlLine = br.readLine()) != null) {
        soapXml.append(soapXmlLine);
        soapXml.append("\n");

    }

String url = "https://ucf3-zpri-fa-ext.oracledemos.com:443/xmlpserver/services/ExternalReportWSSService";

String encodedCredential = encodeStringToBase64("user:password");

String response = executeSoapRequest(soapXml,url,"Basic "+encodedCredential);

System.out.println(response);
}
public static String encodeStringToBase64(String s) throws UnsupportedEncodingException {
    byte[] message = s.getBytes("UTF-8");
    String encoded = DatatypeConverter.printBase64Binary(message);
    return encoded;
}
    public static String executeSoapRequest(StringBuilder payload, String url, String authorization)
            throws Exception {
        String apiResponse;
        SOAPConnectionFactory sfc = SOAPConnectionFactory.newInstance();
        SOAPConnection connection = sfc.createConnection();
        InputStream is = new ByteArrayInputStream(payload.toString().getBytes());

        SOAPMessage request = MessageFactory.newInstance().createMessage(null, is);

        request.getMimeHeaders().addHeader("Authorization", authorization);
        request.saveChanges();

        SOAPMessage soapResponse = connection.call(request, url);
        //System.out.println(soapResponse.toString());
         ByteArrayOutputStream stream2 = new ByteArrayOutputStream();
            soapResponse.writeTo(stream2);
            String contractTermResponse = new String(stream2.toByteArray(),"utf-8");
            System.out.println("Test SOAP Response :" + contractTermResponse);

        return contractTermResponse;

    }

}

sopa\u request.xml是:

<soapenv:Envelope xmlns:soapenv="http://www.w3.org/2003/05/soap-envelope"><soapenv:Header></soapenv:Header><soapenv:Body><pub:runReport xmlns:pub="http://xmlns.oracle.com/oxp/service/PublicReportService">
   <pub:reportRequest>
      <pub:parameterNameValues>
         <pub:item>
            <pub:name>PR_NUM</pub:name>
            <pub:values>
               <pub:item>52326</pub:item>
            </pub:values>
         </pub:item>
      </pub:parameterNameValues>
      <pub:reportAbsolutePath>/Custom/Globality/Globality Agreement Details Report.xdo</pub:reportAbsolutePath>
      <pub:sizeOfDataChunkDownload>-1</pub:sizeOfDataChunkDownload>
   </pub:reportRequest>
</pub:runReport></soapenv:Body></soapenv:Envelope>

输出:

问题:soap请求通过soapui工作,我得到了正确的响应:

<env:Envelope xmlns:env="http://www.w3.org/2003/05/soap-envelope">
   <env:Header/>
   <env:Body>
      <ns2:runReportResponse xmlns:ns2="http://xmlns.oracle.com/oxp/service/PublicReportService">
         <ns2:runReportReturn>
            <ns2:reportBytes>PD94bWwgdmVyc2lvbj0iMS4wIiBlbmNvZGluZz0iVVRGLTgiPz4KPCEtLUdlbmVyYXRlZCBieSBP
cmFjbGUgQkkgUHVibGlzaGVyIC1EYXRhZW5naW5lLCBkYXRhbW9kZWw6X0N1c3RvbV9HbG9iYWxp
dHlfRGF0YV9Nb2RlbF9HQkxfQ1BBX1NUQVRfeGRtIC0tPgo8QUdSX1NUQVQ+PFBSX05VTT41MjMy
NjwvUFJfTlVNPgo8SEVBREVSPgo8QUdSRUVNRU5UX05VTT41MjMyNjwvQUdSRUVNRU5UX05VTT48
QUdSRUVNRU5UX0hFQURFUl9JRD4yNTA3ODI8L0FHUkVFTUVOVF9IRUFERVJfSUQ+PEFHUkVFTUVO
VF9TVEFUVVM+UEVORElORyBBUFBST1ZBTDwvQUdSRUVNRU5UX1NUQVRVUz48QlVfSUQ+MzAwMDAw
MDQ2OTg3MDEyPC9CVV9JRD48Q1VSUkVOQ1lfQ09ERT5DQUQ8L0NVUlJFTkNZX0NPREU+PFNUQVJU
X0RBVEU+MjAyMC0xMS0yNVQwMDowMDowMC4wMDArMDA6MDA8L1NUQVJUX0RBVEU+PEVORF9EQVRF
PjIwMjEtMDItMjFUMDA6MDA6MDAuMDAwKzAwOjAwPC9FTkRfREFURT48QU1PVU5UX0xJTUlUPjEw
MDA8L0FNT1VOVF9MSU1JVD48TUlOX1JFTEVBU0VfQU1PVU5UPjEwMDwvTUlOX1JFTEVBU0VfQU1P
VU5UPgo8L0hFQURFUj4KPC9BR1JfU1RBVD4=</ns2:reportBytes>
            <ns2:reportContentType>text/xml</ns2:reportContentType>
            <ns2:reportFileID xsi:nil="true" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"/>
            <ns2:reportLocale xsi:nil="true" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"/>
            <ns2:metaDataList xsi:nil="true" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"/>
         </ns2:runReportReturn>
      </ns2:runReportResponse>
   </env:Body>
</env:Envelope>

但通过java代码,我在控制台上获得了高于垃圾的价值。更奇怪的是,如果我在头中注解了授权的设置值,那么代码就不会记录缺少的凭证。输出仍然是垃圾值。

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题