//客户端脚本
//服务器端代码
<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>
<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;
}
}
}
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;
}
}
}
                    
                
                
            
        
浙公网安备 33010602011771号