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 { } } }
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。