獲得網頁的輸出值

 

爲了模擬查詢價格,我們編寫一個HttpHandler,接收書籍的No,並返回價格:

public class BookHandler : IHttpHandler
{
    public static readonly string[] PriceList = new string[] { 
        "66.00",
        "78.30",
        "56.50",
        "28.80",
        "77.00"
    };
    public void ProcessRequest(HttpContext context)
    {
        context.Response.ContentType = "text/plain";
        context.Response.Write(PriceList[Int32.Parse(context.Request.QueryString["No"])]);
    }

    public bool IsReusable
    {
        get
        {
            return false;
        }
    }
}
客戶端調用
void Books_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
    Uri endpoint = new Uri(String.Format("http://localhost:49955/BookHandler.ashx?No={0}",Books.SelectedIndex));

    WebClient client = new WebClient();
    client.DownloadStringCompleted += new DownloadStringCompletedEventHandler(client_DownloadStringCompleted);
    
    client.DownloadStringAsync(endpoint);
}

void client_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
{
    if (e.Error == null)
    {
        lblPrice.Text = "價格:" + e.Result;
    }
    else
    {
        lblPrice.Text = e.Error.Message;
    }
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章