SpringBoot HttpClient方式调用Webservice接口

1.引入JAR包


	org.apache.httpcomponents
	httpclient
	4.5.12



	org.apache.httpcomponents
	httpcore
	4.4.13

2.Java代码

import com.sun.org.apache.xml.internal.security.utils.Base64;
import org.apache.commons.lang3.StringUtils;
import org.apache.http.HttpEntity;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.message.BasicHeader;
import org.apache.http.util.EntityUtils;

import java.io.IOException;
import java.nio.charset.Charset;

/**
 * Http方式调用webservice接口
 * */
public class HttpWS {
    static int socketTimeout = 30000;// 请求超时时间
    static int connectTimeout = 30000;// 传输超时时间

    /**
     * 使用SOAP1.1发送消息
     *
     * @param postUrl webservice地址
     * @param soapXml 发送的报文
     * @param soapAction
     * @param user 用户名
     * @param pass 密码
     * @return
     */
    public static String postSoap1_1(String postUrl, String soapXml, String soapAction, String user,String pass) {
        String retStr = "";
        // 创建HttpClientBuilder
        HttpClientBuilder httpClientBuilder = HttpClientBuilder.create();
        // HttpClient
        CloseableHttpClient closeableHttpClient = httpClientBuilder.build();
        HttpPost httpPost = new HttpPost(reWriteUrl(postUrl));

        // 需要用户名密码验证
        if(StringUtils.isNotEmpty(user)){
            httpPost.addHeader(new BasicHeader("Authorization","Basic "
                    + Base64.encode((user+":"+pass).getBytes())));
        }

        // 设置请求和传输超时时间
        RequestConfig requestConfig = RequestConfig.custom()
                .setSocketTimeout(socketTimeout)
                .setConnectTimeout(connectTimeout).build();
        httpPost.setConfig(requestConfig);

        if(soapAction != null){
            soapAction = "";
        }
        try {
            httpPost.setHeader("Content-Type", "text/xml;charset=UTF-8");
            httpPost.setHeader("SOAPAction", soapAction);
            StringEntity data = new StringEntity(soapXml, Charset.forName("UTF-8"));
            httpPost.setEntity(data);
            CloseableHttpResponse response = closeableHttpClient.execute(httpPost);
            HttpEntity httpEntity = response.getEntity();
            if (httpEntity != null) {
                // 打印响应内容
                retStr = EntityUtils.toString(httpEntity, "UTF-8");
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            // 释放资源
            try {
                if (closeableHttpClient != null)
                    closeableHttpClient.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return retStr;
    }

    /**
     * 使用SOAP1.2发送消息
     *
     * @param postUrl
     * @param soapXml
     * @param soapAction
     * @param user
     * @param pass
     * @return
     */
    public static String postSoap1_2(String postUrl, String soapXml, String soapAction, String user,String pass) {
        String retStr = "";
        // 创建HttpClientBuilder
        HttpClientBuilder httpClientBuilder = HttpClientBuilder.create();
        // HttpClient
        CloseableHttpClient closeableHttpClient = httpClientBuilder.build();
        HttpPost httpPost = new HttpPost(postUrl);

        // 需要用户名密码验证
        if(StringUtils.isNotEmpty(user)){
            httpPost.addHeader(new BasicHeader("Authorization","Basic "
                    + Base64.encode((user+":"+pass).getBytes())));
        }

        // 设置请求和传输超时时间
        RequestConfig requestConfig = RequestConfig.custom()
                .setSocketTimeout(socketTimeout)
                .setConnectTimeout(connectTimeout).build();
        httpPost.setConfig(requestConfig);

        if(soapAction != null){
            soapAction = "";
        }
        try {
            httpPost.setHeader("Content-Type", "application/soap+xml;charset=UTF-8");
            httpPost.setHeader("SOAPAction", soapAction);
            StringEntity data = new StringEntity(soapXml, Charset.forName("UTF-8"));
            httpPost.setEntity(data);
            CloseableHttpResponse response = closeableHttpClient.execute(httpPost);
            HttpEntity httpEntity = response.getEntity();
            if (httpEntity != null) {
                // 打印响应内容
                retStr = EntityUtils.toString(httpEntity, "UTF-8");
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            // 释放资源
            try {
                if (closeableHttpClient != null)
                    closeableHttpClient.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return retStr;
    }

    public static String reWriteUrl(String wsdlUrl){
        if(wsdlUrl.endsWith("?wsdl")){
            int index = wsdlUrl.indexOf("?wsdl");
            String address = wsdlUrl;
            if (index > 0) {
                address = wsdlUrl.substring(0, index);
            }
            return address;
        }else {
            return wsdlUrl;
        }
    }

    //测试
    public static void main(String args[]){
        String postUrl = "http://localhost:9080/test/services/TestWS?wsdl";
        String soapXml = "" +
                "   " +
                "   " +
                "      " +
                "         ?" +
                "      " +
                "   " +
                "";

        String username = "test";
        String password = "123456";
        String result = postSoap1_1(postUrl, soapXml, "",username,password);
        System.out.println(result);
    }
}

 

你可能感兴趣的:(xml,Java,springboot,HttpClient,webservice)