• 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);
                }
            }
        }
    }
  • 相关阅读:
    OGG-01168
    浅谈webuploader上传文件
    [ IE浏览器兼容问题 ] Web Uploader 在IE、FireFox下点击上传没反应
    webuploader在ie7下的flash模式的使用
    记一次项目使用webuploader爬坑之旅
    解决Web Uploader上传文件和图片 延迟和not defined
    SpringMVC上传图片总结(2)--- 使用百度webuploader上传组件进行上传图片
    百度上传插件(webupload)单文件(单图片)上传设置
    webservice大文件怎么传输
    java使用WebUploader做大文件的分块和断点续传
  • 原文地址:https://www.cnblogs.com/rhythmK/p/2528120.html
Copyright © 2020-2023  润新知