WebService简单示例

建一个webService:

 

代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Web.Services.Protocols;

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]
    public class WebService1 : System.Web.Services.WebService
    {
        
public Createntials toKen;

        [WebMethod(Description 
= "获取全部公告")]
        [SoapHeader(
"toKen", Direction = SoapHeaderDirection.In)] 
        
public List<Person> GetPersonList()
        {
            
if (toKen.AccountID != "admin" || toKen.PIN != "admin")
            {
                
return null;
            }

            
return new PersonData().GetDataSource();
        }
    }

    
/// <summary>
    
/// 用于WebService的安全性
    
/// </summary>
    public class Createntials : System.Web.Services.Protocols.SoapHeader
    {
        
public string AccountID;
        
public string PIN;
    }

    
public class PersonData
    {
      
public List<Person> personList = new List<Person>() 
        { 
             
new Person(){Id=1,Name="张三",Address="浙江金华"},
             
new Person(){Id=2,Name="李四",Address="浙江杭州"},
             
new Person(){Id=3,Name="赵六",Address="浙江温州"}
        };

        
public List<Person> GetDataSource()
        {
            
return personList;
        }

        
public Person GetPersonByID(int id)
        {
            
return personList.FirstOrDefault(p => p.Id == id);
        }

        
public List<Person> GetPersonByName(string name)
        {
            
return personList.Where(p => p.Name == name).ToList();
        }
    }


    
public class Person
    {
        
private int id;

        
public int Id
        {
            
get { return id; }
            
set { id = value; }
        }

        
private string name;

        
public string Name
        {
            
get { return name; }
            
set { name = value; }
        }

        
private string address;

        
public string Address
        {
            
get { return address; }
            
set { address = value; }
        }
    }
}

 

 

调用WebService代码:

 

代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace WebApplication2
{
    
public partial class _Default : System.Web.UI.Page
    {
        
        
protected void Page_Load(object sender, EventArgs e)
        {
            
if (!IsPostBack)
            {
                
//创建调用的WebService类的对象
                localhost.WebService1 ws = new localhost.WebService1();

                
//验证
                ws.CreatentialsValue = new localhost.Createntials() { AccountID = "admin", PIN = "admin" };

                
//返回的集合会转成数组
                localhost.Person[] pArray = ws.GetPersonList();

                GridView1.DataSource 
= pArray;
                GridView1.DataBind();

                
//也可以将数组转成集合
                
//List<localhost.Person> list = new List<localhost.Person>();

                
//foreach (localhost.Person item in pArray)
                
//{
                
//    list.Add(item);
                
//}

                
//GridView1.DataSource = list;
                
//GridView1.DataBind();
            }
        }
    }
}

 

 

posted @ 2010-09-20 09:31  唔愛吃蘋果  阅读(252)  评论(0编辑  收藏  举报