1、解决无法识别请求元素问题
在c#类名的空间下面添加 [WebService(Namespace = "http://tempuri.org/")]
java代码修改:
mCall.setOperationName(new QName("http://tempuri.org/","HelloWord"));
2、解决参数丢失问题
在C#类中每一个webservice方法前面添加:
[SoapRpcMethod(Use = SoapBindingUse.Literal, Action = "http://tempuri.org/HelloWord",
RequestNamespace = "http://tempuri.org/", ResponseNamespace = "http://tempuri.org/")]
服务端:
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;
}
}
}
客户端
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);
}
}
部分摘自:http://bbs.csdn.net/topics/390617053