加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

webservice客户端代码

发布时间:2020-12-16 22:26:20 所属栏目:安全 来源:网络整理
导读: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
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 url,int timeout) throws Exception;

?? ?public abstract OMFactory createOMFactory();


?? ?public abstract OMElement netPost(String url,String action,String methodStr,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,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,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[] 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,int timeout) { ?? ??? ?OMElement result = null; ?? ??? ?ServiceClient client = null; ?? ??? ?try { ?? ??? ??? ?client = new ServiceClient(); ?? ??? ??? ?client.setOptions(getClientOptions(action,url,timeout)); ?? ??? ??? ?result = client.sendReceive(getOMMethod(methodStr,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,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 { ?? ??? ?} ?? ?} }

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读