2

calling soap web service

I have a soap web service to call, here is my code:

private List<Guaranty> getGuarantyList(String nationalId, String centerBankCode) throws IOException{
        
        try {
            fixHttpsHandler();
        } catch (Exception ex) {
            System.out.println(ex.getMessage());
        }

        // Code to make a webservice HTTP request
        String responseString = "";
        String outputString = "";
        String wsEndPoint = "http://192.168.5.202/services/out.asmx";
        URL url = new URL(wsEndPoint);
        URLConnection connection = url.openConnection();
        HttpURLConnection httpConn = (HttpURLConnection) connection;
        ByteArrayOutputStream bout = new ByteArrayOutputStream();
        String xmlInput = "<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:pos=\"http://postbank.ir/\">\r\n" + 
                "   <soapenv:Header>\r\n" + 
                "      <pos:PostbankAuth>\r\n" + 
                "         <!--Optional:-->\r\n" + 
                "         <pos:UserName>ebank</pos:UserName>\r\n" + 
                "         <!--Optional:-->\r\n" + 
                "         <pos:Password>qwer@123</pos:Password>\r\n" + 
                "      </pos:PostbankAuth>\r\n" + 
                "   </soapenv:Header>\r\n" + 
                "   <soapenv:Body>\r\n" + 
                "      <pos:getGuaranty>\r\n" + 
                "         <!--Optional:-->\r\n" + 
                "         <pos:nationalCode>" + nationalId + "</pos:nationalCode>\r\n" + 
                "         <!--Optional:-->\r\n" + 
                "         <pos:centerBankCode>" + centerBankCode + "</pos:centerBankCode>\r\n" + 
                "         <!--Optional:-->\r\n" + 
                "         <pos:length>10</pos:length>\r\n" + 
                "         <pos:offset>0</pos:offset>\r\n" + 
                "      </pos:getGuaranty>\r\n" + 
                "   </soapenv:Body>\r\n" + 
                "</soapenv:Envelope>";
        byte[] buffer = new byte[xmlInput.length()];
        buffer = xmlInput.getBytes();
        bout.write(buffer);
        byte[] b = bout.toByteArray();
        String SOAPAction = "http://postbank.ir/getGuaranty";
        httpConn.setRequestProperty("Accept-Encoding", "gzip,deflate");
        httpConn.setRequestProperty("Content-Type", "text/xml; charset=utf-8");
        httpConn.setRequestProperty("SOAPAction", SOAPAction);
        httpConn.setRequestProperty("Content-Length", String.valueOf(b.length));
        httpConn.setRequestProperty("Host", "192.168.5.202");
        httpConn.setRequestProperty("Connection", "Keep-Alive");
        httpConn.setRequestProperty("User-Agent", "Apache-HttpClient/4.5.5 (Java/16.0.1)");
        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.
        // Read the response.

        InputStreamReader isr = new InputStreamReader(httpConn.getInputStream(), Charset.forName("UTF-8"));

        BufferedReader in = new BufferedReader(isr);
        // Write the SOAP message response to a String.
        while ((responseString = in.readLine()) != null) {
            outputString = outputString + responseString;
        }
            
        String formattedSOAPResponse = formatXML(outputString);
        return formattedSOAPResponse ;
        
    }

but i get this as response instead a readable xml:

�________�__I�%&/m�{_J�J��t�_�$ؐ@������_iG#)�*��eVe]f@�흼��{���{���;�N'���?\fd_l��J�ɞ!���?~|?"�_�_ez��MQ-?�hw��Q�/�լX^|�Ѻ=�>���8z�T�����2/�U��+��>��yۮ_ݽ�L��"k��>W��]�r7ח�~���k�������_7���ٽ�{��5_�.�M�-��{kv�[)�O����㋼�|��ٲ�~�7�j�(�_Ъj�I�|;.��n�ºl�__�7Mv�_Uo_�5�?~��N�|v�����]���Y�__��$ϖG��E�^��ۏ�>�1������I~Q,�e��GG{;{{�;����ٻ�hg����νG��-ۚ^����j潹��Km�_�ыl�5����_�����f�_�=���6��_5�k��C wv?ݻ�wp����n�Rެ�__��nz�6���|V��۬]7�g�NO���_�\�51�ku��_5ҏѠi�E^c(�ѿ�7��_X�o����_O��_��?&����_�_����_�o����������?������o����_F_�[_��W�[տ�7_�����_�_��dzu��$�C_����q��aB��2}�{���ӶG�_�TD���������;?�y0�9,�B$o�oW�,��g���Ͻ�o�5����c������~���3�q��٫��;{�4=43�v�_�?x���s_�:�_B���_�7 �;4�;;�_t^Ֆ�� �{��������h�פ����o�1D����#R"�_�o��4"��_A��_t��;+��������?}@�_�uM__o�2_ͫe��r v_����}�C�^c_BL�^z�������S~��9z�^��y3��__�5���)���+��?�8�_�o��������������K��?���_�m鏿��������ߜn_��Q��_�o�5��_w���N?G����.q"i��_�����'}�iW�ݘ���_ꙴZH

this way works for other web services but not this one. i tested the web service in soap-ui and it works correctly... thanks

1 Answers1

4

The garbled response you're receiving indicates that the response is compressed with gzip or deflate encoding. To properly handle this compressed response, you need to decode it before reading it as a readable XML.

import java.io.*;
import java.net.*;

import java.util.zip.GZIPInputStream;

private List<Guaranty> getGuarantyList(String nationalId, String centerBankCode) throws IOException {
    try {
        fixHttpsHandler();
    } catch (Exception ex) {
        System.out.println(ex.getMessage());
    }

    String responseString = "";
    String outputString = "";
    String wsEndPoint = "http://192.168.5.202/services/out.asmx";
    URL url = new URL(wsEndPoint);
    URLConnection connection = url.openConnection();
    HttpURLConnection httpConn = (HttpURLConnection) connection;
    ByteArrayOutputStream bout = new ByteArrayOutputStream();
    String xmlInput = "<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:pos=\"http://postbank.ir/\">\r\n" + 
            ...
            "</soapenv:Envelope>";
    byte[] buffer = new byte[xmlInput.length()];
    buffer = xmlInput.getBytes();
    bout.write(buffer);
    byte[] b = bout.toByteArray();
    String SOAPAction = "http://postbank.ir/getGuaranty";
    httpConn.setRequestProperty("Accept-Encoding", "gzip,deflate");
    httpConn.setRequestProperty("Content-Type", "text/xml; charset=utf-8");
    httpConn.setRequestProperty("SOAPAction", SOAPAction);
    httpConn.setRequestProperty("Content-Length", String.valueOf(b.length));
    httpConn.setRequestProperty("Host", "192.168.5.202");
    httpConn.setRequestProperty("Connection", "Keep-Alive");
    httpConn.setRequestProperty("User-Agent", "Apache-HttpClient/4.5.5 (Java/16.0.1)");
    httpConn.setRequestMethod("POST");
    httpConn.setDoOutput(true);
    httpConn.setDoInput(true);
    OutputStream out = httpConn.getOutputStream();
    out.write(b);
    out.close();

    InputStream inputStream = httpConn.getInputStream();
    String contentEncoding = httpConn.getContentEncoding();

    if (contentEncoding != null && contentEncoding.equalsIgnoreCase("gzip")) {
        inputStream = new GZIPInputStream(inputStream);
    } else if (contentEncoding != null && contentEncoding.equalsIgnoreCase("deflate")) {
        inputStream = new InflaterInputStream(inputStream);
    }

    InputStreamReader isr = new InputStreamReader(inputStream, Charset.forName("UTF-8"));

    BufferedReader in = new BufferedReader(isr);
    while ((responseString = in.readLine()) != null) {
        outputString = outputString + responseString;
    }

    String formattedSOAPResponse = formatXML(outputString);
    return formattedSOAPResponse;
}

I added logic to check the Content-Encoding header of the HTTP response. If it indicates gzip compression, the inputStream is wrapped in a GZIPInputStream for decompression. Similarly, if the encoding is deflate, it is wrapped in an InflaterInputStream.

Ben Meehan
  • 166
  • 5