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

  • 相关阅读:
    bash中执行SQL语句返回一个值
    对机器特定端口增加网络延迟
    修改jmeter的界面文字大小和语言
    pip3 安装一直报错ssl问题,重装python3
    robotframework学习
    jmeter
    python3向oracle插入数据
    oracle使用时注意
    人心惟危,道心惟微,惟精惟一,允执厥中。
    vim 离线安装 .tar.gz 源码程序
  • 原文地址:https://www.cnblogs.com/zhuawang/p/2311445.html
Copyright © 2011-2022 走看看