java调用wcf控件的两种交互
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
總結
以上是生活随笔為你收集整理的java调用wcf控件的两种交互的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: n+电子烟在哪里可以买得到?
- 下一篇: 想给爱车换个膜,汽车膜粘贴合格的标准是什