WEB开发网
开发学院图形图像Flash Silverlight 2.0 正式版跨域提交数据全攻略 阅读

Silverlight 2.0 正式版跨域提交数据全攻略

 2008-10-27 11:49:10 来源:WEB开发网   
核心提示: 本例子中使用的 Page.xaml 如下:查看复制到剪切板打印<UserControl xmlns:basics="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls"x:C

本例子中使用的 Page.xaml 如下:

查看复制到剪切板打印

<UserControl xmlns:basics="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls"
  x:Class="SilverlightApplication3.Page"
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  Width="400" Height="300">
 <Grid x:Name="LayoutRoot" Background="White">
  <Canvas Width="400" Height="300" Canvas.Left="2">
   <TextBox x:Name="data" Canvas.Left="2" Canvas.Top="2" Height="30" Width="180" Text="【孟子E章】测试数据" F></TextBox>
   <Button C Width="100" Height="30" Click="PostData" Canvas.Top="2" Canvas.Left="200"></Button>
   <TextBlock x:Name="result" Canvas.Top="40" F></TextBlock>
  </Canvas>
 </Grid>
</UserControl>

Page.xaml.cs 完整内容

查看复制到剪切板打印

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.Text;
using System.IO;
using System.Diagnostics;
using System.Threading;
namespace SilverlightApplication3
{
  public partial class Page : UserControl
  { 
    public Page()
    {
      InitializeComponent();
     
      //设置当前界面线程
      syncContext = SynchronizationContext.Current;
    }
    private SynchronizationContext syncContext;
    private String postData = String.Empty;
    private void PostData(object sender, RoutedEventArgs e)
    {
     //请注意:Silverlight 里的 HttpWebRequest, Encoding 类的属性和方法与常规的 .NET Framework 里面还是不同的。
     result.Text = "开始提交数据……";
     postData = "data=" + data.Text; //要提交的内容
     Uri url = new Uri("http://www.mengxianhui.com/admin?tmp=" + DateTime.Now.Ticks.ToString(), UriKind.Absolute);
     System.Net.HttpWebRequest request = (System.Net.HttpWebRequest)WebRequest.Create(url);
     request.Method = "POST"; // Silverlight 只支持 GET 和 POST 方法
     request.AllowReadStreamBuffering = true;
     request.C;
     request.BeginGetRequestStream(new AsyncCallback(RequestReady), request);
    }
    private void RequestReady(IAsyncResult asyncResult)
    {
     HttpWebRequest request = asyncResult.AsyncState as HttpWebRequest;
     StreamWriter postStream = new StreamWriter(request.EndGetRequestStream(asyncResult));
     //byte[] byteArray = Encoding.UTF8.GetBytes(postData);
     //Encoding.GetEncoding()方法只支持四种编码。utf-8 , utf-16 , utf-16BE , utf-16LE
     postStream.Write(postData); //处理中文的问题。不要使用 Stream 的 Write 方法。
     postStream.Close();
     postStream.Dispose();
     request.BeginGetResponse(new AsyncCallback(ResponseReady), request);
    }
    void ResponseReady(IAsyncResult asyncResult)
    {
     HttpWebRequest request = asyncResult.AsyncState as HttpWebRequest;
     WebResponse response = request.EndGetResponse(asyncResult) as WebResponse;
    
     //同步线程上下文
     if (syncContext == null) syncContext = new SynchronizationContext();
     syncContext.Post(ExtractResponse, response);
    }
    private void ExtractResponse(object state)
    {
     WebResponse response = state as WebResponse;
     Stream responseStream = response.GetResponseStream();
     StreamReader streamRead = new StreamReader(responseStream);
     //显示返回的数据
     result.Text = new StreamReader(responseStream).ReadToEnd();
     response.Close();
     responseStream.Close();
     responseStream.Dispose();
     streamRead.Close();
     streamRead.Dispose();
    }
  }
}

上一页  1 2 3 

Tags:Silverlight 提交

编辑录入:爽爽 [复制链接] [打 印]
赞助商链接