java调用.net的webservice
一.参考文献
1. http://www.cnblogs.com/xuqifa100/archive/2007/12/13/993926.html 使用.net如何发布web service
3.Java调用DotNet WebService为什么那么难?
二.概述
前面写了一篇博客eclipse+webservice 是在java环境下进行的。考虑到webservice的跨系统,跨语言,跨网络的特性,下面写了一个实例来测试其跨语言的的特性。
首先是用asp.net开发一个webservice,然后再java中创建客户端来调用这个service。
三.实例
(1)打开visual studio 2010,新建项目,如下图所示:

新建的项目结果如下图所示:

(2)在Service1.asmx.cs中添加服务方法,代码如下:
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Web;
- using System.Web.Services;
- namespace AspWebService1
- {
- /// <summary>
- /// Service1 的摘要说明
- /// </summary>
- [WebService(Namespace = "http://erplab.sjtu.edu/")]
- [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
- [System.ComponentModel.ToolboxItem(false)]
- // 若要允许使用 ASP.NET AJAX 从脚本中调用此 Web 服务,请取消对下行的注释。
- // [System.Web.Script.Services.ScriptService]
- public class Service1 : System.Web.Services.WebService
- {
- [WebMethod]
- public string HelloWorld()
- {
- return "Hello World";
- }
- [WebMethod]
- public string sayHelloToPersonNew(String name)
- {
- if (name == null)
- {
- name = "nobody";
- }
- return "hello," + name;
- }
- [WebMethod]
- public double count(double number, double price, double discount)
- {
- return number * price * discount;
- }
- [WebMethod]
- public float getFloat(float x)
- {
- return x;
- }
- //加法
- [WebMethod]
- public float plus(float x, float y)
- {
- return x + y;
- }
- //减法
- [WebMethod]
- public float minus(float x, float y)
- {
- return x - y;
- }
- //乘法
- [WebMethod]
- public float multiply(float x, float y)
- {
- return x * y;
- }
- //除法
- [WebMethod]
- public float divide(float x, float y)
- {
- if (y != 0)
- {
- return x / y;
- }
- else
- return -1;
- }
- }
- }
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services; namespace AspWebService1
{
/// <summary>
/// Service1 的摘要说明
/// </summary>
[WebService(Namespace = "http://erplab.sjtu.edu/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.ComponentModel.ToolboxItem(false)]
// 若要允许使用 ASP.NET AJAX 从脚本中调用此 Web 服务,请取消对下行的注释。
// [System.Web.Script.Services.ScriptService]
public class Service1 : System.Web.Services.WebService
{ [WebMethod]
public string HelloWorld()
{
return "Hello World";
} [WebMethod]
public string sayHelloToPersonNew(String name)
{
if (name == null)
{
name = "nobody";
}
return "hello," + name;
} [WebMethod]
public double count(double number, double price, double discount)
{
return number * price * discount;
} [WebMethod]
public float getFloat(float x)
{
return x;
} //加法
[WebMethod]
public float plus(float x, float y)
{
return x + y;
} //减法
[WebMethod]
public float minus(float x, float y)
{
return x - y;
} //乘法
[WebMethod]
public float multiply(float x, float y)
{
return x * y;
} //除法
[WebMethod]
public float divide(float x, float y)
{
if (y != 0)
{
return x / y;
}
else
return -1;
} }
}
(3)发布服务,按CTRL+F5运行项目,即可打开服务首页:http://localhost:5329/Service1.asmx,如下图所示:

上图中显示的就是我们在Service1.asmx.cs文件中定义的服务方法。点击“服务说明”可以查看webservice的wsdl文件。
(4)编写java客户端来测试webservice,java程序如下所示:
- package edu.sjtu.erplab.aspwebservice;
- import javax.xml.namespace.QName;
- import javax.xml.rpc.ParameterMode;
- import org.apache.axis.client.Call;
- import org.apache.axis.client.Service;
- import org.apache.axis.encoding.XMLType;
- public class AspWebServiceTestClient1 {
- public static void main(String[] args) throws Exception {
- // 定义方法
- String method = "HelloWorld";
- String methodPlus = "plus";
- String methodMinus = "minus";
- String methodMultiply = "multiply";
- String methodDivide = "divide";
- String methodSayTo = "sayHelloToPersonNew";
- // 定义服务
- Service service = new Service();
- // 测试1:调用HelloWorld方法,方法没有参数
- Call call = (Call) service.createCall();
- call.setTargetEndpointAddress(new java.net.URL(
- "http://localhost:5329/Service1.asmx"));
- call.setUseSOAPAction(true);
- // 第一种设置返回值类型为String的方法
- call.setReturnType(XMLType.SOAP_STRING);
- call.setOperationName(new QName("http://erplab.sjtu.edu/", method));
- call.setSOAPActionURI("http://erplab.sjtu.edu/HelloWorld");
- String retVal1 = (String) call.invoke(new Object[] {});
- System.out.println(retVal1);
- // 测试2: 调用sayHelloToPersonNew方法,方法有一个参数:name。sayHelloToPersonNew(String name)
- Call call2 = (Call) service.createCall();
- call2.setTargetEndpointAddress(new java.net.URL(
- "http://localhost:5329/Service1.asmx"));
- call2.setUseSOAPAction(true);
- call2.setReturnType(new QName("http://www.w3.org/2001/XMLSchema",
- "string"));
- // 第二种设置返回值类型为String的方法
- call2.setOperationName(new QName("http://erplab.sjtu.edu/", methodSayTo));
- call2.setSOAPActionURI("http://erplab.sjtu.edu/sayHelloToPersonNew");
- call2.addParameter(new QName("http://erplab.sjtu.edu/", "name"),// 这里的name对应参数名称
- XMLType.XSD_STRING, ParameterMode.IN);
- String retVal2 = (String) call2
- .invoke(new Object[] { "asp webservice" });
- System.out.println(retVal2);
- // 测试3: 调用sgetFloat方法,方法有一个参数:x,为float类型
- Call call3 = (Call) service.createCall();
- call3.setTargetEndpointAddress(new java.net.URL(
- "http://localhost:5329/Service1.asmx"));
- call3.setUseSOAPAction(true);
- call3.setEncodingStyle(null);// 必须有,否为会系统报错。最关键的语句。决定生成xmlns的中soap的命名空间
- // 第一种设置返回值类型为Float类型的方法
- call3.setReturnType(org.apache.axis.encoding.XMLType.XSD_FLOAT);
- call3.setOperationName(new QName("http://erplab.sjtu.edu/", "getFloat"));
- call3.setSOAPActionURI("http://erplab.sjtu.edu/getFloat");
- call3.addParameter(new QName("http://erplab.sjtu.edu/", "x"),// 这里的x对应参数名称
- XMLType.XSD_FLOAT, ParameterMode.INOUT);
- Float retVal3 = (Float) call3.invoke(new Object[] { 123 });
- System.out.println(retVal3);
- // 测试4: 调用plus方法,方法有两个参数:x,y。plus(float x, float y)
- Call call4 = (Call) service.createCall();
- call4.setTargetEndpointAddress(new java.net.URL(
- "http://localhost:5329/Service1.asmx"));
- call4.setUseSOAPAction(true);
- call4.setEncodingStyle(null);
- // 第二种设置返回值类型为Float类型的方法
- call4.setReturnType(new QName("http://www.w3.org/2001/XMLSchema",
- "float"));
- call4.setOperationName(new QName("http://erplab.sjtu.edu/", methodPlus));// 加法
- call4.setSOAPActionURI("http://erplab.sjtu.edu/plus");
- call4.addParameter(new QName("http://erplab.sjtu.edu/", "x"),// 参数x
- org.apache.axis.encoding.XMLType.XSD_FLOAT, ParameterMode.IN);
- call4.addParameter(new QName("http://erplab.sjtu.edu/", "y"),// 参数y
- XMLType.XSD_FLOAT, ParameterMode.IN);
- Float retVal4 = (Float) call4.invoke(new Object[] { 5, 6 });
- System.out.println(retVal4);
- }
- }
package edu.sjtu.erplab.aspwebservice; import javax.xml.namespace.QName;
import javax.xml.rpc.ParameterMode;
import org.apache.axis.client.Call;
import org.apache.axis.client.Service;
import org.apache.axis.encoding.XMLType; public class AspWebServiceTestClient1 { public static void main(String[] args) throws Exception {
// 定义方法
String method = "HelloWorld";
String methodPlus = "plus";
String methodMinus = "minus";
String methodMultiply = "multiply";
String methodDivide = "divide";
String methodSayTo = "sayHelloToPersonNew";
// 定义服务
Service service = new Service(); // 测试1:调用HelloWorld方法,方法没有参数
Call call = (Call) service.createCall();
call.setTargetEndpointAddress(new java.net.URL(
"http://localhost:5329/Service1.asmx"));
call.setUseSOAPAction(true);
// 第一种设置返回值类型为String的方法
call.setReturnType(XMLType.SOAP_STRING);
call.setOperationName(new QName("http://erplab.sjtu.edu/", method));
call.setSOAPActionURI("http://erplab.sjtu.edu/HelloWorld");
String retVal1 = (String) call.invoke(new Object[] {});
System.out.println(retVal1); // 测试2: 调用sayHelloToPersonNew方法,方法有一个参数:name。sayHelloToPersonNew(String name)
Call call2 = (Call) service.createCall();
call2.setTargetEndpointAddress(new java.net.URL(
"http://localhost:5329/Service1.asmx"));
call2.setUseSOAPAction(true);
call2.setReturnType(new QName("http://www.w3.org/2001/XMLSchema",
"string"));
// 第二种设置返回值类型为String的方法
call2.setOperationName(new QName("http://erplab.sjtu.edu/", methodSayTo));
call2.setSOAPActionURI("http://erplab.sjtu.edu/sayHelloToPersonNew");
call2.addParameter(new QName("http://erplab.sjtu.edu/", "name"),// 这里的name对应参数名称
XMLType.XSD_STRING, ParameterMode.IN);
String retVal2 = (String) call2
.invoke(new Object[] { "asp webservice" });
System.out.println(retVal2); // 测试3: 调用sgetFloat方法,方法有一个参数:x,为float类型
Call call3 = (Call) service.createCall();
call3.setTargetEndpointAddress(new java.net.URL(
"http://localhost:5329/Service1.asmx"));
call3.setUseSOAPAction(true);
call3.setEncodingStyle(null);// 必须有,否为会系统报错。最关键的语句。决定生成xmlns的中soap的命名空间
// 第一种设置返回值类型为Float类型的方法
call3.setReturnType(org.apache.axis.encoding.XMLType.XSD_FLOAT);
call3.setOperationName(new QName("http://erplab.sjtu.edu/", "getFloat"));
call3.setSOAPActionURI("http://erplab.sjtu.edu/getFloat");
call3.addParameter(new QName("http://erplab.sjtu.edu/", "x"),// 这里的x对应参数名称
XMLType.XSD_FLOAT, ParameterMode.INOUT);
Float retVal3 = (Float) call3.invoke(new Object[] { 123 });
System.out.println(retVal3); // 测试4: 调用plus方法,方法有两个参数:x,y。plus(float x, float y)
Call call4 = (Call) service.createCall();
call4.setTargetEndpointAddress(new java.net.URL(
"http://localhost:5329/Service1.asmx"));
call4.setUseSOAPAction(true);
call4.setEncodingStyle(null);
// 第二种设置返回值类型为Float类型的方法
call4.setReturnType(new QName("http://www.w3.org/2001/XMLSchema",
"float"));
call4.setOperationName(new QName("http://erplab.sjtu.edu/", methodPlus));// 加法
call4.setSOAPActionURI("http://erplab.sjtu.edu/plus");
call4.addParameter(new QName("http://erplab.sjtu.edu/", "x"),// 参数x
org.apache.axis.encoding.XMLType.XSD_FLOAT, ParameterMode.IN);
call4.addParameter(new QName("http://erplab.sjtu.edu/", "y"),// 参数y
XMLType.XSD_FLOAT, ParameterMode.IN);
Float retVal4 = (Float) call4.invoke(new Object[] { 5, 6 });
System.out.println(retVal4);
}
}
运行结果:
- - Unable to find required classes (javax.activation.DataHandler and javax.mail.internet.MimeMultipart). Attachment support is disabled.
- Hello World
- hello,asp webservice
- 123.0
- 11.0
- Unable to find required classes (javax.activation.DataHandler and javax.mail.internet.MimeMultipart). Attachment support is disabled.
Hello World
hello,asp webservice
123.0
11.0
注意点:
(a)我们发现如果参数是String类型的,那么可以不需要设置call的参数 call3.setEncodingStyle(null); 但是如果传入参数为float类型,那么就必须加上这一条语句。
(b)设置返回值类型有两种方式:
一种是
- call.setReturnType(XMLType.SOAP_STRING);
call.setReturnType(XMLType.SOAP_STRING);
另外一种是
- call2.setReturnType(new QName("http://www.w3.org/2001/XMLSchema","string"));
call2.setReturnType(new QName("http://www.w3.org/2001/XMLSchema","string"));
这两种方法是等价的
java调用.net的webservice的更多相关文章
- java调用第三方的webservice应用实例
互联网上面有很多的免费webService服务,我们可以调用这些免费的WebService服务,将一些其他网站的内容信息集成到我们的Web应用中显示. 一些常用的webservice网站的链接地址: ...
- java调用第三方的webservice应用实例【转载】
互联网上面有很多的免费webService服务,我们可以调用这些免费的WebService服务,将一些其他网站的内容信息集成到我们的Web应用中显示. 一些常用的webservice网站的链接地址: ...
- 转 java调用php的webService
1.首先先下载php的webservice包:NuSOAP,自己到官网去下载,链接就不给出来了,自己去google吧 基于NoSOAP我们写了一个php的webservice的服务端,例子如下: ...
- java调用.net的webservice接口
要调用webservice,首先得有接口,用已经写好的接口地址在myEclipse的目标project中,右键->new web service client-> 选择JAX-WS方式,点 ...
- java 调用wsdl的webservice接口 两种调用方式
关于wsdl接口对于我来说是比较头疼的 基本没搞过.一脸懵 就在网上搜 看着写的都很好到我这就不好使了,非常蓝瘦.谨以此随笔纪念我这半个月踩过的坑... 背景:短短两周除了普通开发外我就接到了两个we ...
- Java调用net的webservice故障排除实战分享
转载地址:http://blog.sina.com.cn/s/blog_4c925dca01014y3r.html 前几天公司要接入国外公司的一个业务功能,对方是提供的net产生的webservice ...
- java调用.net的webservice[转]
一.引用jar包. 完整包路径:http://files.cnblogs.com/files/chenghu/axis完整jar包.rar 二.java程序代码如下所示: package edu.sj ...
- java调用sap的webservice(需要登录验证)
1.Base64.java /* * Copyright (C) 2010 The Android Open Source Project * * Licensed under the Apache ...
- wsdl 生成 java 代码 java 使用CXF将wsdl文件生成客户端代码命令java调用第三方的webservice应用实例 推荐使用, 并且设置了 utf8
推荐使用, 并且设置了 utf8 wsdl2java -p cn.smborderservice -encoding utf-8 -d f:\logink\src -all -autoNameRes ...
随机推荐
- Confluence 6 使用 LDAP 授权连接一个内部目录 - 服务器设置
名字(Name) 名字的描述将会帮助你在目录中识别.例如: Internal directory with LDAP Authentication Corporate LDAP for Authent ...
- mysql 视图,事务,存储过程,触发器
一 视图 视图是一个虚拟表(非真实存在),是跑到内存中的表,真实表是硬盘上的表.使用视图我们可以把查询过程中的临时表摘出来,保存下来,用视图去实现,这样以后再想操作该临时表的数据时就无需重写复杂的sq ...
- Python进阶--常用模块
一.模块.包 什么是模块? 模块实质上就是一个python文件,它是用来组织代码的,意思就是说把python代码写到里面,文件名就是模块的名称,test.py test就是模块名称. 什么是包? 包, ...
- spring boot 基础篇 -- 自带图片服务器
我们平时在日常项目中经常会遇到图片的上传和访问的情景,平时我们可能习惯于把图片传到resource或者项项目中的某个位置,这样会有一个缺点,当我们重新项目打包时,这些图片会丢失.为了解决这一缺点,我们 ...
- dp练习(8)——数的划分
1039 数的划分 2001年NOIP全国联赛提高组 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 黄金 Gold 题解 题目描述 Description 将整数 ...
- JavaScript学习总结(十三)——极简主义法编写JavaScript类
前两天在网上无意中发现了一篇使用极简主义法定义JavaScript类的文章,原文链接,这个所谓的"极简主义法"我还是第一次听说,是荷兰程序员Gabor de Mooij提出来的,这 ...
- HDU 1710 二叉树遍历
首先.先序遍历是先访问根节点.然后左节点 然后右节点.从根节点开始 直到它的子节点没有左节点才开始回溯访问上一个节点的右节点.同理.中序遍历 先访问左节点 然后是父节点 然后是右节点.从根节点开始 直 ...
- 当前标识没有对“C:\Windows\Microsoft.NET\Framework64\v4.0.30319\Temporary ASP.NET Files”的写访问权限。
解决方案: PHP程序如果遇到这个问题,请将应用程序池.NET改成无托管代码
- System.Insert - 插入字符串
System.Insert - 插入字符串 procedure Insert( Substr: String; {要插入的字符串; 可以是常量} var Dest: String; {源字符串} In ...
- 在junit中添加fail--有test失败即build Failed
项目使用jenkins做持续集成,ant来构建,发现在跑junit单元测试的时候,如果有test case失败了,ci的状态是黄色的unstable,而不是红色的failed,看起来很不爽.个人觉得b ...