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;
            }
        }
    }
       

  • 相关阅读:
    利用DTrace实时检测MySQl
    改进MySQL Order By Rand()的低效率
    RDS for MySQL查询缓存 (Query Cache) 的设置和使用
    RDS For MySQL 字符集相关说明
    RDS for MySQL 通过 mysqlbinlog 查看 binlog 乱码
    RDS for MySQL Mysqldump 常见问题和处理
    RDS for MySQL Online DDL 使用
    RDS MySQL 表上 Metadata lock 的产生和处理
    RDS for MySQL 如何使用 Percona Toolkit
    北京已成为投融资诈骗重灾区:存好骗子公司黑名单,谨防上当!
  • 原文地址:https://www.cnblogs.com/zhuawang/p/2311445.html
Copyright © 2011-2022 走看看