自强不息,止于至善

身无半文,心忧天下;手释万卷,神交古人
  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

异步通信层

Posted on 2007-10-27 16:43  L.Zhang  阅读(180)  评论(0)    收藏  举报
//客户端脚本
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    
<title>Use Async Communication Layer</title>
    
    
<script language="javascript" type="text/javascript">    
        
var webRequest = null;
    
        
function sendRequest(action)
        {
            webRequest 
= new Sys.Net.WebRequest();
            webRequest.set_url(
"Handlers/Complex.ashx");
            webRequest.get_headers()[
"action"= action;
            webRequest.set_body(
"data=" + encodeURIComponent("Hello World!"));
            webRequest.set_httpVerb(
"POST");
            webRequest.set_timeout(
3000);
            
            webRequest.add_completed(onCompleted);
            webRequest.invoke();
        }
        
        
function onCompleted(response, eventArgs)
        {
            
if (response.get_aborted())
            {
                alert(
"Request aborted!");
            }
            
else if (response.get_responseAvailable())
            {
                
var statusCode = response.get_statusCode();
                
if ((statusCode < 200|| (statusCode >= 300))
                {
                    alert(
"Error occurred!");
                }
                
else
                {
                    alert(response.get_responseData());
                    
// response.get_xml();
                    // response.get_object();
                    // response.getResponseHeader();
                }
            }
            
else if (response.get_timedOut())
            {
                alert(
"Request timed out!");
            }
            
else
            {
                alert(
"Error occurred!");
            }
        }
    
</script>
</head>
<body>
    
<form id="form1" runat="server">
        
<asp:ScriptManager ID="ScriptManager1" runat="server" />
        
        
<input type="button" value="Normal" onclick="sendRequest('normal');" />
        
<input type="button" value="Error" onclick="sendRequest('error');" />
        
<input type="button" value="Timeout" onclick="sendRequest('abc')" />
        
<input type="button" value="Abort" onclick="webRequest.get_executor().abort()" />
    
</form>
</body>
</html>

//服务器端代码
using System;
using System.Web;

public class Complex : IHttpHandler 
{
    
    
public void ProcessRequest(HttpContext context)
    {
        context.Response.ContentType 
= "text/plain";

        
string action = context.Request.Headers["action"];
        
if (action == "normal")
        {
            context.Response.Write(
"You've sent: " + context.Request.Params["data"]);
        }
        
else if (action == "error")
        {
            
throw new Exception();
        }
        
else
        {
            System.Threading.Thread.Sleep(
5000);
        }        
    }
 
    
public bool IsReusable
    {
        
get
        {
            
return false;
        }
    }

}