新瓶旧酒ASP.NET AJAX(8) - 客户端脚本编程(Sys.Net命名空间下的WebRequestManager)-阿里云开发者社区

开发者社区> 开发与运维> 正文
登录阅读全文

新瓶旧酒ASP.NET AJAX(8) - 客户端脚本编程(Sys.Net命名空间下的WebRequestManager)

简介:
[索引页]
[源码下载]


新瓶旧酒ASP.NET AJAX(8) - 客户端脚本编程(Sys.Net命名空间下的WebRequestManager、WebRequest、WebRequestExecutor和XMLHttpExecutor)


作者:webabcd


介绍
学习一下Sys.Net命名空间下与web请求相关的类,WebRequestManager、WebRequest、WebRequestExecutor和XMLHttpExecutor


关键
1、Sys.Net.WebRequestManager Class
    ·add_completedRequest(handler); - 添加请求完成后的处理器
    ·remove_completedRequest(handler); - 移除请求完成后的处理器
    ·add_invokingRequest(handler); - 添加处理调用请求的处理器
    ·remove_invokingRequest(handler); - 移除处理调用请求的处理器
    ·defaultTimeout - 默认超时时间

2、Sys.Net.WebRequest Class
    ·add_completed(handler); - 添加请求完成后的处理器
    ·remove_completed(handler); - 移除请求完成后的处理器
    ·invoke(); - 执行请求
    ·getResolvedUrl(); - 转换为可用url
    ·body - HTTP的Body内容
    ·executor - 与当前WebRequest对象相关的executor
    ·headers - HTTP的头信息
    ·httpVerb - HTTP请求方式“GET”或“POST”
    ·timeout - 请求超时时间
    ·url - 请求的url
    ·userContext - 用户上下文

3、Sys.Net.WebRequestExecutor Class
    ·abort(); - 终止请求
    ·executeRequest(); - 执行请求
    ·getAllResponseHeaders(); - 获得全部头信息
    ·getResponseHeader(key); - 获得指定头信息
    ·aborted - 请求是否被终止
    ·responseAvailable - 请求是否成功完成
    ·responseData - 响应当前请求的Body文本
    ·started - 是否已经开始处理请求
    ·statusCode - 状态代码
    ·statusText - 状态信息
    ·timedOut - 请求是否超时
    ·xml - 响应当前请求的XMLDOM对象
    ·webRequest - 获得与当前executor相关的WebRequest对象

4、Sys.Net.XMLHttpExecutor Class
    ·abort(); - 终止请求
    ·executeRequest(); - 执行请求
    ·getAllResponseHeaders(); - 获得全部头信息
    ·getResponseHeader(key); - 获得指定头信息
    ·aborted - 请求是否被终止
    ·responseAvailable - 请求是否成功完成
    ·responseData - 响应当前请求的Body文本
    ·started - 是否已经开始处理请求
    ·statusCode - 状态代码
    ·statusText - 状态信息
    ·timedOut - 请求是否超时
    ·xml - 响应当前请求的XMLDOM对象


示例
GetTarget.xml
<?xml version="1.0" encoding="utf-8" ?> 
<root> 
    <employee> 
        <name>webabcd</name> 
        <age>27</age> 
    </employee> 
    <employee> 
        <name>ge</name> 
        <age>26</age> 
    </employee> 
</root>
 
 
PostTarget.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="PostTarget.aspx.cs" Inherits="ClientScripting_SysNet_PostTarget" %> 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml"> 
<head runat="server"> 
        <title>Post Target测试页</title> 

        <script language="C#" runat="server"> 
                 
                protected void Page_Load(object sender, EventArgs e) 
                { 
                        Context.Response.Cache.SetNoStore(); 

                        System.Threading.Thread.Sleep(new Random().Next(0, 1000)); 

                        if (HttpContext.Current.Request.Form["Message"] != null) 
                        { 
                                Label1.Text = HttpContext.Current.Request.Form["Message"]; 
                        } 
                } 
                 
        </script> 

</head> 
<body> 
        <form id="form1" runat="server"> 
                <div> 
                        <h1> 
                                <%= DateTime.Now.ToString() %> 
                        </h1> 
                        <p> 
                                <asp:TextBox ID="Label1" Text="test" runat="server" /> 
                        </p> 
                </div> 
        </form> 
</body> 
</html>
 
 
WebRequestAndWebRequestExecutor.js
var getPage; 
var postPage; 
var displayElement; 

function pageLoad() 

        getPage = "GetTarget.xml"
        postPage = "PostTarget.aspx"
        displayElement = $get("resultId"); 


function GetWebRequest() 

        // 创建WebRequest对象 
        var wRequest =    new Sys.Net.WebRequest(); 
         
        // url - 请求的url    
        wRequest.set_url(getPage); 
         
        // getResolvedUrl() - 转换为可用url 
        alert(wRequest.getResolvedUrl()); 
         
        // httpVerb - HTTP请求方式“GET”或“POST” 
        wRequest.set_httpVerb("GET"); 
                     
        // userContext - 用户上下文 
        wRequest.set_userContext("webabcd"); 
                         
        wRequest.add_completed(OnWebRequestCompleted); 
         
        displayElement.innerHTML = ""; 
                                
        // invoke() - 执行请求 
        wRequest.invoke(); 


function PostWebRequest() 

        // 创建WebRequest对象 
        var wRequest =    new Sys.Net.WebRequest(); 
         
        // url - 请求的url    
        wRequest.set_url(postPage); 
         
        // timeout - 超时时间 
        wRequest.set_timeout(500); 
         
        var body = "Message=信息信息信息信息信息信息信息信息信息信息信息信息信息信息信息" 
        // body - HTTP的Body内容 
        wRequest.set_body(body); 

        // headers - HTTP的头信息 
        wRequest.get_headers()["Content-Length"] = body.length; 
                         
        wRequest.add_completed(OnWebRequestCompleted); 

        displayElement.innerHTML = ""; 
                                
        // invoke() - 执行请求 
        wRequest.invoke();     


// executor - WebRequestExecutor对象 
function OnWebRequestCompleted(executor, e)    
{                 
        // responseAvailable - 请求是否成功完成 
        if(executor.get_responseAvailable())    
        { 
                displayElement.innerHTML = ""; 
        
                // 显示Web Request的用户上下文 
                // webRequest - 获得与当前executor相关的WebRequest对象 
                // userContext - 用户上下文 
                displayElement.innerHTML += "User Context:" + executor.get_webRequest().get_userContext(); 
                displayElement.innerHTML += "<br /><br />"
        
                // 显示Web Request的状态             
                // statusCode - 状态代码 
                // statusText - 状态信息 
                displayElement.innerHTML += "Status Code:" + executor.get_statusCode(); 
                displayElement.innerHTML += "Status Text:" + executor.get_statusText(); 
                displayElement.innerHTML += "<br /><br />"
     
                // 显示Web Request的所有Header                     
                // getAllResponseHeaders() - 获得全部头信息 
                displayElement.innerHTML += "Headers:" + executor.getAllResponseHeaders(); 
                displayElement.innerHTML += "<br /><br />"
                 
                // 显示Web Request的指定Header 
                // getResponseHeader() - 获得指定头信息 
                displayElement.innerHTML += executor.getResponseHeader("Content-Type"); 
                displayElement.innerHTML += "<br /><br />"
                 
                // 显示Web Request的Body                                    
                displayElement.innerHTML += "Body:"
                if (document.all) 
                { 
                        // responseData - 响应当前请求的Body文本 
                        displayElement.innerText += "\r\n" + executor.get_responseData(); 
                } 
                else 
                { 
                        // Firefox 
                        displayElement.textContent += "\r\n" + executor.get_responseData(); 
                } 
                displayElement.innerHTML += "<br /><br />"
                 
                // 显示XML数据 
                displayElement.innerHTML += "XML:"
                if (document.all) 
                { 
                        // xml - 响应当前请求的XMLDOM对象 
                        displayElement.innerText += "\r\n" + executor.get_xml().xml; 
                } 
                else 
                { 
                        // Firefox 
                        displayElement.textContent += "\r\n" + "首节点:" + executor.get_xml().documentElement.nodeName; 
                } 
        } 
        else 
        { 
                // timedOut - 请求是否超时 
                if (executor.get_timedOut()) 
                { 
                        alert("超时"); 
                } 
                // aborted - 请求是否被终止 
                else if (executor.get_aborted()) 
                { 
                        alert("请求被终止"); 
                } 
        } 


// 通知ScriptManager这段脚本已经加载完毕     
if (typeof(Sys) !== "undefined") Sys.Application.notifyScriptLoaded();
 
 
WebRequestAndWebRequestExecutor.aspx
<%@ Page Language="C#" MasterPageFile="~/Site.master" AutoEventWireup="true" CodeFile="WebRequestAndWebRequestExecutor.aspx.cs" 
        Inherits="ClientScripting_SysNet_WebRequestAndWebRequestExecutor" Title="WebRequest和WebRequestExecutor" %> 

<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" runat="Server"> 
        <asp:ScriptManagerProxy ID="ScriptManagerProxy1" runat="Server"> 
                <Scripts> 
                        <asp:ScriptReference Path="~/ClientScripting/SysNet/WebRequestAndWebRequestExecutor.js" /> 
                </Scripts> 
        </asp:ScriptManagerProxy> 
        <p> 
                <input type="button" id="btnGetWebRequest" value="GetWebRequest" /> 
        </p> 
        <p> 
                <input type="button" id="btnPostWebRequest" value="PostWebRequest" /> 
        </p> 
        <hr /> 
        <div id="resultId" style="background-color: Aqua;"> 
        </div> 
</asp:Content>
 
 
运行结果
1、单击“GetWebRequest”按钮
弹出框,信息:当前的URL
显示与“GetTarget.xml”相关的结果

2、单击“PostWebRequest”按钮
可能提示超时,也可能显示与“PostTarget.aspx”相关的结果


WebRequestManagerAndXMLHttpExecutor.js
var displayElement; 

function pageLoad() 

        displayElement = $get("ResultId"); 
         
        // defaultTimeout - 默认超时时间 
        Sys.Net.WebRequestManager.set_defaultTimeout(500); 
         
        Sys.Net.WebRequestManager.add_completedRequest(OnWebRequestCompleted); 
                 
        Sys.Net.WebRequestManager.add_invokingRequest 
        ( 
                function (executor, e) 
                { 
                        alert('调用请求前'); 
                } 
        ); 


function GetData() 

        // 创建WebRequest对象 
        wRequest =    new Sys.Net.WebRequest(); 

        // url - 请求的url    
        wRequest.set_url("PostTarget.aspx"); 
             
        if (document.all) 
        { 
                displayElement.innerText = ""; 
        } 
        else 
        { 
                // Firefox    
                displayElement.textContent = ""; 
        } 
        
        // invoke() - 执行请求 
        wRequest.invoke(); 
         
        if (new Date().getSeconds() % 2 == 0) 
        { 
                // executor - 与当前WebRequest对象相关的executor 
                var executor = wRequest.get_executor(); 
            
                // abort() - 终止请求 
                executor.abort(); 
                 
                // aborted - 与当前executor相关的请求是否被终止 
                alert('请求是否被终止:' + executor.get_aborted()); 
        } 


// executor - WebRequestExecutor对象 
function OnWebRequestCompleted(executor, e)    
{                 
        // responseAvailable - 请求是否成功完成 
        if(executor.get_responseAvailable())    
        { 
                 // 显示Web Request的Body                                    
                displayElement.innerHTML += "Body:"
                if (document.all) 
                { 
                        // responseData - 响应当前请求的Body文本 
                        displayElement.innerText += "\r\n" + executor.get_responseData(); 
                } 
                else 
                { 
                        // Firefox 
                        displayElement.textContent += "\r\n" + executor.get_responseData(); 
                } 
                displayElement.innerHTML += "<br /><br />"
        } 
        else 
        { 
                // timedOut - 请求是否超时 
                if (executor.get_timedOut()) 
                { 
                        alert("超时"); 
                } 
                // aborted - 请求是否被终止 
                else if (executor.get_aborted()) 
                { 
                        alert("请求被终止"); 
                } 
        } 


function ExecuteRequest() 
{         
        // 创建WebRequest对象 
        var wRequest =    new Sys.Net.WebRequest(); 
         
        // url - 请求的url    
        wRequest.set_url("GetTarget.xml"); 
         
        // 创建XMLHttpExecutor对象 
        var executor = new Sys.Net.XMLHttpExecutor(); 
        
        // executor - 与当前WebRequest对象相关的executor 
        wRequest.set_executor(executor);    
         
        // executeRequest() - 执行请求 
        executor.executeRequest(); 

        // executor - 与当前WebRequest对象相关的executor 
        var executor =    wRequest.get_executor(); 
         
        // started - 是否已经开始处理请求 
        alert("executor是否已经开始处理请求:" + executor.get_started()) 


// 通知ScriptManager这段脚本已经加载完毕     
if (typeof(Sys) !== "undefined") Sys.Application.notifyScriptLoaded();
 
 
WebRequestManagerAndXMLHttpExecutor.aspx
<%@ Page Language="C#" MasterPageFile="~/Site.master" AutoEventWireup="true" CodeFile="WebRequestManagerAndXMLHttpExecutor.aspx.cs" 
        Inherits="ClientScripting_SysNet_WebRequestManagerAndXMLHttpExecutor" Title="WebRequestManager和XMLHttpExecutor" %> 

<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" runat="Server"> 
        <asp:ScriptManagerProxy ID="ScriptManagerProxy1" runat="Server"> 
                <scripts> 
                        <asp:ScriptReference Path="~/ClientScripting/SysNet/WebRequestManagerAndXMLHttpExecutor.js" /> 
                </scripts> 
        </asp:ScriptManagerProxy> 
        <p> 
                <input type="button" id="btnGetData" value="GetData" /> 
        </p> 
        <p> 
                <input type="button" id="btnExecuteRequest" value="ExecuteRequest" /> 
        </p> 
        <hr /> 
        <div id="resultId" style="background-color: Aqua;"> 
        </div> 
</asp:Content>
 
运行结果
1、单击“GetData”按钮
可能提示超时,也可能提示终止,也可能显示与“PostTarget.aspx”相关的结果

2、单击“ExecuteRequest”按钮
提示executor已经开始处理请求,显示与“GetTarget.xml”相关的结果


OK
[源码下载]


     本文转自webabcd 51CTO博客,原文链接:http://blog.51cto.com/webabcd/344807,如需转载请自行联系原作者

版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。

分享:
开发与运维
使用钉钉扫一扫加入圈子
+ 订阅

集结各类场景实战经验,助你开发运维畅行无忧

其他文章
最新文章
相关文章