HttpWebRequest 是 .net 基类库中的一个类,在命名空间 System.Net 下面,用来使用户通过 HTTP 协议和服务器交互。 HttpWebRequest 对 HTTP 协议进行了完整的封装,程序使用 HTTP 协议和服务器交互主要是进行数据的提交,通常数据的提交是通过 GET 和 POST 两种方式来完成。
   HttpWebRequest常用命令如下:
  HttpWebRequest - 对指定的 URI 发出请求
  Create() 
- 初始化一个 WebRequest
  BeginGetResponse() 
- 开始对指定 URI 资源做异步请求
  EndGetResponse() 
- 结束对指定 URI 资源做异步请求
  HttpWebResponse 
- 对指定的 URI 做出响应
  GetResponseStream() 
- 获取响应的数据流
   需要注意的是: HttpWebRequest使用基于代理的异步编程模型,在HTTP响应返回时引发的HttpWebRequest回调不是在UI线程上返回的,因此在该回调中需要额外代码处理UI,否则就会产生"跨线程访问无效"错误。
   在本例中我们将通过两种方式来编写处理UI的额外代码,即解决"跨线程访问无效"的问题(当然是在Client成功向Server端发送信息,以及Server端成功向Client端传回数据的前提下)。
   注:在实际应用时,如果我们要更新UI,则我们常使用silverlight支持的另一种发送HTTP请示的方式即WebClient方式,此方式与HttpWebRequest不同处在于,WebClient方式使用基于事件的异步编程模型,在HTTP响应返回时引发的WebClient回调是在UI线程中调用的,因此可用于更新UI元素的属性,例如把HTTP响应中的数据绑定到UI的指定控件上进行显示。
   另注:在Form元素的语法中,EncType表明提交数据的格式 用 Enctype 属性指定将数据回发到服务器时浏览器使用的编码类型,有以下三种类型:
      application/x-www-form-urlencoded: 窗体数据被编码为名称/值对。这是标准的编码格式。   
      multipart/form-data: 窗体数据被编码为一条消息,页上的每个控件对应消息中的一个部分。
      text/plain: 窗体数据以纯文本形式进行编码,其中不含任何控件或格式字符。
   本例,我们将在两个按钮中分别采用"application/x-www-form-urlencoded"和"text/plain"方式来编码提交数据到服务器端
新建Silverlight应用程序

SilverLight学习笔记--Silverlight中HttpWebRequest通讯
程序用户界面如下:


SilverLight学习笔记--Silverlight中HttpWebRequest通讯
如图所示,界面上有两个按钮,两个按钮的后台操作分别为:
  按钮"用Thread配合AsynchronazationContext解决跨线程访问无效问题"--当点击后
  1、将把字符串"This Msg Come From Client"传递到服务器端
  2、uri地址是指定的asp页面(此处是Default.aspx页面)
  3、编码方式采用text/plain
  4、服务器端接收到客户端传来的数据后,用StreamReader把传参接收下来,并在此基础上加入服务器端数据并返回
  5、客户端采用"Thread配合AsynchronazationContext方式"把服务器端返回的数据显示在UI控件上
   按钮"用Socket配合AsynchronazationContext解决跨线程访问无效问题"--当点击后
  1、将把字符串"This Msg Come From Client"传递到服务器端
  2、uri地址是指定的Handler(此处是ServerHandler.ashx)
  3、编码方式采用application/x-www-form-urlencoded
  4、服务器端接收到客户端传来的数据后,用Params.Get()方法把传参接收下来,并在此基础上加入服务器端数据并返回
  5、客户端采用"Socket配合AsynchronazationContext解决跨线程访问无效问题"把服务器端返回的数据显示在UI控件上
  以下是代码内容:
   Page.xaml界面设计代码

    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x
="http://schemas.microsoft.com/winfx/2006/xaml" 
    Width
="400" Height="300">
<StackPanel x:Name="layoutRoot" Background="Wheat" VerticalAlignment="Center">
        
        
<TextBlock Text="HttpWebRequest 通讯示例 " TextAlignment="Center"  FontSize="18" Foreground="red"></TextBlock>
        
<TextBlock Text="用Thread配合AsynchronazationContext解决跨线程访问无效问题" TextAlignment="Center" Foreground="Green"></TextBlock>
        
<TextBlock x:Name="lsResultThread"></TextBlock>
        
<Button Content="ClickMe-Thread" Click="ClickThread"/>         
        
<TextBlock Text="用Socket配合AsynchronazationContext解决跨线程访问无效问题" TextAlignment="Center" Foreground="Green"></TextBlock>
        
<TextBlock x:Name="lsResultSocket"/>
        
<Button  Click="ClicksSocket" Content="ClickMe-Socket"  />

    
</StackPanel>
</UserControl>

  Page.xaml.cs后台代码
SilverLight学习笔记--Silverlight中HttpWebRequest通讯using System;
SilverLight学习笔记--Silverlight中HttpWebRequest通讯
using System.Collections.Generic;
SilverLight学习笔记--Silverlight中HttpWebRequest通讯
using System.Linq;
SilverLight学习笔记--Silverlight中HttpWebRequest通讯
using System.Net;
SilverLight学习笔记--Silverlight中HttpWebRequest通讯
using System.Windows;
SilverLight学习笔记--Silverlight中HttpWebRequest通讯
using System.Windows.Controls;
SilverLight学习笔记--Silverlight中HttpWebRequest通讯
using System.Windows.Documents;
SilverLight学习笔记--Silverlight中HttpWebRequest通讯
using System.Windows.Input;
SilverLight学习笔记--Silverlight中HttpWebRequest通讯
using System.Windows.Media;
SilverLight学习笔记--Silverlight中HttpWebRequest通讯
using System.Windows.Media.Animation;
SilverLight学习笔记--Silverlight中HttpWebRequest通讯
using System.Windows.Shapes;
SilverLight学习笔记--Silverlight中HttpWebRequest通讯
using System.IO;
SilverLight学习笔记--Silverlight中HttpWebRequest通讯
using System.Text;
SilverLight学习笔记--Silverlight中HttpWebRequest通讯
using System.Threading;  //解决线程同步问题
SilverLight学习笔记--Silverlight中HttpWebRequest通讯
using System.Net.Sockets;
SilverLight学习笔记--Silverlight中HttpWebRequest通讯
SilverLight学习笔记--Silverlight中HttpWebRequest通讯
namespace SLHttpWebRequest

  Default.aspx代码
 SLHttpWebRequest.Web
{
    public partial class _Default : System.Web.UI.Page
    {
        
protected void Page_Load(object sender, EventArgs e)
        {

            StringBuilder sb 
= new StringBuilder("Message From Server:");

            
if (Request.ContentType == "text/plain"//首先判断ContentType是不是"text/plain"方式,如果是,则采用StreamReader类来获取Client端传来的参数
            {
                
if (Request.InputStream == null || Request.InputStream.Length <= 0)
                {
                    sb.Append(
"Sorry, There is No Message Come From Client");
                }
                
else
                {
                    
using (Stream inputStream = Request.InputStream)  //接收从Client端传来的参数,由于ContentType是"text/plain",所以用StreamReader来取得对应参数
                    {
                        
using (StreamReader sr = new StreamReader(inputStream))
                        {
                            sb.Append(sr.ReadToEnd());
                        }
                    }
                }

            }

            Response.Write(sb.ToString());
            Response.End();
        }
    }
}
 ServerHandler.ashx代码
 SLHttpWebRequest.Web
{
    ///// <summary>
    
///// $codebehindclassname$ 的摘要说明
    
///// </summary>
    //[WebService(Namespace = "http://tempuri.org/")]
    
//[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    public class ServerHandler : IHttpHandler
    {

        
public void ProcessRequest(HttpContext context)
        {

            StringBuilder sb 
= new StringBuilder("Message From Server:");
            
if (context.Request.ContentType == "application/x-www-form-urlencoded")
            {
//首先判断ContentType是不是"application/x-www-form-urlencoded"方式,如果是,则采用Params.Get方法来获取Client端传来的参数
                try
                {
                    
string paramStr = context.Request.Params.Get("clientMsg");  //接收从Client端传来的参数,由于ContentType是"application/x-www-form-urlencoded",所以用Params.Get方法来取得对应参数
                    sb.Append(paramStr);
                }
                
catch { }
            }

            context.Response.Write(sb.ToString());
            context.Response.End();
        }

        
public bool IsReusable
        {
            
get
            {
                
return false;
            }
        }
    }
}
运行后效果如下
SilverLight学习笔记--Silverlight中HttpWebRequest通讯

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

相关文章:

  • 2021-05-30
  • 2021-07-12
  • 2022-12-23
  • 2021-06-19
  • 2021-09-12
  • 2021-11-07
  • 2021-11-15
猜你喜欢
  • 2021-07-24
  • 2021-07-11
  • 2021-08-10
  • 2021-12-02
  • 2021-06-15
相关资源
相似解决方案