java调用net_Java调用C#.net开发的WebService

1.开发C#.net的WebService服务

1.1点击“开始”->“程序”-> "Microsoft Visual Studio 2005" -> "Microsoft Visual Studio 2005",打开.net界面

1.2 选择“文件”-> “新建” -> “网站”,选择“ASP.NET Web服务”,命名为WebServiceHelloWord,“确定”;

1.3 WebService工程建好后,能看到默认的HelloWord方法

[WebMethod]

public string HelloWorld(string) {

return "Hello World ";

}

修改此方法为:

[WebMethod]

[SoapDocumentMethodAttribute(Action = "http://microsoft.com/webservices/HelloWorld", RequestNamespace = "http://microsoft.com/webservices/T", ResponseNamespace = "http://microsoft.com/webservices/T", ResponseElementName = "arithmeticMeanResponse", Use = SoapBindingUse.Literal)]

public string HelloWorld(string name) {

return "Hello World "  + name;

}

增加参数name和SoapDocumentMethodAttribute设置,注意SoapDocumentMethodAttribute必须设置,否则java调用C#.net时会调用不成功。

1.4 编译并部署到服务器的IIS上,命名的名字为websh,地址为http://localhost/websh/Service.asmx

2.开发java客户端

2.1 新建java工程WbeServiceTest,新建java类WST,具体代码如下

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 WST{

/**

* @param args

* @throws ServiceException

* @throws MalformedURLException

* @throws RemoteException

*/

public static void main(String[] args) throws ServiceException, MalformedURLException, RemoteException {

//   TODO Auto-generated method stub

// WebService URL

String service_url = "http://localhost/websh/Service.asmx";

Service service = new Service();

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

call.setTargetEndpointAddress(new java.net.URL(service_url));

// 设置要调用的方法

call.setOperationName(new QName("http://microsoft.com/webservices/T", "HelloWorld"));

// 该方法需要的参数

call.addParameter(new QName("http://microsoft.com/webservices/T","name"), org.apache.axis.encoding.XMLType.XSD_STRING,

javax.xml.rpc.ParameterMode.IN);

// 方法的返回值类型

call.setReturnType(org.apache.axis.encoding.XMLType.XSD_STRING);

call.setUseSOAPAction(true);

call.setSOAPActionURI("http://microsoft.com/webservices/HelloWorld");

// 调用该方法

String res = call.invoke(new Object[] {"rock"}).toString();

System.out.println(" Result: " + res.toString());

}

}

2.2 客户端开发完毕,执行这个类,就能看到返回的结果: Result: Hello World rock 表明调用成功

在java调用C#.net开发的WebService过程中,特别要配置[SoapDocumentMethodAttribute(...)]

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值