WebService客户端几种实现方式

客户端几种实现方式(转载)

Demo-HttpClient

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

        //soap服务地址
        String url = "http://10.104.168.38:6906/sip/services/XJSSServices?wsdl";
        StringBuilder soapBuilder = new StringBuilder(64);
        soapBuilder.append("");
        soapBuilder.append("   ");
        soapBuilder.append("       ");
        soapBuilder.append("             ");
        soapBuilder.append("                   CBXX001");
        soapBuilder.append("               ");
        soapBuilder.append("               ");
        soapBuilder.append("    ");
        soapBuilder.append("");

        //创建httpcleint对象
        CloseableHttpClient httpClient = HttpClients.createDefault();
        //创建http Post请求
        HttpPost httpPost = new HttpPost(url);
        // 构建请求配置信息
        RequestConfig config = RequestConfig.custom().setConnectTimeout(1000) // 创建连接的最长时间
                .setConnectionRequestTimeout(500) // 从连接池中获取到连接的最长时间
                .setSocketTimeout(3 * 1000) // 数据传输的最长时间10s
                .build();
        httpPost.setConfig(config);
        CloseableHttpResponse response = null;
        try {
            //采用SOAP1.1调用服务端,这种方式能调用服务端为soap1.1和soap1.2的服务
            httpPost.setHeader("Content-Type", "text/xml;charset=UTF-8");
            httpPost.setHeader("SOAPAction", "http://10.104.168.38:6906/sip/services/XJSSServices?wsdl");

            //采用SOAP1.2调用服务端,这种方式只能调用服务端为soap1.2的服务
            StringEntity stringEntity = new StringEntity(soapBuilder.toString(), Charset.forName("UTF-8"));
            httpPost.setEntity(stringEntity);
            response = httpClient.execute(httpPost);
            // 判断返回状态是否为200
            if (response.getStatusLine().getStatusCode() == 200) {
                String content = EntityUtils.toString(response.getEntity(), "UTF-8");
                System.out.println("------------:"+content);

            } else {
                System.out.println("调用失败!");
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (null != response) {
                response.close();
            }
            if (null != httpClient) {
                httpClient.close();
            }
        }

    }

你可能感兴趣的:(java)