zoukankan      html  css  js  c++  java
  • 数据与通信之WebRequest.Web

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Net;
    using System.Windows;
    using System.Windows.Controls;
    using System.Windows.Documents;
    using System.Windows.Input;
    using System.Windows.Media;
    using System.Windows.Media.Animation;
    using System.Windows.Shapes;
    using System.IO;

    namespace 数据与通信之WebRequest
    {
        public partial class MainPage : UserControl
        {
            public MainPage()
            {
                InitializeComponent();
            }

            private void UserControl_Loaded(object sender, RoutedEventArgs e)
            {
                List<Book> books = new List<Book>() {
            new Book("Professional ASP.NET 3.5"),
            new Book("ASP.NET AJAX In Action"),
            new Book("Silverlight In Action"),
            new Book("ASP.NET 3.5 Unleashed"),
            new Book("Introducing Microsoft ASP.NET AJAX")
        };

                Books.ItemsSource = books;

            }

            private string bookNo;

            void Books_SelectionChanged(object sender, SelectionChangedEventArgs e)
            {
                bookNo = Books.SelectedIndex.ToString();

                Uri endpoint = new Uri("http://localhost:8675/BookHandler.ashx");

                WebRequest request = WebRequest.Create(endpoint);
                request.Method = "POST";
                request.ContentType = "application/x-www-form-urlencoded";
                request.BeginGetRequestStream(new AsyncCallback(RequestReady), request);
            }

            void RequestReady(IAsyncResult asyncResult)
            {
                WebRequest request = asyncResult.AsyncState as WebRequest;
                Stream requestStream = request.EndGetRequestStream(asyncResult);

                using (StreamWriter writer = new StreamWriter(requestStream))
                {
                    writer.Write(String.Format("No={0}", bookNo));
                    writer.Flush();
                }
                request.BeginGetResponse(new AsyncCallback(ResponseReady), request);
            }

            void ResponseReady(IAsyncResult asyncResult)
            {
                WebRequest request = asyncResult.AsyncState as WebRequest;
                WebResponse response = request.EndGetResponse(asyncResult);

                using (Stream responseStream = response.GetResponseStream())
                {
                    StreamReader reader = new StreamReader(responseStream);
                    string price = reader.ReadToEnd();
                    if (this.lblPrice.CheckAccess())
                    {
                        this.lblPrice.Text = "价格:" + price;
                    }
                    else
                    {
                        this.Dispatcher.BeginInvoke(new UIContextChangedEventHandler(UIContextChanged), price);
                    }
                }
            }
            public delegate void UIContextChangedEventHandler(object state);
            private void UIContextChanged(object state)
            {
                this.lblPrice.Text = "价格:" + (String)state;
            }
        }
    }
       

  • 相关阅读:
    课程5:Spring框架2016版视频--视频列表目录
    TAQSkinScrollBar 类美化滚动条再讨论
    C#区分多态和重载-delphi也类似
    Delphi之静态方法,虚方法virtual,动态dynamic,抽象abstract,消息
    课程4:黑马程序员_spring2.5视频教程--视频列表
    在 Delphi 中判断一个字符是中文的方法
    salesforce 零基础学习(四十八)自定义列表分页之Pagination基类封装 ※※※
    salesforce 零基础学习(四十七) 数据加密简单介绍
    salesforce 零基础学习(四十六)动态美观显示列表中记录的审批状态
    salesforce 零基础学习(四十五)Approval Lock & UnLock相关注意事项
  • 原文地址:https://www.cnblogs.com/zhuawang/p/2311445.html
Copyright © 2011-2022 走看看