刚喝了一口咖啡,真苦!
就像生活一样,连心都是苦的。虽然有人说咖啡很不错,可那只是对于适合他的人来说。
在一个地方呆久了,心里也会像咖啡一样苦。
把我今天上午学习的结果粘出来。
aspx页:(其cs页面无任何代码)
Handler.ashx页:
就像生活一样,连心都是苦的。虽然有人说咖啡很不错,可那只是对于适合他的人来说。
在一个地方呆久了,心里也会像咖啡一样苦。
把我今天上午学习的结果粘出来。
aspx页:(其cs页面无任何代码)
1<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Sys_WebRequest.aspx.cs" Inherits="Sys_WebRequest" %>
2
3<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
4
5<html xmlns="http://www.w3.org/1999/xhtml" >
6<head runat="server">
7 <title>无标题页</title>
8</head>
9<body>
10 <form id="form1" runat="server">
11 <div>
12 <asp:ScriptManager ID="ScriptManager1" runat="server">
13 </asp:ScriptManager>
14
15 <asp:UpdatePanel ID="UpdatePanel1" runat="server">
16 <ContentTemplate>
17 <%=DateTime.Now %>
18 <br />
19 <asp:TextBox TextMode="multiLine" ID="txtContent" Rows="5" Columns="4" runat="server" />
20 <asp:Button ID="Button1" runat="server" Text="Refresh" />
21 </ContentTemplate>
22 </asp:UpdatePanel>
23
24 <input type="button" onclick="sendRequest('normal');" value="Normal" />
25 <input type="button" onclick="sendRequest('error');" value="Error" />
26 <input type="button" onclick="sendRequest('timeout');" value="TimeOut" />
27 <input type="button" onclick="try{webRequest.get_executor().abort();}catch(e){alert(e.message+'\n请先单击timeout按钮,并在3秒钟内单击此按钮');}" value="abort" />
28
29 <script type="text/javascript" language="javascript">
30
31 var webRequest;
32 function sendRequest(action)
33 {
34 webRequest = new Sys.Net.WebRequest();
35 webRequest.set_url("handlers/handler.ashx");
36
37 //设置头信息
38 webRequest.get_headers()["action"]=action;
39
40 //设置body信息
41 webRequest.set_body("data=" + encodeURIComponent($get("txtContent").innerHTML));
42 webRequest.set_httpVerb("POST");
43 webRequest.set_timeout(3000);
44
45 webRequest.add_completed(OnCompleted);
46 webRequest.invoke();
47 }
48
49 function OnCompleted(executor,e)
50 {
51 if(executor.get_aborted())
52 {
53 alert("Request Aborted!");
54 }
55 else if(executor.get_responseAvailable())
56 {
57 var statusCode=executor.get_statusCode();
58 alert("statusCode=" + statusCode);
59 if(statusCode < 200|| statusCode >= 300)
60 {
61 alert("Error Occured!");
62 }
63 else
64 {
65 alert(executor.get_responseData());
66 }
67 }
68 else if(executor.get_timedOut())
69 {
70 alert("Time Out!");
71 }
72 else
73 {
74 alert("error occured!");
75 }
76 }
77
78 function pageLoad()
79 {
80 Sys.WebForms.PageRequestManager.getInstance().add_endRequest(OnRequestEnd);
81 }
82 function OnRequestEnd(sender,e)
83 {
84 if(e.get_error())
85 {
86 e.set_errorHandled(true);
87 alert(e.get_error().message);
88 }
89 else if(e.get_response().get_aborted())
90 {
91 }
92 }
93 </script>
94 </div>
95 </form>
96</body>
97</html>
98
2
3<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
4
5<html xmlns="http://www.w3.org/1999/xhtml" >
6<head runat="server">
7 <title>无标题页</title>
8</head>
9<body>
10 <form id="form1" runat="server">
11 <div>
12 <asp:ScriptManager ID="ScriptManager1" runat="server">
13 </asp:ScriptManager>
14
15 <asp:UpdatePanel ID="UpdatePanel1" runat="server">
16 <ContentTemplate>
17 <%=DateTime.Now %>
18 <br />
19 <asp:TextBox TextMode="multiLine" ID="txtContent" Rows="5" Columns="4" runat="server" />
20 <asp:Button ID="Button1" runat="server" Text="Refresh" />
21 </ContentTemplate>
22 </asp:UpdatePanel>
23
24 <input type="button" onclick="sendRequest('normal');" value="Normal" />
25 <input type="button" onclick="sendRequest('error');" value="Error" />
26 <input type="button" onclick="sendRequest('timeout');" value="TimeOut" />
27 <input type="button" onclick="try{webRequest.get_executor().abort();}catch(e){alert(e.message+'\n请先单击timeout按钮,并在3秒钟内单击此按钮');}" value="abort" />
28
29 <script type="text/javascript" language="javascript">
30
31 var webRequest;
32 function sendRequest(action)
33 {
34 webRequest = new Sys.Net.WebRequest();
35 webRequest.set_url("handlers/handler.ashx");
36
37 //设置头信息
38 webRequest.get_headers()["action"]=action;
39
40 //设置body信息
41 webRequest.set_body("data=" + encodeURIComponent($get("txtContent").innerHTML));
42 webRequest.set_httpVerb("POST");
43 webRequest.set_timeout(3000);
44
45 webRequest.add_completed(OnCompleted);
46 webRequest.invoke();
47 }
48
49 function OnCompleted(executor,e)
50 {
51 if(executor.get_aborted())
52 {
53 alert("Request Aborted!");
54 }
55 else if(executor.get_responseAvailable())
56 {
57 var statusCode=executor.get_statusCode();
58 alert("statusCode=" + statusCode);
59 if(statusCode < 200|| statusCode >= 300)
60 {
61 alert("Error Occured!");
62 }
63 else
64 {
65 alert(executor.get_responseData());
66 }
67 }
68 else if(executor.get_timedOut())
69 {
70 alert("Time Out!");
71 }
72 else
73 {
74 alert("error occured!");
75 }
76 }
77
78 function pageLoad()
79 {
80 Sys.WebForms.PageRequestManager.getInstance().add_endRequest(OnRequestEnd);
81 }
82 function OnRequestEnd(sender,e)
83 {
84 if(e.get_error())
85 {
86 e.set_errorHandled(true);
87 alert(e.get_error().message);
88 }
89 else if(e.get_response().get_aborted())
90 {
91 }
92 }
93 </script>
94 </div>
95 </form>
96</body>
97</html>
98
Handler.ashx页:
1<%@ WebHandler Language="C#" Class="Handler" %>
2
3using System;
4using System.Web;
5
6public class Handler : IHttpHandler {
7
8 public void ProcessRequest (HttpContext context) {
9 context.Response.ContentType = "text/plain";
10 string action = context.Request.Headers["action"].ToString();
11
12 switch (action)
13 {
14 case "error":
15 throw new Exception();
16 break;
17 case "normal":
18 //for (int i = 0; i < context.Request.ServerVariables.Count; i++)
19 //{
20 // context.Response.Write(context.Request.ServerVariables.Keys[i] + "=" + context.Request.ServerVariables[i].ToString() + "\n");
21 //}
22 context.Response.Write("发送请求的网页URL:" + context.Request.ServerVariables["HTTP_REFERER"]);
23 context.Response.Write("\n发送过来的数据是:" + context.Request.Form["data"].ToString());
24 break;
25 case "timeout":
26 System.Threading.Thread.Sleep(5000);
27 break;
28 default:
29 context.Response.Write("非法的请求!");
30 break;
31 }
32 }
33
34 public bool IsReusable {
35 get {
36 return false;
37 }
38 }
39
40}
2
3using System;
4using System.Web;
5
6public class Handler : IHttpHandler {
7
8 public void ProcessRequest (HttpContext context) {
9 context.Response.ContentType = "text/plain";
10 string action = context.Request.Headers["action"].ToString();
11
12 switch (action)
13 {
14 case "error":
15 throw new Exception();
16 break;
17 case "normal":
18 //for (int i = 0; i < context.Request.ServerVariables.Count; i++)
19 //{
20 // context.Response.Write(context.Request.ServerVariables.Keys[i] + "=" + context.Request.ServerVariables[i].ToString() + "\n");
21 //}
22 context.Response.Write("发送请求的网页URL:" + context.Request.ServerVariables["HTTP_REFERER"]);
23 context.Response.Write("\n发送过来的数据是:" + context.Request.Form["data"].ToString());
24 break;
25 case "timeout":
26 System.Threading.Thread.Sleep(5000);
27 break;
28 default:
29 context.Response.Write("非法的请求!");
30 break;
31 }
32 }
33
34 public bool IsReusable {
35 get {
36 return false;
37 }
38 }
39
40}
点个广告: