webservice客户端代码

package com.miitgxt.webservices.client.rpc;

import javax.xml.rpc.ServiceException;

import org.apache.axiom.om.OMAbstractFactory;
import org.apache.axiom.om.OMElement;
import org.apache.axiom.om.OMFactory;
import org.apache.axis.client.Call;
import org.apache.axis.client.Service;
import org.apache.axis2.AxisFault;
import org.apache.axis2.client.Options;
import org.apache.axis2.rpc.client.RPCServiceClient;

/**
 * webservice调用基类
 *
 *
 *
 * @author wenhao
 *
 */
public abstract class WebServiceFactoryClient {

    /**
     * @Title: WebServer_Init
     * @描述:(方法描述)
     * @作者: 客户端调用的方法
     * @参数: 传入参数定义
     * @返回值: Call 返回类型
     * @throws
     */
    public static Call WebServer_Init(String WebServerAddress, String CallOperationName) {
        Service service = new Service();
        Call call = null;
        try {
            call = (Call) service.createCall();
            call.setTargetEndpointAddress(WebServerAddress);
            call.setOperationName(CallOperationName);
        } catch (ServiceException e) {
            e.printStackTrace();
        }
        return call;
    }

    public abstract RPCServiceClient createRpcServiceClient(String url, String namespace, String method,int timeout) throws Exception;

    public abstract String WebPost(Long clientId, String pwd, String fileContent, String method, String url, String namespace,int timeout) throws Exception;

    public abstract OMFactory createOMFactory();


    public abstract OMElement netPost(String url, String action, String methodStr, String namespace, String tns, String[] pars, String[] vals,int timeout) throws AxisFault;


    public static String createXml(String yhm, String yhlb) {
        return null;
    }
}




package com.miitgxt.webservices.client.rpc;

import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;
import java.security.Security;

import javax.xml.namespace.QName;

import org.apache.axiom.om.OMAbstractFactory;
import org.apache.axiom.om.OMElement;
import org.apache.axiom.om.OMFactory;
import org.apache.axiom.om.OMNamespace;
import org.apache.axis2.AxisFault;
import org.apache.axis2.addressing.EndpointReference;
import org.apache.axis2.client.Options;
import org.apache.axis2.client.ServiceClient;
import org.apache.axis2.rpc.client.RPCServiceClient;
import org.apache.axis2.transport.http.HTTPConstants;
import org.apache.commons.lang.StringUtils;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;

import com.miitgxt.webservices.client.WebServiceParameters;

/**
 * webservice 请求
 *
 * @author wenhao 公共类
 *
 */
public class WebServiceClient extends WebServiceFactoryClient {
    /**
     * 代理类编写
     */
    @Override
    public RPCServiceClient createRpcServiceClient(String url, String namespace, String method, int timeout) throws Exception {
        RPCServiceClient serviceClient = null;
        try {
            serviceClient = new RPCServiceClient();
            EndpointReference targetEPR = new EndpointReference(url);
            Options options = new Options();
            options.setTo(targetEPR);
            options.setTimeOutInMilliSeconds(timeout);
            options.setProperty(HTTPConstants.CHUNKED, "false");// 把chunk关掉后,会自动加上Content-Length。
            serviceClient.setOptions(options);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {

        }
        return serviceClient;
    }

    /**
     * 请求发送
     */
    @SuppressWarnings("rawtypes")
    @Override
    public String WebPost(Long clientId, String pwd, String fileContent, String method, String url, String namespace, int timeout) throws Exception {
        RPCServiceClient serviceClient = null;
        try {
            serviceClient = createRpcServiceClient(url, namespace, method, timeout);
            // 在创建QName对象时,QName类的构造方法的第一个参数表示WSDL文件的命名空间名,也就是<wsdl:definitions>元素的targetNamespace属性值
            QName opAddEntry = new QName(namespace, method);
            // 参数,如果有多个,继续往后面增加即可,不用指定参数的名称
            Object[] opAddEntryArgs = new Object[] { clientId, pwd, fileContent };
            Class[] classes = new Class[] { String.class };
            fileContent = (String) serviceClient.invokeBlocking(opAddEntry, opAddEntryArgs, classes)[0];
        } catch (Exception e) {
            e.printStackTrace();
            fileContent = WebServiceParameters.JG_FAIL;
            throw new RuntimeException("调用远程接口 链接失败");
        } finally {
            serviceClient.cleanupTransport();
        }
        return fileContent;
    }

    /**
     * .net与Java的区别 createOMFactory(这里用一句话描述这个方法的作用) (这里描述这个方法适用条件 – 可选)
     *
     * @return OMFactory
     * @author wenhao
     * @exception
     * @since 1.0.0
     */
    public OMFactory createOMFactory() {
        OMFactory fac = OMAbstractFactory.getOMFactory();
        return fac;
    }

    public OMElement getOMMethod(String methodStr, String namespace, String tns, String[] pars, String[] vals) {
        OMFactory fac = createOMFactory();
        // 创建命名空间
        OMNamespace nms = fac.createOMNamespace(namespace, tns);
        // 创建OMElement方法 元素,并指定其在nms指代的名称空间中
        OMElement method = fac.createOMElement(methodStr, nms);
        // 添加方法参数名和参数值
        for (int i = 0; i < pars.length; i++) {
            // 创建方法参数OMElement元素
            OMElement param = fac.createOMElement(pars[i], nms);
            // 设置键值对 参数值
            param.setText(vals[i]);
            // 讲方法元素 添加到method方法元素中
            method.addChild(param);
        }
        return method;
    }

    public OMElement netPost(String url, String action, String methodStr, String namespace, String tns, String[] pars, String[] vals, int timeout) {
        OMElement result = null;
        ServiceClient client = null;
        try {
            client = new ServiceClient();
            client.setOptions(getClientOptions(action, url, timeout));
            result = client.sendReceive(getOMMethod(methodStr, namespace, tns, pars, vals));
        } catch (AxisFault e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } finally {
            try {
                client.cleanupTransport();
            } catch (AxisFault e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        return result;
    }

    public Options getClientOptions(String action, String url, int timeout) {
        // 创建request soap包 请求选项
        Options options = new Options();
        // 设置request soap包的端点引用(接口地址)
        EndpointReference targetEpr = new EndpointReference(url);
        // 设置options的soapAction
        options.setAction(action);
        options.setTo(targetEpr);
        // 如果报错提示Content-Length,请求内容长度
        options.setProperty(HTTPConstants.CHUNKED, "false");// 把chunk关掉后,会自动加上Content-Length。
        options.setTimeOutInMilliSeconds(timeout);
        return options;
    }

    // 安全机制 添加协议
    public void sslSecurity() throws Exception {
        File file = null;
        try {
            System.clearProperty("javax.net.ssl.trustStore");
            Resource resource = new ClassPathResource("com/miitgxt/webservices/xml/client.truststore");
            file = resource.getFile();
            System.setProperty("javax.net.ssl.trustStore", "d:/testclientjdk.truststore");
            System.setProperty("javax.net.ssl.trustStorePassword", "changeit");
            System.setProperty("java.protocol.handler.pkgs", "com.sun.net.ssl.internal.www.protocol");
            Security.addProvider(new com.sun.net.ssl.internal.ssl.Provider());
        } catch (IOException e) {
            e.printStackTrace();
            throw new Exception("安全认证失败!");
        }
    }

    public void deleteFile(String path) {
        File file = null;
        try {
            if (StringUtils.trimToNull(path) != null) {
                file = new File(path);
                if (file != null) {
                    if (file.isFile()) {
                        file.delete();
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
        }
    }
}

转载于:https://my.oschina.net/lwhmdj0823/blog/550581

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值