webservice客户端访问写法

Webservice 客户端访问服务端的几种方式:

1,使用axis2 访问,经测试方式java开发的服务端可以访问,.net的不能访问

public static void main(String[] args) throws AxisFault {

String sendXml = "<?xml version=\"1.0\" encoding=\"utf-8\"?>"+

"<Msg>"+

"<Head>"+

   "<InterfaceCode>transWrapOrderDownload</InterfaceCode>"+

   "<InterfaceDescription>卷包工单下达</InterfaceDescription>"+

    "<MsgID></MsgID>"+

      "<Source>JBSC</Source>"+

      "<MsgMark>JBSC_ESB_Service</MsgMark>"+

      "<WsMethod>SaveData</WsMethod>"+

      "<Date></Date>"+

      "<Cryp></Cryp>"+

      "<User>MES</User>"+

      "<StateCode>000</StateCode>"+

      "<StateDesription>success</StateDesription>"+

   "</Head>"+

   "<Data>"+

      "<Detail>"+

         "<BatchCode>112222</BatchCode>"+

         "<OrderCode>121</OrderCode>"+

         "<MaterialCode>B12O99</MaterialCode>"+

         "<MaterialName>baisha</MaterialName>"+

         "<EquCode>EQ521</EquCode>"+

         "<EquName>jb</EquName>"+

         "<Shift>1</Shift>"+

         "<Team>2</Team>"+

         "<OrderState>1</OrderState>"+

         "<PlanQuantity>13000</PlanQuantity>"+

         "<Unit>wang</Unit>"+

         "<UnitName>talkweb</UnitName>"+

         "<StartTime>1222</StartTime>"+

         "<EndTime>12222</EndTime>"+

      "</Detail>"+

   "</Data>"+

"</Msg>";

//1.准备工作

        RPCServiceClient serviceClient = new RPCServiceClient();//使用RPC方式创建客户端对豿

        Options options = serviceClient.getOptions(); //创建options对象

        EndpointReference targetEPR = new EndpointReference( //指定调用WebService的URL

                "http://localhost:8080/AxisService/services/myService");

        options.setTo(targetEPR); //设置选项

        //2.声明变量

        Object[] opAddEntryArgs = null; //指定getGreeting方法的参数忿

        Class[] classes = null; //指定getGreeting方法返回值的数据类型

        QName opAddEntry = null; //指定要调用的getGreeting方法及WSDL文件的命名空闿

        

        //3.调用getGreeting方法

        opAddEntryArgs = new Object[] {"超人"};

        classes = new Class[] {String.class};

//是命名空间targetNamespace,访问方法

        opAddEntry = new QName("http://service.business.sail", "getGreeting");

        Object [] obj = serviceClient.invokeBlocking(opAddEntry, opAddEntryArgs, classes);

        

        //4.调用update方法,注意:update方法没有回忼,承以使用invokeRobust方法调用

        opAddEntry = new QName("http://service.business.sail", "update");

        opAddEntryArgs = new Object[]{"123"};

        serviceClient.invokeRobust(opAddEntry, opAddEntryArgs);

        

        //5.调用getGreeting方法

        opAddEntryArgs = new Object[] {sendXml};

        classes = new Class[] {String.class};

        opAddEntry = new QName("http://service.business.sail", "transWrapOrderDownload");

        Object [] result = serviceClient.invokeBlocking(opAddEntry, opAddEntryArgs, classes);

        

        //5.打印结果

       // System.out.println(obj[0]);//getGreeting方法输出返回倿

        System.out.println(result[0]); //getGreeting方法输出返回倿

}

2,使用axis ,访问.net发布的服务端

import org.apache.axis.client.Call;

import org.apache.axis.client.Service;

import javax.xml.namespace.QName;

import javax.xml.rpc.ParameterMode;

import javax.xml.rpc.ServiceException;

import javax.xml.rpc.encoding.XMLType;

import org.dom4j.Document;  

import org.dom4j.Element;

try {

String endpoint = "http://localhost:8089/MES_ESB_Service.asmx?wsdl";

Service service = new Service();

Call call = (Call) service.createCall();

call.setTargetEndpointAddress(endpoint);

call.setOperationName(new QName(webservice_namespace, remote_method));// WSDL里面描述的接口名称 // SaveData

call.addParameter(new QName(webservice_namespace, "xml"),XMLType.XSD_STRING, ParameterMode.IN);

// call.addParameter(new QName(webservice_namespace, "args"),// XMLType.XSD_STRING, ParameterMode.IN);

call.setReturnType(org.apache.axis.encoding.XMLType.XSD_STRING);// 设置返回类型

call.setSOAPActionURI("http://tempuri.org/SaveData"); // /getLocalJson

// String args =// "[{Key:\"rownum\",Value:3},{Key:\"psw\",Value:\"e10adc3949ba59abbe56e057f20f883e\"}]";

//接收的返回信息

resXml = (String) call.invoke(new Object[] { sendXml });

} catch (RemoteException e) {

e.printStackTrace();

} catch (ServiceException e) {

e.printStackTrace();

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值