Silverlight GET ,POST

1.Get请求

cs:

 private void Download()
        {
            string url = "http://localhost:13936/GetData.aspx?id=getwangk";
            WebClient client = new WebClient();
            client.DownloadStringCompleted += new DownloadStringCompletedEventHandler(DownloadBind);
            client.DownloadStringAsync(new Uri(url));
        }

        private void DownloadBind(object sender, DownloadStringCompletedEventArgs e)
        {
            if (e.Error == null)
            {
                tb.Text = e.Result;
            }
            else
            {
                tb.Text = "错误信息" + e.Error.Message;
            }
        }

2.Post 请求

xaml:

 

<UserControl x:Class="RhythmkSilverlightApp.Primary.SimplifyPost"
    xmlns
="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x
="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d
="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc
="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable
="d"
    d:DesignHeight
="300" d:DesignWidth="400">

    <StackPanel x:Name="LayoutRoot" Background="White">
        <Button x:Name="btn" Width="100" Height="30" Margin="50" Content="Post" Click="btn_Click"></Button>
    </StackPanel>
</UserControl>

cs:

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Threading;
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;
//  by  rhythmk
namespace RhythmkSilverlightApp.Primary
{
    public partial class SimplifyPost : UserControl
    {
        private SynchronizationContext currentContext;

        public SimplifyPost()
        {
            InitializeComponent();
            this.currentContext = SynchronizationContext.Current;
        }

        private void btn_Click(object sender, RoutedEventArgs e)
        {
            HttpClientPost post = new HttpClientPost();
            post.PostData("http://localhost:13936/GetData.aspx""id=cnblogs.com/rhythmk", currentContext, new SendOrPostCallback(ShowResult));
        }

        public void ShowResult(object obj)
        {
            MessageBox.Show("回传数据:" + obj.ToString());
        }
    }

    public class HttpClientPost
    {
        private string postData;
        SynchronizationContext currentContext;

        SendOrPostCallback sopc;

        public void PostData(string url, string data, SynchronizationContext _currentContext, SendOrPostCallback _sopc)
        {
            currentContext = _currentContext;
            Uri endpoint = new Uri(url);
            sopc = _sopc;
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(endpoint);
            request.Method = "POST";
            request.ContentType = "application/x-www-form-urlencoded";
            postData = data;
            request.BeginGetRequestStream(new AsyncCallback(RequestReadySocket), request);
        }

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

            using (StreamWriter writer = new StreamWriter(requestStream))
            {
                writer.Write(postData);
                writer.Flush();
            }

            request.BeginGetResponse(new AsyncCallback(ResponseReadySocket), request);
        }

        private void ResponseReadySocket(IAsyncResult asyncResult)
        {
            WebRequest request = asyncResult.AsyncState as WebRequest;
            WebResponse response = request.EndGetResponse(asyncResult);
            using (Stream responseStream = response.GetResponseStream())
            {
                StreamReader reader = new StreamReader(responseStream);
                string paramStr = reader.ReadToEnd();
                currentContext.Post(sopc, paramStr);
            }
        }
    }
}

 

 

posted @ 2012-05-31 11:17  Rhythmk  阅读(2091)  评论(2编辑  收藏  举报
Rhythmk 个人笔记