WebService传多个参数和返回多个参数的方法
WebService方面:
在webService项目中新建两个类访问性都为public,
request:用来存放请求参数;
public class request
{
public request()
{
}
public string name;
public string sex;
public int age;
public string enable;
}
{
public request()
{
}
public string name;
public string sex;
public int age;
public string enable;
}
response:用来存放应答参数;
public class response
{
public response()
{
}
public string name2;
public string sex2;
public int age2;
public bool marry;
}
{
public response()
{
}
public string name2;
public string sex2;
public int age2;
public bool marry;
}
在webService中使用方法:
[WebMethod(Description = "real test")]
public response change(request re)
{
response resp = new response();
resp.name2 = re.name + "2";
resp.sex2 = re.sex + "2";
resp.age2 = re.age + 2;
resp.marry = false;
return resp;
}
public response change(request re)
{
response resp = new response();
resp.name2 = re.name + "2";
resp.sex2 = re.sex + "2";
resp.age2 = re.age + 2;
resp.marry = false;
return resp;
}
这个方法测试接收多个参数,并改变其值后返回多个参数
在Web项目中调用WebService:
添加webService的引用,假设引用名为localhost,使用该webService的方法如下:
客户端不用再另加request和response两个类。
localhost.request re = new localhost.request();
re.name = "aa";
re.sex = "man";
re.age = 12;
re.enable = "true";
localhost.Service ser = new localhost.Service();
localhost.response res = ser.change(re);
Response.Write(res.name2+"<br>"+res.sex2+"<br>"+res.age2.ToString()+"<br>"+res.marry.ToString());
re.name = "aa";
re.sex = "man";
re.age = 12;
re.enable = "true";
localhost.Service ser = new localhost.Service();
localhost.response res = ser.change(re);
Response.Write(res.name2+"<br>"+res.sex2+"<br>"+res.age2.ToString()+"<br>"+res.marry.ToString());
关于调用webservice时出现401错误:Access Denied
解决办法:将webservice的访问权限加上“允许匿名访问”,在webService属性的“目录安全性”选项卡中。