Search code examples
javaaxis

how to call Soup webservice according the WSDL file use axis when there is header info


EndPoint = "https:128.0.0.1/test/wsdl";

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ghe="http://gheo1.test.com">
   <soapenv:Header>
      <wsse:Security xmlns:wsse="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd" xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd">
         <wsse:UsernameToken>
            <wsse:Username>test_user</wsse:Username>
            <wsse:Password Type="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordText">test123</wsse:Password>
         </wsse:UsernameToken>
      </wsse:Security>
       <soapheader:loginInformation xmlns:soapheader="com.test.common">
         <soapheader:loginUserID>USERID</soapheader:loginUserID>
         <soapheader:loginUserType>CUSTOMER</soapheader:loginUserType> 
         <soapheader:loginUserUniqueKey>123456</soapheader:loginUserUniqueKey>
         <soapheader:loginUserRole>CUSTOMER</soapheader:loginUserRole>
      </soapheader:loginInformation>      
   </soapenv:Header>
   <soapenv:Body>
      <ghe:Request>
         <!--Optional:-->
         <arg0>
            <!--Optional:-->
            <accountNumber>123</accountNumber>
            <!--Optional:-->
            <groupNumber>456</groupNumber>
            <!--Optional:-->
            <authGroupNumber>789</authGroupNumber>
            <!--Optional:-->
            <authAccountNumber>123</authAccountNumber>
            <!--Optional:-->
            <authCertificateNumber>555</authCertificateNumber>
         </arg0>
      </ghe:Request>
   </soapenv:Body>
</soapenv:Envelope>

I have a WSDL file as above, and I don't know how to call this with header info.

anyone can help me to give a java axis sample use above xml, thanks.

Use other webservice framework is ok.


Solution

  • User simple java with HTTP to call:

    public class getSoupResponse {

    public static void main(String[] args) throws IOException {
    
        String urlString = "https:128.0.0.1/test/wsdl";
        URL url = new URL(urlString);
    
        HttpURLConnection connection = (HttpURLConnection) url.openConnection();
    
        if(urlString.startsWith("https")){
            HttpsURLConnection https = (HttpsURLConnection) connection;
            trustAllHosts(https);
            https.setHostnameVerifier(DO_NOT_VERIFY);
        }
    
    
        connection.setRequestMethod("POST");
        connection.setRequestProperty("Content-Type", "text/xml;charset=UTF-8");
        connection.setDoInput(true);
        connection.setDoOutput(true);
    
        String soupXML = getXML();
        OutputStream os = connection.getOutputStream();
        os.write(soupXML.getBytes(StandardCharsets.UTF_8));
    
        int responseCode = connection.getResponseCode();
        if(200==responseCode){
            InputStream inputStream = connection.getInputStream();
            InputStreamReader inputStreamReader = new InputStreamReader(inputStream);
            BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
    
            StringBuilder stringBuilder = new StringBuilder();
            String temp;
            while (null != (temp = bufferedReader.readLine())){
                stringBuilder.append(temp);
            }
    
            JSONObject object = XML.toJSONObject(stringBuilder.toString());
            System.out.println(object);
            inputStream.close();
            inputStreamReader.close();
            bufferedReader.close();
        }
        os.close();
    }
    
    private static String getXML(){
    
        //your soupXML request in soupUI
        return "<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ghe="http://gheo1.test.com">
                    <soapenv:Header>
                    </soapenv:Header>
                    <soapenv:Body>
                    </soapenv:Body>
                </soapenv:Envelope>";
    }
    
    private static final TrustManager[] trustAllCerts = new TrustManager[]{
    
            new X509TrustManager() {
                @Override
                public void checkClientTrusted(X509Certificate[] x509Certificates, String s) {
    
                }
    
                @Override
                public void checkServerTrusted(X509Certificate[] x509Certificates, String s) {
    
                }
    
                @Override
                public X509Certificate[] getAcceptedIssuers() {
                    return new X509Certificate[]{};
                }
            }
    };
    
    private static final HostnameVerifier DO_NOT_VERIFY = (s, sslSession) -> true;
    
    private static SSLSocketFactory trustAllHosts(HttpsURLConnection connection){
    
        SSLSocketFactory oldFactory = connection.getSSLSocketFactory();
        try {
            SSLContext sc = SSLContext.getInstance("TLS");
            sc.init(null, trustAllCerts, new java.security.SecureRandom());
            SSLSocketFactory newFactory = sc.getSocketFactory();
            connection.setSSLSocketFactory(newFactory);
        }catch (Exception e){
            e.printStackTrace();
        }
        return oldFactory;
    }
    

    }