栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

java调WebServiceUtils工具类

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

java调WebServiceUtils工具类

package com.jdl.lomir.chint.adapter.http.utils;

import lombok.extern.slf4j.Slf4j;
import org.apache.axis.client.Call;
import org.apache.axis.client.Service;
import org.apache.axis.encoding.XMLType;
import org.apache.commons.codec.binary.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.util.EntityUtils;

import javax.xml.namespace.QName;
import javax.xml.rpc.ParameterMode;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;


@Slf4j
public class WebServiceUtils {
    private static int socketTimeout = 30000;// 请求超时时间
    private static int connectTimeout = 30000;// 传输超时时间

    
    public static String[] invoke(String address, String method, String targetNamespace, Object[] keys, Object[] values) throws Exception {

        Service service = new Service();
        Call call = (Call) service.createCall();
        call.setEncodingStyle("UTF-8");
        call.setTargetEndpointAddress(address);
        call.setOperationName(new QName(targetNamespace, method)); // 方法名称
        call.setReturnType(XMLType.SOAP_ARRAY);// 设置返回类型
        call.setReturnClass(String[].class);
        params(targetNamespace, call, keys, values);
        call.setUseSOAPAction(true);
        call.setSOAPActionURI(targetNamespace + method);
        return (String[]) call.invoke(values);

    }

    
    private static void params(String targetNamespace, Call call, Object[] keys, Object[] values) {

        if (keys == null || values == null) return;
        if (keys.length == 0 || values.length == 0) return;
        if (keys.length != values.length) {
            throw new IllegalArgumentException("接口方法参数与参数值不匹配!");
        }
        for (int i = 0; i < values.length; i++) {

            String key = (String) keys[i]; //方法参数
            Object value = values[i]; //参数值
            if (value == null) {
                call.addParameter(new QName(targetNamespace, key), XMLType.XSD_STRING, ParameterMode.IN);
            } else if (value instanceof String) {
                call.addParameter(new QName(targetNamespace, key), XMLType.XSD_STRING, ParameterMode.IN);
            } else if (value instanceof Integer) {
                call.addParameter(new QName(targetNamespace, key), XMLType.XSD_INTEGER, ParameterMode.IN);
            } else if (value instanceof Boolean) {
                call.addParameter(new QName(targetNamespace, key), XMLType.XSD_BOOLEAN, ParameterMode.IN);
            }
        }
    }

    
    public static String execute(String userName, String passWord, String postUrl, String soapXml, String soapAction) {
        String retStr = "";
        try {
            // 创建HttpClientBuilder
            HttpClientBuilder httpClientBuilder = HttpClientBuilder.create();
            // HttpClient
            CloseableHttpClient closeableHttpClient = httpClientBuilder.build();
            HttpPost httpPost = new HttpPost(postUrl);
            //  设置请求和传输超时时间
            RequestConfig requestConfig = RequestConfig.custom()
                    .setSocketTimeout(socketTimeout)
                    .setConnectTimeout(connectTimeout).build();
            httpPost.setConfig(requestConfig);

            httpPost.setHeader("Content-Type", "text/xml;charset=UTF-8");
            httpPost.setHeader("SOAPAction", soapAction);
            httpPost.setHeader("Connection","Keep-Alive");
            if (StringUtils.isNotBlank(userName) && StringUtils.isNotBlank(passWord)) {
                String auth = userName + ":" + passWord;
                byte[] encodedAuth = base64.encodebase64(
                        auth.getBytes(StandardCharsets.UTF_8));
                String basic = "Basic " + new String(encodedAuth);
                httpPost.setHeader("Authorization", basic);
            }
            StringEntity stringEntity = new StringEntity(soapXml,
                    Charset.forName("UTF-8"));
            httpPost.setEntity(stringEntity);
            CloseableHttpResponse response = closeableHttpClient
                    .execute(httpPost);
            HttpEntity httpEntity = response.getEntity();
            if (httpEntity != null) {
                retStr = EntityUtils.toString(httpEntity, "UTF-8");
            }
            // 释放资源
            closeableHttpClient.close();
        } catch (Exception e) {
            log.error("exception in SOAP1.1 failed to send message!");
        }
        return retStr;
    }

}

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/351520.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号