关于java调用c#webservice参数传递为空问题解决

a477988140 2013-10-16 11:40:29
通过Myeclipse10.0 jdk1.7调用VS2012 webService遇到java客户端参数传递不过去的问题,搞了一下午终于出结果了,其实网上好多方法都只是一部分,需要综合一下。


客户端我是用import org.apache.axis.client.Call;方法调用服务成功解决该问题的的,通过RPCServiceClient以及axis2自动生成的stub调用均未找到合适的传递参数的方法


client:

import java.net.MalformedURLException;
import java.rmi.RemoteException;

import javax.xml.namespace.QName;
import javax.xml.rpc.ParameterMode;
import javax.xml.rpc.ServiceException;

import org.apache.axis.client.Call;
import org.apache.axis.client.Service;
import org.apache.axis.encoding.XMLType;
import org.apache.axis2.client.Options;
import org.apache.axis2.rpc.client.RPCServiceClient;

public class test4 {
public static void main(String[] args) throws RemoteException, MalformedURLException, ServiceException {
String service_url = "http://localhost:30332/WebService1.asmx";
Service ser = new Service();
Call call = (Call) ser.createCall();
call.setTargetEndpointAddress(new java.net.URL(service_url));
QName opAddEntry = new QName("http://tempuri.org/", "HelloWorld");
call.setOperationName(opAddEntry);
call.addParameter("name", XMLType.XSD_STRING, ParameterMode.IN);
call.setReturnType(XMLType.XSD_STRING);
Object[] opAddEntryArgs = new Object[] { "jone" };
String result = (String) call.invoke(opAddEntryArgs);
call.invoke("http://tempuri.org/", "HelloWorld", opAddEntryArgs);
System.out.println("result:" + result);
}
}
服务端:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Web.Services.Description;
using System.Web.Services.Protocols;
using System.Diagnostics;
namespace WebApplication1
{
/// <summary>
/// WebService1 的摘要说明
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.ComponentModel.ToolboxItem(false)]
// 若要允许使用 ASP.NET AJAX 从脚本中调用此 Web 服务,请取消注释以下行。
// [System.Web.Script.Services.ScriptService]
[SoapDocumentService(RoutingStyle = SoapServiceRoutingStyle.RequestElement)]
public class WebService1 : System.Web.Services.WebService
{
[SoapRpcMethod(Use = SoapBindingUse.Literal, Action = "http://tempuri.org/HelloWord", RequestNamespace = "http://tempuri.org/", ResponseNamespace = "http://tempuri.org/")]
[WebMethod]
public string HelloWorld(string name)
{
//Console.WriteLine("name:"+name);
//Console.ReadKey();
Debug.WriteLine("NAME:"+name);
return "hello"+name;
}
[SoapRpcMethod(Use = SoapBindingUse.Literal, Action = "http://tempuri.org/add", RequestNamespace = "http://tempuri.org/", ResponseNamespace = "http://tempuri.org/")]
[WebMethod]
public int add(int x, int y)
{
return x + y;
}

}
}

调用结果:

result:hellojone


1 call.addParameter("name", XMLType.XSD_STRING, ParameterMode.IN);
call.setReturnType(XMLType.XSD_STRING);

2[SoapRpcMethod(Use = SoapBindingUse.Literal, Action = "http://tempuri.org/HelloWord", RequestNamespace = "http://tempuri.org/", ResponseNamespace = "http://tempuri.org/")] (c#中每个方法前都要添加这样的声明,否则会报错)

这两步是必要条件缺一不可,貌似是用以匹配二者的命名空间



3[SoapDocumentService(RoutingStyle = SoapServiceRoutingStyle.RequestElement)]

本方法加在类之前,旨在防止java调用时出现无法识别 soapAction的问题

PS:1 同学用eclipse做客户端直接用exis2自动生成的stub操作即可调用成功

HelloWorld helloWorld = new HelloWorld();
helloWorld.setName("jone");
HelloWorldResponse re=stub.HelloWorld(helloWorld);

System.out.println( re.getHelloWorldResult());

但是我跟他C/S代码完全相同却出现这个问题,说明,跟客户端IDE有关。

2通过RPCServiceClient以及axis2自动生成的stub调用如果可以通过某种API实现两边的命名空间一致,或许也能传递成功,这里我并未深究

3 按原理通过axis2生成stub调用时正确利用了服务器端的wsdl实现的调用功能,但这里苦于调用java编辑的服务器webservice抛出wrong number of arguments,调用C#webservice出现参数传递失败问题,目前仅能用本帖方法实现,这也可可能跟IDE有关,同学用eclipse就为出现此类情况。。。请大家引以为戒








...全文
795 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
qq_14897909 2016-03-17
  • 打赏
  • 举报
回复
非常感谢,可以运行了,纠结了好久
u013786479 2014-02-26
  • 打赏
  • 举报
回复
按说你上面的做了,为什么还是不行,传进去的还是空的,求回复

67,516

社区成员

发帖
与我相关
我的任务
社区描述
J2EE只是Java企业应用。我们需要一个跨J2SE/WEB/EJB的微容器,保护我们的业务核心组件(中间件),以延续它的生命力,而不是依赖J2SE/J2EE版本。
社区管理员
  • Java EE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧