本文我们学习如何使用WebRequest类实现客户端和服务器端的通讯。
     本例处理过程:在客户端,我们在文本框中输入任意文本,然后用POST方法向服务器端传递信息,服务器端收到从客户端传来的信息后做相应处理,再向客户端返回服务器端的信息,客户端收到服务器端信息后也做相应处理然后在客户端界面上显示处理结果。效果如下:

                        SilverLight学习笔记--Silverlight中WebRequest通讯
新建 Silverlight应用程序,命名为:SLWebRequestTest,在SLWebRequestTest.Web项目下,建立一个Handler,命名为:WebRequestHandler.ashx,它负责处理响应客户端请求并返回处理结果。
如下图:


                        SilverLight学习笔记--Silverlight中WebRequest通讯

1、建立程序界面
Page.xaml代码如下:

<UserControl x:Class="SLWebRequestTest.Page"
    xmlns
="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x
="http://schemas.microsoft.com/winfx/2006/xaml"
 
    Width
="400" Height="300">

    
<StackPanel Width="400" Height="250" Background="Wheat" >
        
<TextBlock Text="请输入传递到服务器的数据" TextAlignment="Center" Foreground="Red" Margin="2" FontSize="16"></TextBlock>
        
<TextBox  x:Name="tbClientContent" Width="250"></TextBox>
        
<Button  x:Name="btnGetData" Width ="200" Height="25" Content="获取数据"  Click="btnGetData_Click" Margin="20"></Button>
        
<TextBlock Text="通过WebRequest从服务器取得的数据如下" TextAlignment="Center" Foreground="Red" Margin="2" FontSize="16"></TextBlock>
        
<TextBlock x:Name="tbRetText" Text="当前内容为空" TextAlignment="Center" Foreground="Green" FontSize="16"></TextBlock>

    
</StackPanel>
</UserControl>

2、客户端完成的工作
i、向服务器端POST客户端信息
ii、向服务器端发起异步请求
iii、接收服务器端返回的数据,做相应处理后显示在界面上。

Page.xaml.cs全部代码如下:


SilverLight学习笔记--Silverlight中WebRequest通讯using System;
SilverLight学习笔记--Silverlight中WebRequest通讯
using
 System.Collections.Generic;
SilverLight学习笔记--Silverlight中WebRequest通讯
using
 System.Linq;
SilverLight学习笔记--Silverlight中WebRequest通讯
using
 System.Net;
SilverLight学习笔记--Silverlight中WebRequest通讯
using
 System.Windows;
SilverLight学习笔记--Silverlight中WebRequest通讯
using
 System.Windows.Controls;
SilverLight学习笔记--Silverlight中WebRequest通讯
using
 System.Windows.Documents;
SilverLight学习笔记--Silverlight中WebRequest通讯
using
 System.Windows.Input;
SilverLight学习笔记--Silverlight中WebRequest通讯
using
 System.Windows.Media;
SilverLight学习笔记--Silverlight中WebRequest通讯
using
 System.Windows.Media.Animation;
SilverLight学习笔记--Silverlight中WebRequest通讯
using
 System.Windows.Shapes;
SilverLight学习笔记--Silverlight中WebRequest通讯
SilverLight学习笔记--Silverlight中WebRequest通讯
using
 System.IO;
SilverLight学习笔记--Silverlight中WebRequest通讯
using
 System.Threading;
SilverLight学习笔记--Silverlight中WebRequest通讯
SilverLight学习笔记--Silverlight中WebRequest通讯
namespace
 SLWebRequestTest

3、服务器端完成的工作
在建立的Handler中接收客户端的信息,做相应处理后返回给客户端。
WebRequestHandler.ashx.cs全部代码如下:

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace SLWebRequestTest.Web
{
    
public class WebRequestHandler : IHttpHandler
    {

        
public void ProcessRequest(HttpContext context)
        {
            
//读取从客户端传来的参数
            
// string clientInfoStr = context.Request.Params.Get("ClientInfo");  
            
//string clientInfoStr = context.Request.Form["ClientInfo"]; 
            string clientInfoStr = context.Request.Form[0];

            
//返回信息
            context.Response.ContentType = "text/plain";
            context.Response.Write(
"Server Received Client Info:" + clientInfoStr + "\n Server Return To Client: Welcome To Server");
        }

        
public bool IsReusable
        {
            
get
            {
                
return false;
            }
        }
    }
}



 


前往:Silverlight学习笔记清单
 

本文程序在Silverlight2.0和VS2008环境中调试通过。本文参照了部分网络资料,希望能够抛砖引玉,大家共同学习。
(转载本文请注明出处)

相关文章: