用Java做客户端调用.NET写的 Web Services

1. 用VS.NET新建一个ASP.NET web 服务,命名为MathServices.asmx。代码如下:

using System; using System.Web; using System.Web.Services; using System.Web.Services.Protocols;

[WebService(Namespace = "http://localhost/WebSite/MathService/MathServices.asmx", Description = "算术运算( + - * / )服务")] [WebServiceBinding(ConformsTo = WsiProfiles.None)] public class MathServices : System.Web.Services.WebService {     public MathServices()     {

        //如果使用设计的组件,请取消注释以下行         //InitializeComponent();     }     [SoapRpcMethod(Action = "http://localhost/WebSite/MathService/MathServices.asmx/Add")]     [WebMethod(Description = "两单精度数相加")]     public float Add(float A, float B)     {         return A + B;     }     [SoapRpcMethod(Action = "http://localhost/WebSite/MathService/MathServices.asmx/Subtract")]     [WebMethod(Description = "两单精度数相减")]     public float Subtract(float A, float B)     {         return A - B;     }     [SoapRpcMethod(Action = "http://localhost/WebSite/MathService/MathServices.asmx/Multiply")]     [WebMethod(Description = "两单精度数相乘")]     public float Multiply(float A, float B)     {         return A * B;     }     [SoapRpcMethod(Action = "http://localhost/WebSite/MathService/MathServices.asmx/Divide")]     [WebMethod(Description = "两单精度数相除")]     public float Divide(float A, float B)     {         if (B == 0)             return -1;         return A / B;     }

}

在Web.Config中需加上

<system.web>     <webServices>       <conformanceWarnings>         <remove name='BasicProfile1_1'/>       </conformanceWarnings>     </webServices> </system.web>

2. 打开Eclipse,新建一个项目,添加一个java class ,命名为TestNetService,输入下列代码:

package myWebServiceJavaClient;

import org.apache.axis.client.Call; import org.apache.axis.client.Service; import javax.xml.namespace.QName;

/** * <p>Title: </p> * <p>Description: </p> * <p>Copyright: Copyright (c) 2008</p> * <p>Company: </p> * @author XiangWei * @version 1.0 */ public class TestNetService {  public TestNetService() {  }  public static void main(String[] args) {   try {      Float i = new Float(7);    Float j = new Float(3);    String endpoint="http://localhost/WebSite/MathService/MathServices.asmx";    Service service = new Service();    Call call = (Call)service.createCall();    call.setTargetEndpointAddress(new java.net.URL(endpoint));    call.setOperationName(new QName("http://localhost/WebSite/MathService/MathServices.asmx","Multiply"));    call.addParameter("A",org.apache.axis.encoding.XMLType.XSD_DATE,javax.xml.rpc.ParameterMode.IN);    call.addParameter("B",org.apache.axis.encoding.XMLType.XSD_DATE,javax.xml.rpc.ParameterMode.IN);    call.setReturnType(org.apache.axis.encoding.XMLType.XSD_FLOAT);    call.setUseSOAPAction(true);    call.setSOAPActionURI("http://localhost/WebSite/MathService/MathServices.asmx/Multiply");    Float k = (Float)call.invoke(new Object[]{i,j});    System.out.println( "result is " + k.toString() + ".");   }   catch (Exception e) {    System.err.println(e.toString());   }  } }

遇到的问题: 1.抛出一个SoapAction异常。 在MathServices中的 [web method] 的上一行添加下列代码: [SoapRpcMethod(Action = "http://localhost/WebSite/MathService/MathServices.asmx/Add")] [SoapRpcMethod(Action = "http://localhost/WebSite/MathService/MathServices.asmx/Subtract")] [SoapRpcMethod(Action = "http://localhost/WebSite/MathService/MathServices.asmx/Multiply")] [SoapRpcMethod(Action = "http://localhost/WebSite/MathService/MathServices.asmx/Divide")]

2.测试服务时出现:此 Web 服务不符合 WS-I Basic Profile v1.1 a.并将类的WebServiceBinding 属性中ConformsTo 指定为WsiProfiles.None b.创建一个配置文件,在其中添加 <configuration>   <system.web>     <webServices>       <conformanceWarnings>         <remove name='BasicProfile1_1'/>       </conformanceWarnings>     </webServices>   </system.web> </configuration>

3.安装Axis 1.copy axis安装包下webapps文件夹中的axis目录到tomcat的webapps下发布axis应用 2.把axis用到的jar包添加到CLASSPATH环境变量中去。如下: F:/tomcat/webapps/axis/WEB-INF/lib/axis.jar; F:/tomcat/webapps/axis/WEB-INF/lib/jaxrpc.jar; F:/tomcat/webapps/axis/WEB-INF/lib/saaj.jar; F:/tomcat/webapps/axis/WEB-INF/lib/commons-discovery-0.2.jar; F:/tomcat/webapps/axis/WEB-INF/lib/commons-logging-1.0.4.jar; F:/tomcat/webapps/axis/WEB-INF/lib/wsdl4j-1.5.1.jar;

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值