zoukankan      html  css  js  c++  java
  • silverlight向服务器post数据类

    using System;
    using System.Net;
    using System.Windows;
    using System.Windows.Controls;
    using System.Windows.Documents;
    using System.Windows.Ink;
    using System.Windows.Input;
    using System.Windows.Media;
    using System.Windows.Media.Animation;
    using System.Windows.Shapes;
    using System.IO;


    public class HttpClient
    {
        
    private WebRequest webRequest = null;
        
    public delegate void ResponseEventHandler(String responseResult);
        
    public event ResponseEventHandler onResponseEventHandler;
        
    public delegate void RequestEventHandler();
        
    public event RequestEventHandler onRequestEventHandler;
        
    public string PostData { getset; }

        
    public HttpClient(Uri clientUri)
        {
            Uri endpoint 
    = clientUri;
            webRequest 
    = WebRequest.Create(endpoint);
            webRequest.ContentType 
    = "application/x-www-form-urlencoded";
            webRequest.Method 
    = "POST";
        }

        
    public void Post()
        {
            webRequest.BeginGetRequestStream(
    new AsyncCallback(RequestReady), webRequest);
        }
        
    private void RequestReady(IAsyncResult asyncResult)
        {
            
    if (this.onRequestEventHandler != null)
            {
                
    this.onRequestEventHandler();
            }
            WebRequest request 
    = asyncResult.AsyncState as WebRequest;
            Stream requestStream 
    = request.EndGetRequestStream(asyncResult);

            
    using (StreamWriter writer = new StreamWriter(requestStream))
            {
                writer.Write(
    this.PostData);
                writer.Flush();
                writer.Close();
            }
            webRequest.BeginGetResponse(
    new AsyncCallback(ResponseReady), webRequest);
        }
        
    private void ResponseReady(IAsyncResult asyncResult)
        {
            
    if (this.onResponseEventHandler != null)
            {
                WebRequest request 
    = asyncResult.AsyncState as WebRequest;
                WebResponse response 
    = request.EndGetResponse(asyncResult);

                
    using (Stream responseStream = response.GetResponseStream())
                {
                    String responseResult 
    = String.Empty;
                    
    try
                    {
                        StreamReader reader 
    = new StreamReader(responseStream);
                        responseResult 
    = reader.ReadToEnd();
                    }
                    
    catch (WebException er)
                    {
                        responseResult 
    = er.Message;
                    }
                    
    catch (Exception er)
                    {
                        responseResult 
    = er.Message;
                    }
                    
    this.onResponseEventHandler(responseResult);
                }
            }
        }
    }

    作者:nasa
    出处:nasa.cnblogs.com
    联系:nasa_wz@hotmail.com
    QQ:12446006
  • 相关阅读:
    LeetCode OJ:Merge Two Sorted Lists(合并两个链表)
    LeetCode OJ:Remove Nth Node From End of List(倒序移除List中的元素)
    LeetCode OJ:Find Peak Element(寻找峰值元素)
    LeetCode OJ:Spiral MatrixII(螺旋矩阵II)
    LeetCode OJ:Longest Palindromic Substring(最长的回文字串)
    利用生产者消费者模型实现大文件的拷贝
    Linux下用c语言实现whereis.
    Huffman编码实现文件的压缩与解压缩。
    修改MySQL数据库存储位置datadir
    python中pickle简介
  • 原文地址:https://www.cnblogs.com/nasa/p/1264757.html
Copyright © 2011-2022 走看看