package com.hj.services.webservices;
import java.net.URL;
import javax.xml.namespace.QName;
import javax.xml.rpc.ServiceException;
import org.apache.axis.client.Call;
import org.apache.axis.client.Service;
import org.apache.cxf.jaxws.JaxWsProxyFactoryBean;
import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
public class WebServicesClient {
public static void main(String[] args) throws Exception {
//new 一个服务
Service sv = new Service();
//创建一个call对象
Call call = (Call) sv.createCall();
//设置要调用的接口地址以上一篇的为例子
call.setTargetEndpointAddress(new URL("http://192.168.200.39:8080/creazy"));
//设置要调用的接口方法
call.setOperationName(new QName("getUsers"));
//设置参数名 id 第二个参数表示String类型,第三个参数表示入参
call.addParameter("id", org.apache.axis.encoding.XMLType.XSD_STRING,javax.xml.rpc.ParameterMode.IN);
//返回参数类型
call.setReturnType(org.apache.axis.encoding.XMLType.XSD_STRING);
//开始调用方法,假设我传入的参数id的内容是1001 调用之后会根据id返回users信息,
// 以xml格式的字符串返回,也可以json格式主要看对方用什么方式返回
String result = (String) call.invoke(new Object[]{"1001"});
//打印字符串
System.out.println(result);
//转成Document对象
Document doc = DocumentHelper.parseText(result);
//用dom4j方式拿到xml的根节点然后打印结果信息
Element root = doc.getRootElement();
System.out.println("id="+root.element("UsersID").getText()+" name="+root.element("UsersName").getText()+" sex="+root.element("UsersSex").getText());
}
}
/**
* 将数据发送到对方 webservice 接口里
* @param
* @return
* @throws Exception
*/
public static String sendMethod(String xml,String wsdl_ulr,String OperationCode) throws Exception
{
//new 一个服务
Service sv = new Service();
//创建一个call对象
Call call = (Call) sv.createCall();
//设置要调用的接口地址以
call.setTargetEndpointAddress(new URL(wsdl_ulr));
//设置要调用的接口方法
//new QName的URL是要指向的命名空间的名称,这个URL地址在你的wsdl打开后可以看到的,
//上面有写着targetNamespace="http://*.*.*/",这个就是你的命名空间值了;
call.setOperationName(new QName("http://10.0.0.0:7002/webapp/services/DTIService","processMessage"));
//设置参数名 id 第二个参数表示String类型,第三个参数表示入参
call.addParameter("arg0", org.apache.axis.encoding.XMLType.XSD_STRING, javax.xml.rpc.ParameterMode.IN);
call.addParameter("arg1", org.apache.axis.encoding.XMLType.XSD_STRING, javax.xml.rpc.ParameterMode.IN);
call.addParameter("arg2", org.apache.axis.encoding.XMLType.XSD_STRING, javax.xml.rpc.ParameterMode.IN);
call.addParameter("arg3", org.apache.axis.encoding.XMLType.XSD_STRING, javax.xml.rpc.ParameterMode.IN);
//返回参数类型
call.setReturnType(org.apache.axis.encoding.XMLType.XSD_STRING);
//开始调用方法,以xml格式的字符串返回,也可以json格式主要看对方用什么方式返回
String result = (String) call.invoke( new Object[] { "03", "00", OperationCode, xml });
//打印字符串
System.out.println(result);
return result;
}
XFire方式
public boolean CallWebServiceCaseSender(String xmlString, String url)throws MalformedURLException {
//ICaseWrite.class 为接口
org.codehaus.xfire.service.Service serviceModel = new ObjectServiceFactory().create(ICaseWrite.class);
ICaseWrite servcie;
servcie = (ICaseWrite) new XFireProxyFactory().create(serviceModel,url);
boolean bs = servcie.caseWrite(xmlString);
return bs;
}
public interface ICaseWrite {
public boolean synchXfDclx(String caseid);
public boolean caseWrite(String xml);
}