稳扎稳打Silverlight(21) - 2.0通信之WebRequest和WebResponse, 对指定的URI发出请求以及接收响应

稳扎稳打Silverlight(21) - 2.0通信之WebRequest和WebResponse, 对指定的URI发出请求以及接收响应,第1张

概述  [索引页] [源码下载] 稳扎稳打Silverlight(21) - 2.0通信之WebRequest和WebResponse, 对指定的URI发出请求以及接收响应 作者: webabcd 介绍 Silverlight 2.0 详解WebRequest和WebResponse,对指定的URI做GET和POST请求,以及接收其响应     HttpWebRequest - 对指定的 URI 发出   [索引页]
[源码下载]

稳扎稳打Silverlight(21) - 2.0通信之WebRequest和WebResponse,对指定的URI发出请求以及接收响应
作者: webabcd
介绍
Silverlight 2.0 详解WebRequest和WebResponse,对指定的URI做GET和POST请求,以及接收其响应
    httpWebRequest - 对指定的 URI 发出请求
        Create() - 初始化一个 WebRequest
        BeginGetResponse() - 开始对指定 URI 资源做异步请求
        EndGetResponse() - 结束对指定 URI 资源做异步请求
    httpWebResponse - 对指定的 URI 做出响应
        GetResponseStream() - 获取响应的数据流
在线DEMO
http://www.cnblogs.com/webabcd/archive/2008/10/09/1307486.html  
示例
1、对指定的URI做GET请求以及接收响应
WebRequestGet.xaml

< UserControl  x:Class ="Silverlight20.Communication.WebRequestGet"

    xmlns
="http://schemas.microsoft.com/winfx/2006/xaml/presentation"  

    xmlns:x
="http://schemas.microsoft.com/winfx/2006/xaml" >

    
< StackPanel  HorizontalAlignment ="left"  margin ="5" >

    

        
< TextBlock  x:name ="lblMsg"   />

        

    
</ StackPanel >

</ UserControl >


WebRequestGet.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.Threading;

using  System.IO;


namespace  Silverlight20.Communication

{

    
public partial class WebRequestGet : UserControl

    
{

        
// 接收 GET 方式数据的 REST 服务

        string _url = "http://localhost:3036/REST.svc/Users/Json";


        
// 异常信息

        string _exception = "";

        

        
// SynchronizationContext - 同步上下文管理类

        SynchronizationContext _syncContext;


        
public WebRequestGet()

        
@H_20_403@{

            InitializeComponent();


            Demo();

@H_633_419@

        }


        
voID Demo()

        
@H_20_403@{

            
// SynchronizationContext.Current - 当前线程的同步上下文

            _syncContext = SynchronizationContext.Current;


            
@H_20_403@/*

             * httpWebRequest - 对指定的 URI 发出请求

             *     httpWebRequest.Create(uri) - 初始化一个 WebRequest

             *     httpWebRequest.BeginGetResponse(AsyncCallback callback, Object state) - 开始对指定 URI 资源做异步请求

             *         AsyncCallback callback - System.AsyncCallback 委托。异步 *** 作完成时调用的回调方法

             *         Object state - 包含此异步请求的对象。即相应的 httpWebRequest 对象

             *     httpWebRequest.Abort() - 取消该异步请求

             *     httpWebRequest.Accept - http 头的 Accept  部分

             *     httpWebRequest.ContentType - http 头的 ContentType 部分

             *     httpWebRequest.headers - http 头的 key/value 对集合

             *     httpWebRequest.Method - http 方法(只支持GET和POST)

             *     httpWebRequest.RequestUri - 所请求的 URI

             *     httpWebRequest.HaveResponse - 是否接收到了指定 URI 的响应

             *     httpWebRequest.AllowReadStreamBuffering - 是否对从 Internet 资源接收的数据做缓冲处理。默认值为true,将数据缓存在客户端内存中,以便随时被应用程序读取

@H_633_419@

             
*/


            httpWebRequest request 
= WebRequest.Create(

                
new Uri(_url, UriKind.absolute)) as httpWebRequest;

            request.Method 
= "GET";

            request.BeginGetResponse(
new AsyncCallback(ResponseCallback), request);

@H_633_419@

        }


        
private voID ResponseCallback(IAsyncResult result)

        
@H_20_403@{

            
// IAsyncResult.AsyncState - AsyncCallback 传过来的对象

            httpWebRequest request = result.AsyncState as httpWebRequest;


            WebResponse response 
= null;


            
try

            
@H_20_403@{

                
// httpWebRequest.EndGetResponse(IAsyncResult) - 结束对指定 URI 资源做异步请求

                
//     返回值为 WebResponse 对象

                response = request.EndGetResponse(result) as httpWebResponse;

@H_633_419@

            }

            
catch (Exception ex)

            
@H_20_403@{

                _exception 
= ex.ToString();

@H_633_419@

            }


            
// SynchronizationContext.Post(SendOrPostCallback d, Object state) - 将异步消息发送到该同步上下文中

            
//     SendOrPostCallback d - System.Threading.SendOrPostCallback 委托

            
//     Object state - 需要传递的参数

            _syncContext.Post(GetResponse, response);

@H_633_419@

        }


        
private voID GetResponse(object state)

        
{

            
@H_20_403@/*

             * httpWebResponse - 对指定的 URI 做出响应

             *     GetResponseStream() - 获取响应的数据流

@H_633_419@

             
*/


            httpWebResponse response 
= state as httpWebResponse;


            
if (response != null)

            
@H_20_403@{

                Stream responseStream 
= response.GetResponseStream();

                
using (StreamReader sr = new StreamReader(responseStream))

                
@H_20_403@{

                    lblMsg.Text 
= sr.ReadToEnd();

@H_633_419@

                }

@H_633_419@

            }

            
else

            
@H_20_403@{

                lblMsg.Text 
= _exception;

@H_633_419@

            }

@H_633_419@

        }

@H_633_419@

    }

}


2、对指定的URI做POST请求以及接收响应
WebRequestPost.xaml

< UserControl  x:Class ="Silverlight20.Communication.WebRequestPost"

    xmlns
="http://schemas.microsoft.com/winfx/2006/xaml/presentation"  

    xmlns:x
="http://schemas.microsoft.com/winfx/2006/xaml" >

    
< StackPanel  HorizontalAlignment ="left"  margin ="5" >

    

        
< TextBlock  x:name ="lblMsg"   />

    

    
</ StackPanel >

</ UserControl >


WebRequestPost.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.Threading;

using  System.IO;


namespace  Silverlight20.Communication

{

    
public partial class WebRequestPost : UserControl

    
{

        
// 接收 POST 方式数据的 REST 服务

        string _url = "http://localhost:3036/REST.svc/PostUser";


        
// 异常信息

        string _exception = "";


        
// SynchronizationContext - 同步上下文管理类

        SynchronizationContext _syncContext;


        
public WebRequestPost()

        
@H_20_403@{

            InitializeComponent();


            Demo();

@H_633_419@

        }


        
voID Demo()

        
@H_20_403@{

            _syncContext 
= SynchronizationContext.Current;


            httpWebRequest request 
= WebRequest.Create(

                
new Uri(_url, UriKind.absolute)) as httpWebRequest;


            request.Method 
= "POST";


            
// BeginGetRequestStream(AsyncCallback callback, Object state) - 向指定的 URI 资源发送数据的流的异步请求

            
//     AsyncCallback callback - System.AsyncCallback 委托

            
//     Object state - 包含此异步请求的对象。即相应的 httpWebRequest 对象

            IAsyncResult asyncResult = request.BeginGetRequestStream(

                
new AsyncCallback(RequestStreamCallback), request);

@H_633_419@

        }


        
private voID RequestStreamCallback(IAsyncResult result)

        
@H_20_403@{

            httpWebRequest request 
= result.AsyncState as httpWebRequest;

            request.ContentType 
= "application/x-www-form-urlencoded";


            
// httpWebRequest.EndGetRequestStream(IAsyncResult) - 返回用于将数据写入某 URI 资源的 Stream

            Stream requestStream = request.EndGetRequestStream(result);


            StreamWriter streamWriter 
= new StreamWriter(requestStream);


            
// byte[] postdata = System.Text.EnCoding.UTF8.GetBytes("name=webabcd");

            
// 多个参数用“&”分隔

            streamWriter.Write("name=webabcd");


            streamWriter.Close();


            request.BeginGetResponse(
new AsyncCallback(ResponseCallback), request);

@H_633_419@

        }


        
private voID ResponseCallback(IAsyncResult result)

        
@H_20_403@{

            httpWebRequest request 
= result.AsyncState as httpWebRequest;


            WebResponse response 
= null;


            
try

            
@H_20_403@{

                response 
= request.EndGetResponse(result);

@H_633_419@

            }

            
catch (Exception ex)

            
@H_20_403@{

                _exception 
= ex.ToString();

@H_633_419@

            }


            
// 调用 UI 线程

            _syncContext.Post(GetResponse, response);

@H_633_419@

        }


        
private voID GetResponse(object state)

        
{

            
@H_20_403@/*

             * httpWebResponse - 对指定的 URI 做出响应

             *     GetResponseStream() - 获取响应的数据流

             *     ContentLength - 接收的数据的内容长度

             *     ContentType - http 头的 ContentType 部分

             *     Method - http 方法

             *     ResponseUri - 响应该请求的 URI

             *     StatusCode - 响应状态 [System.Net.httpStatusCode枚举]

             *         httpStatusCode.OK - http 状态为 200

             *         httpStatusCode.NotFound - http 状态为 404

             *     StatusDescription - 响应状态的说明

@H_633_419@

             
*/


            httpWebResponse response 
= state as httpWebResponse;


            
if (response != null && response.StatusCode == httpStatusCode.OK)

            
@H_20_403@{

                Stream responseStream 
= response.GetResponseStream();

                
using (StreamReader sr = new StreamReader(responseStream))

                
@H_20_403@{

                    lblMsg.Text 
= string.Format("接收的数据的内容长度:{0}\r\nhttp 头的 ContentType 部分:{1}\r\nhttp 方法:{2}\r\n响应该请求的 URI:{3}\r\n响应状态:{4}\r\n响应状态的说明:{5}\r\n响应的结果:{6}\r\n",

                        response.ContentLength,

                        response.ContentType,

                        response.Method,

                        response.ResponseUri,

                        response.StatusCode,

                        response.StatusDescription,

                        sr.ReadToEnd());

@H_633_419@

                }

@H_633_419@

            }

            
else

            
@H_20_403@{

                lblMsg.Text 
= _exception;

@H_633_419@

            }

@H_633_419@

        }

@H_633_419@

    }

}

OK
[源码下载] @H_502_2083@ 总结

以上是内存溢出为你收集整理的稳扎稳打Silverlight(21) - 2.0通信之WebRequest和WebResponse, 对指定的URI发出请求以及接收响应全部内容,希望文章能够帮你解决稳扎稳打Silverlight(21) - 2.0通信之WebRequest和WebResponse, 对指定的URI发出请求以及接收响应所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://www.54852.com/web/1071686.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-05-26
下一篇2022-05-26

发表评论

登录后才能评论

评论列表(0条)

    保存