java和.net互相调用webservice注意事项

最后在研究GWT,需要用到.net 的webservice,网上搜了点代码,基本上问题多多.

Java要调用.net的webservice是需要一个axis的包的. 需要下载,然后引用.

引用的时候,需要注意一些小问题.

Java 端

public String ServiceLogon(String user, String pwd) throws ServiceException  
    {  
        String url="http://localhost/WebServices/Service.asmx";  
        Service service = new Service();  
        Call call = (Call)service.createCall();  
        //We have to figure our the namespace. The java doesn't know where is the namespace  
        call.setOperationName(new QName("http://localhost/WebServices/Service.asmx","Logon"));  
          
        call.addParameter(new QName("http://localhost/WebServices/Service.asmx","username"),XMLType.XSD_STRING, javax.xml.rpc.ParameterMode.IN);  
        call.addParameter(new QName("http://localhost/WebServices/Service.asmx","password"),XMLType.XSD_STRING, javax.xml.rpc.ParameterMode.IN);  
        String res = "";  
        try {  
            call.setTargetEndpointAddress(new URL("http://localhost/WebServices/Service.asmx"));  
            res = (String)call.invoke(new Object[]{user,pwd});   
        } catch (MalformedURLException e) {  
            // TODO Auto-generated catch block  
            e.printStackTrace();  
        } catch (RemoteException e) {  
            // TODO Auto-generated catch block  
            e.printStackTrace();  
        }  
        return res;  
    } 
public String ServiceLogon(String user, String pwd) throws ServiceException
 {
  String url="http://localhost/WebServices/Service.asmx";
  Service service = new Service();
  Call call = (Call)service.createCall();
  //We have to figure our the namespace. The java doesn't know where is the namespace
  call.setOperationName(new QName("http://localhost/WebServices/Service.asmx","Logon"));
  
  call.addParameter(new QName("http://localhost/WebServices/Service.asmx","username"),XMLType.XSD_STRING, javax.xml.rpc.ParameterMode.IN);
  call.addParameter(new QName("http://localhost/WebServices/Service.asmx","password"),XMLType.XSD_STRING, javax.xml.rpc.ParameterMode.IN);
  String res = "";
  try {
   call.setTargetEndpointAddress(new URL("http://localhost/WebServices/Service.asmx"));
   res = (String)call.invoke(new Object[]{user,pwd});
  } catch (MalformedURLException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  } catch (RemoteException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }
        return res;
 }


需要注意的一个小问题是, 在引用的时候,要指出.net webservice的namespace.不同的代码namespace不一样的.

还有添加参数的时候, 一定要new 出一个QName来才可以.

call.addParameter(new QName("http://localhost/WebServices/Service.asmx","username"),XMLType.XSD_STRING, javax.xml.rpc.ParameterMode.IN); 不new的话, 接收到的参数会是空的.

.net 端.

view plaincopy to clipboardprint?
using System;  
using System.Web;  
using System.Web.Services;  
using System.Web.Services.Protocols;  
[WebService(Namespace = "http://localhost/WebServices/Service.asmx")] //namespace 
[SoapDocumentService(RoutingStyle=SoapServiceRoutingStyle.RequestElement)] //very import must figure out  
public class Service : System.Web.Services.WebService  
{  
    public Service () {  
        //Uncomment the following line if using designed components   
        //InitializeComponent();   
    }  
    [WebMethod]  
    public string HelloWorld() {  
        return "Hello World";  
    }  
    [WebMethod]  
    public string Logon(string username, string password)  
    {  
        if (username == "steven_wang" && password == "123123")  
            return "Welcome to Asp.net WebService";  
        else 
            return "username:" +username +", Password:" + password;  
    }  
      

using System;
using System.Web;
using System.Web.Services;
using System.Web.Services.Protocols;
[WebService(Namespace = "http://localhost/WebServices/Service.asmx")] //namespace
[SoapDocumentService(RoutingStyle=SoapServiceRoutingStyle.RequestElement)] //very import must figure out
public class Service : System.Web.Services.WebService
{
    public Service () {
        //Uncomment the following line if using designed components
        //InitializeComponent();
    }
    [WebMethod]
    public string HelloWorld() {
        return "Hello World";
    }
    [WebMethod]
    public string Logon(string username, string password)
    {
        if (username == "steven_wang" && password == "123123")
            return "Welcome to Asp.net WebService";
        else
            return "username:" +username +", Password:" + password;
    }
   
}
 

本代码在vs2005 framework2.0, windowXP, IIS5.0

JDK1.6, Eclipse3.3, axis1.4

情况下调试通过.

 

本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/w59879213/archive/2009/03/28/4031300.aspx

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值