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

  • 相关阅读:
    【Linux学习三】Linux系统目录架构
    【Linux学习二】Linux文件系统
    【Python网络爬虫四】通过关键字爬取多张百度图片的图片
    【GitHub】命令行操作
    【Linux学习一】命令行CLI、BASH的基本操作
    从CK+库提取标记点信息
    【Python网络爬虫三】 爬取网页新闻
    Windows命令行下pip安装python whl包
    【GitHub Desktop】MacOS和Win下配置及简单的使用
    【Python文件处理】递归批处理文件夹子目录内所有txt数据
  • 原文地址:https://www.cnblogs.com/zhuawang/p/2311445.html
Copyright © 2011-2022 走看看