WebService简单示例

记录一下,以备后用:

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


namespace Niunan.ZZ.JKDA.Web.webservice
{
    
/// <summary>
    
/// GGService 的摘要说明
    
/// </summary>
    [WebService(Namespace = "http://gxbest.cn/")]
    [WebServiceBinding(ConformsTo 
= WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(
false)]
    
// 若要允许使用 ASP.NET AJAX 从脚本中调用此 Web 服务,请取消对下行的注释。
    
// [System.Web.Script.Services.ScriptService]
    public class GGService : System.Web.Services.WebService
    {
        
public Credentials token; 


        
/// <summary>
        
/// 获取全部公告
        
/// </summary>
        
/// <returns></returns>
        [WebMethod(Description = "获取全部公告")]
        [SoapHeader(
"token", Direction = SoapHeaderDirection.In)] 
        
public List<Niunan.ZZ.JKDA.Model.S_GongGao> GetGongGao() {
            
if (token.AccountID != "admin" || token.PIN != "admin")
            {
                
return null;
            } 
            
return new Niunan.ZZ.JKDA.DAL.S_GongGaoDAO().GetListArray("");
        }


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


    }
}

添加了web引用后,下面是提取示例:

代码
            cn.gxbest.GGService ws = new Niunan.ZZ.JKDA.Web.cn.gxbest.GGService();
            ws.CredentialsValue 
= new Niunan.ZZ.JKDA.Web.cn.gxbest.Credentials() { AccountID = "admin", PIN = "admin" };
            cn.gxbest.S_GongGao[] gg 
= ws.GetGongGao();
            List
<cn.gxbest.S_GongGao> list = new List<cn.gxbest.S_GongGao>();


            
foreach (cn.gxbest.S_GongGao item in gg)
            {
                list.Add(item);
            }
            GridView1.DataSource 
= list;
            GridView1.DataBind();
撸码:复制、粘贴,拿起键盘就是“干”!!!
原文地址:https://www.cnblogs.com/niunan/p/1791525.html