.net webservice方法参数问题

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了.net webservice方法参数问题相关的知识,希望对你有一定的参考价值。

如题: webservice1.asmx中有一个方法, 里面有三个参数
[WebMethod] public string Test(string param1, string param2, string param3)


现在需要这样一个功能:http方式调用这个webservice方法时,少传或者不传某个参数时报出自定义错误:缺少某个参数,比如:http://127.0.0.1/webservice1.asmx/test?param1=1¶m2=2这时候就报出缺少参数param3,解决分加满,求解

参考技术A c#写的service接口,部署测试正常。注释必须有,不然无法获取xml字符串

using System;
using System.Web;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.Web.Services.Description;

[WebService(Namespace = "http://hoteamsoft.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]

public class Service : System.Web.Services.WebService

public Service ()

//Uncomment the following line if using designed components
//InitializeComponent();

//下面的引用关系必须加----begin
[SoapRpcMethod(Action = "http://hoteamsoft.org/HelloWorld1",
RequestNamespace = "http://hoteamsoft.org/T",
ResponseNamespace = "http://hoteamsoft.org/T ",
Use = SoapBindingUse.Literal)]
[WebMethod]
//上面的引用必须加----end
public string HelloWorld1(string obj)

return "Hello World =" + obj + " = " + DateTime.Now.ToString();




java调用的方法如下:

public String GetMaterial(String xmlString)

try
// 指出service所在URL
String endpoint = "http://localhost:81/webService/Service.asmx";
// 创建一个服务(service)调用(call)
Service service = new Service();
Call call = (Call)service.createCall();// 通过service创建call对象
// 设置service所在URL
call.setTargetEndpointAddress(new java.net.URL(endpoint));
//设置要调用的方法
call.setOperationName( new QName( "http://hoteamsoft.org/T","HelloWorld1" ));
//设置该方法需要的参数
call.addParameter("obj" , 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://hoteamsoft.org/HelloWorld1");
String backStr = (String) call.invoke(new Object[] xmlString );
System.out.println("sss"+backStr);

catch (Exception e)
e.printStackTrace();
return e.toString();

参考技术B 好像是不行的。就像你在c#中调用函数一样,少了一个参数编译就不可能通过,更谈不上运行了。
只能在webconfig中customErrors节定义错误,然后再error.aspx?errpath=webservice1.asmx....看看能不能取得后面的参数来判断出少了param3的参数。
参考技术C 在后台获取你传递的3个参数,进行if判断,即获取得到的3个参数进行“与”判断,
若为假,报错
为真,则执行函数Test()。
参考技术D 在后台获取你传递的3个参数,进行if判断,即获取得到的3个参数进行“与”判断,
若为假,报错

为真,则执行函数Test()可以的用地啊!

以上是关于.net webservice方法参数问题的主要内容,如果未能解决你的问题,请参考以下文章

怎么测试webservice传参数

webservice怎样接收json类型的参数

如何解决WebService参数传递中文的问题

java axis调用webservice,接口方法中的数组型参数应该怎么传参

webservice - 未知的Web方法参数名称methodname

vb.net如何动态调用WebService接口啊