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

flvlnr44  于 2023-05-21  发布在  Java
关注(0)|答案(1)|浏览(143)

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文件,如上所述,我不知道如何调用这个头信息。
任何人都可以帮助我给予一个java轴的例子使用上面的xml,谢谢。
使用其他Web服务框架是可以的。

wpx232ag

wpx232ag1#

用户简单的java与HTTP调用:
public class footer {

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;
}

}

相关问题