Web Service 简单实现的例子
服务端:要发布到IIS上
public class User : SoapHeader
{
private string username;
private string password;
public string UserName
{
get{return username;}
set{username = value;}
}
public string PassWord
{
get{return password;}
set{password= value;}
}
}
public class Service : WebService
{
User u =null;
public Service()
{}
[WebMethod]
public string HelloWorld()
{
return "Hello World";
}
[SoapHeader]
[WebMethod]
public string GetUser()
{
if(u.UserName == "aa" && u.PassWord == "123")
{
return "Good Morning!";
}
else
{
return "";
}
}
}
客户端:用Web页面调用
1.先添加Web引用
2.在页面放3个TextBox 用来输入用户名、密码和显示调用的结果,再放一个Button,当点击Button时调用
3.后台主要代码:
localhost.Server server = new localhost.Server();
localhost.User u = new localhost.User();
protected void Button1_Click(object sender, EventArgs e)
{
u.UserName ="aa";
u.PassWord = "123";
server.UserValue = u;
this.textbox3.text = server.GetUser();
}