有头信息时如何根据WSDL文件使用轴调用Soup webservice

问题描述 投票:0回答:1

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>

我有一个上面的 WSDL 文件,但我不知道如何用头信息调用它。

任何人都可以帮助我在 xml 之上给出一个 java 轴示例使用,谢谢。

使用其他webservice框架是可以的。

java axis
1个回答
0
投票

使用 HTTP 调用的用户简单 java:

公共课 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;
}

}

© www.soinside.com 2019 - 2024. All rights reserved.