学习SOAP一个不错的简单例子(一)

     本文例子修改自《Fundamentals of Web Applications Using .NET and XML》。
     
      今天学习SOAP时看到一个挺不错,比较实用的例子,和大家分享一下。用C#实现服务器端的We Service,用VB.Net写的客户端程序以SOAP访问Web Service,其中的编码对于向我这样初学者很有帮助。
      例子简述:服务器端提供旅游信息的查询和上传的Web服务,客户端用SOAP来获取信息,例子主要演示了SOAP的组装,发送,接收,处理等过程。例子中能显示发送和接收到的SOAP。

服务器端:

      服务器程序的命名空间TAUWebService。
       一条旅游信息用一个 TravlPacket 来表示:
using System;

namespace TAUWebService
{
    
/// <summary>
    
/// TravelPacket 的摘要说明。
    
/// </summary>

    public class TravelPacket
    
{
        
// members
        public string id;
        
public double startingPrice;
        
public int numOfDays;
        
public DateTime offerStarts;
        
public DateTime offerEnds;
        
public string airline;
        
public string destination;
        
public string originCity;
        
public string agent;

        
public TravelPacket()
        
{
            
//
            
// TODO: 在此处添加构造函数逻辑
            
//
        }

    }

}


          Web Service的代码:
           用一个Hashtable 来存储各条 TravelPacket。提供以下Service
          GetPacketCount        返回旅游信息数。
          GetPacketDetail        返回一条旅游信息的详细情况。
          GetNewPackets         返回在指定日期以后的旅游信息。
          UploadPacket            上传一条旅游信息。


           

using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Web;
using System.Web.Services;

namespace TAUWebService
{
    
/// <summary>
    
/// Service1 的摘要说明。
    
/// </summary>

    public class Service1 : System.Web.Services.WebService
    
{
        
/// <summary>
        
/// Keep all the travel packets ;
        
/// </summary>

        private static Hashtable packets = null ;
        
public Service1()
        
{
            
//CODEGEN: 该调用是 ASP.NET Web 服务设计器所必需的
            InitializeComponent();
            InitPackets();
        }


        
组件设计器生成的代码

        [WebMethod]
        
public int GetPacketCount()
        
{
            
return packets.Count;
        }


        
        [WebMethod]
        
public TravelPacket GetPacketDetail(string packetId)
        
{
            TravelPacket p 
= (TravelPacket)packets[packetId];
            
return p;
        }

        [WebMethod]
        
public TravelPacket[] GetNewPackets( DateTime day )
        
{
            IDictionaryEnumerator elements 
= packets.GetEnumerator();
            Queue theQ 
= new Queue();
            elements.Reset();
            
while ( elements.MoveNext() )
            
{
                TravelPacket p 
= (TravelPacket)elements.Value;
                
if ( day.CompareTo(p.offerStarts) < 0 )
                
{
                    theQ.Enqueue(p);
                }

            }


            
int count = theQ.Count ;
            TravelPacket[] newPackets 
= new TravelPacket[count];
            
for ( int i = 0 ; i < count ; i++ )
            
{
                newPackets[i] 
= (TravelPacket)theQ.Dequeue();
            }

            
return newPackets;
        }


        public void InitPackets()
        
{
            
if ( packets == null )
            
{
                packets 
= new Hashtable();
                
                
//pregenerate a sample packet
                TravelPacket p = new TravelPacket();
                p.id 
= "001";
                p.agent 
= "China Travel";
                p.airline 
= "East Airline";
                p.destination 
= "Kunming";
                p.originCity 
= "Shanghai";
                p.offerStarts 
= new DateTime(2005,10,1);
                p.offerEnds 
= new DateTime(2005,10,5);
                p.numOfDays 
= 5 ;
                p.startingPrice 
= 1200;
                
                packets.Add(p.id,p);
            }

        }


        [WebMethod]
        
public String UploadPacket(String id,
            String agent, 
            String destination,
            String airline,
            
int numOfDays,
            DateTime offerStarts,
            DateTime offerEnds,
            String originCity,
            
double startingPrice)
        
        
{
            
try
            
{

                
//Check if the id has been used
                if (packets.ContainsKey(id))
                
{
                    
return "The ID has been used.";
                }

            
                TravelPacket p
=new TravelPacket();
                p.id 
= id;
                p.airline
=airline;
                p.destination
=destination;
                p.numOfDays
=numOfDays;
                p.offerStarts
=offerStarts;
                p.offerEnds
=offerEnds;
                p.originCity
=originCity;
                p.startingPrice
=startingPrice;
                p.agent
=agent;
                packets.Add(p.id, p);
            
            }

            
catch (Exception ex)
            
{
                
return ex.Message;
            }

            
//put the new package into packages.
            return "Packet "+id+" has been sucessfully loaded";
        }

    }

}


客户端程序请参看学习SOAP一个不错的简单例子(二)
posted on 2005-04-03 14:46  我在等待  阅读(528)  评论(0)    收藏  举报