java调用wcf控件的两种交互

1.axis方式

package wsdl.axis;

import java.net.MalformedURLException;
import java.rmi.RemoteException;

import javax.xml.namespace.QName;
import javax.xml.rpc.ServiceException;

import org.apache.axis.client.Call;
import org.apache.axis.client.Service;

public class Test2 {

 /**
  * @param args
  */
 public static void main(String[] args) {
  String str = "";
  String endpoint = "http://172.50.9.161:4000/Marine/service";
  String namespace = "http://www.Service.Marine";
  String methodName = "Test2";
  Service service = new Service();
  Call call = null;
  try {
   call = (Call) service.createCall();
  } catch (ServiceException e1) {
   e1.printStackTrace();
  }
  try {
   call.setTargetEndpointAddress(new java.net.URL(endpoint));
  } catch (MalformedURLException e1) {
   e1.printStackTrace();
  }
  call.setUseSOAPAction(true);
  call.setSOAPActionURI("http://www.Service.Marine/IMarineService/Test2");
  call.setOperationName(new QName(namespace, methodName));
  call.addParameter(new javax.xml.namespace.QName(namespace, "strTest"),
    org.apache.axis.encoding.XMLType.XSD_STRING,
    javax.xml.rpc.ParameterMode.IN);// document访问格式,必须
  call.setReturnType(org.apache.axis.encoding.XMLType.XSD_STRING);
  try {
   str = (String) call.invoke(new Object[] { "sss"});
  } catch (RemoteException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
   
  }
  System.out.println("服务器返回值是:" + str + "!");
 }
}

 

注意 1)这里需要注意的参数名称问题,java的实参名称要与.net的形参保持一致才能在.net端口接受到。

         2)本地静态调用的时候无错误,但是放在web工程下运行,程序返回等都正常,但是java控制台会抛出找不到方法名称的异常。目前还没有找到具体原因

2.cxf方式

package cxf;

import javax.xml.namespace.QName;

import org.apache.cxf.endpoint.Client;
import org.apache.cxf.jaxws.endpoint.dynamic.JaxWsDynamicClientFactory;

public class CallDNet {
 /**
  * @param args
  */
 public static void main(String[] args) {
  Object[] replys = null;
  try {
   JaxWsDynamicClientFactory dcf = JaxWsDynamicClientFactory
     .newInstance();
   Client client = dcf
     .createClient("http://127.0.0.1:4000/Marine/service?wsdl");
   String namespace = "http://www.Service.Marine";
   replys = client.invoke(new QName(namespace, "Test2"), "dsadsadsa");
  } catch (Exception e) {
   e.printStackTrace();
  }
  for (Object o : replys) {
   // System.out.println(o);
  }
  System.out.println(replys[0]);
 }

}

 说明一下,2种jar同时存在一个工程下的时候,需要吧cxf的依赖包放在axis依赖包的前面加载。而且cxf形式的参数限制宽松,不需要对应具体参数名称,而且参数个数多的时候系统也补会报错

3.axis2 调用方式

package client;

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.Constants;
import org.apache.axis2.addressing.EndpointReference;
import org.apache.axis2.client.Options;
import org.apache.axis2.client.ServiceClient;

public class Axis2ServiceClient {

    public static void main(String[] args) throws AxisFault {
        EndpointReference targetEPR = new EndpointReference("http://172.50.9.159:4000/Marine/service?wsdl");
        OMFactory fac = OMAbstractFactory.getOMFactory();
        OMNamespace omNs = fac.createOMNamespace("http://www.Service.Marine", "tns");//命名空间
        OMElement method2 = fac.createOMElement("Test2", omNs);//要调用的接口方法名称
        OMElement value1 = fac.createOMElement("strTest", omNs);//方法的第一个参数名称
        value1.addChild(fac.createOMText(value1, "ccc"));//设定参数的值
        method2.addChild(value1);//方法设置参数

        Options options = new Options();
        options.setAction("http://www.Service.Marine/IMarineService/Test2");
        options.setTo(targetEPR);//设定webservice地址
        options.setTransportInProtocol(Constants.TRANSPORT_HTTP);//设定传输协议
        ServiceClient sender = new ServiceClient();
        sender.setOptions(options);

        OMElement result2 = sender.sendReceive(method2);//调用接口方法
        System.out.println(result2);//打印接口返回结果
    }
}

最少依赖jar

你可能感兴趣的:(#,webservice)