关于asp.net AJAX还会有很多。
Sys.Net.WebRequest
提供客户端Script功能制造一个Web请求.
Constructors
Sys.Net.WebRequest webRequest Constructor
初始画一个新的WebRequest class的实例.
var wRequest = new Sys.Net.WebRequest();
Members
(1)Sys.Net.WebRequest add_completed Method
结合Web请求实例注册一个事件,
wRequest.add_completed(handler);
(2)Sys.Net.WebRequest completed Method
结合Sys.Net.WebRequest事例添加完整的事件.
(3)Sys.Net.WebRequest.getResolvedUrl Method
得到一个Web请求实例的resolved URL
(4)Sys.Net.WebRequest invoke Method
执行Web请求
Sys.Net.WebRequestManager.invoke(WebRequest);
异常:
Sys.InvalidOperationException 最多只能调用一次
如 果你已经注册completed request event handler为你的Sys.Net.WebRequest 实例,按照网络堆异步调用 handler的请求过程.一个WebRequest实例中只能调用一个invoke.如果网络请求已经发出就会抛出一个异常.
(5)Sys.Net.WebRequest remove_completed Method
删除添加在Handler上的add_completed方法.
返回值必须是字符串,null,或一个XmlDocument对象实例.
(6)Sys.Net.WebRequest executor Property
得到,设置结合Sys.Net.WebRequest实例的执行者
(7)Sys.Net.WebRequest body Property
得到,设置一个Sys.Net.WebRequest实例的HTTP body.
只是在我们要自定义网络执行者的时候才使用.如果你不设置这个属性,
系统默认为Sys.Net.XmlHttpExecutor执行者,设置在调用invoke之后.
如果你的WebRequest实例已经与执行关联并且Web request已经发行.
如 果你尝试设置executor属性,就像这样
myRequest.set_executor(myRequest.get_executor())
就会有一个异常被抛出.
var
body
=
MyWebRequest.get_body();
MyWebRequest.set_body(value);
(8)Sys.Net.WebRequest headers Property
为Sys.Net.WebRequest实例得到HTTP headers,WebRequest发送的
HTTP headers是键值对的字典.headers属性返回为一个字典.
如果没有自定义headers,返回这个字典为空.你使用返回字典来设置你的自定义header 值.
(9)Sys.Net.WebRequest httpVerb Property
得到或设置一个使用Sys.Net.WebRequest类的HTTP verb.
注意,如果之前Http verb没有被设定.那就要在如果body属性为null,
WebRequest返回"GET",如果body属性为null,
WebRequest返回"POST".如果返回body是一个空字符串(""),
但其实不是null,返回的也应该是"POST"
(10)Sys.Net.WebRequest timeout Property
设置或得到Sys.Net.WebRequest实例超时值.当去前的超时值是以毫秒值.
这个超时属性是表示在结合WebRequest实例网络执行者等待网络请求之前的超时时间.
"0"表示下一个网络堆立即输出.
(11)Sys.Net.WebRequest url Property
得到或设置Sys.Net.WebRequest实例的URL.如果URL没被设置,返回值为空字符串("").
你能设置一个完整的URL,一个绝 对的URL,或相对的URL.
WebRequest能将这个值转换给network执行者使用.
(12)Sys.Net.WebRequest userContext Property
得到或设置用户context.返回user context,userContext属性要在WebRequest调用invoke方法之前调用.
Web请求的completed handler能使用相同的userContext.
允许区别不同的事件handler调用者,或重新获得状态或当处理返回数据的context.
WebRequest类被使用在异步交流层中,制造一个Web请求.在你的应用程序中不用刻意使用这个类,而是在你需要在浏览器中调用Web service方法或静态页面方法按照使用代理方法时使用.
你能创建和使用一个WebRequest实例在下面场景中使用:
当你要制造网络请求不是简单的Web service方法调用(就是这 是一种复杂的调用Web service的方法).
当你要设置HTTP请求属性.
当你使用自定义的执行者,要基于Sys.Net.WebRequestExecutor类.
下面是事例来于MSDN:
<%
@ Page Language="C#"
%>
![](/Images/OutliningIndicators/None.gif)
<!
DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"
>
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
<
html
>
<
head
id
="Head1"
runat
="server"
>
![](/Images/OutliningIndicators/None.gif)
<
title
>
Using WebRequest
</
title
>
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
<
style
type
="text/css"
>
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
body {
}{ font: 11pt Trebuchet MS;
font-color: #000000;
padding-top: 72px;
text-align: center }
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
.text {
}{ font: 8pt Trebuchet MS }
</
style
>
![](/Images/OutliningIndicators/None.gif)
</
head
>
![](/Images/OutliningIndicators/None.gif)
<
body
>
![](/Images/OutliningIndicators/None.gif)
<
h2
>
Using WebRequest
</
h2
>
![](/Images/OutliningIndicators/None.gif)
<
form
id
="form1"
runat
="server"
>
<
asp:ScriptManager
runat
="server"
ID
="ScriptManagerId"
>
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
<
Scripts
>
<asp:ScriptReference Path="WebRequest.js" />
</
Scripts
>
</
asp:ScriptManager
>
</
form
>
![](/Images/OutliningIndicators/None.gif)
<
table
>
<
tr
align
="left"
>
<
td
>
Make GET Request:
</
td
>
<
td
>
<
button
id
="Button1"
onclick
="GetWebRequest()"
>
GET
</
button
>
</
td
>
</
tr
>
<
tr
align
="left"
>
<
td
>
Request Body:
</
td
>
<
td
>
<
button
id
="Button2"
onclick
="PostWebRequest()"
>
Body
</
button
>
</
td
>
![](/Images/OutliningIndicators/None.gif)
</
tr
>
<
tr
align
="left"
>
<
td
>
Request Timeout:
</
td
>
<
td
>
<
button
id
="Button3"
onclick
="WebRequestTimeout()"
>
Timeout
</
button
>
</
td
>
</
tr
>
<
tr
align
="left"
>
<
td
>
Request Completed Handler:
</
td
>
<
td
>
<
button
id
="Button4"
onclick
="WebRequestCompleted()"
>
Completed Handler
</
button
>
</
td
>
</
tr
>
![](/Images/OutliningIndicators/None.gif)
<
tr
align
="left"
>
<
td
>
Resolved Url:
</
td
>
<
td
>
<
button
id
="Button5"
onclick
="GetWebRequestResolvedUrl()"
>
Resolved Url
</
button
>
</
td
>
![](/Images/OutliningIndicators/None.gif)
</
tr
>
![](/Images/OutliningIndicators/None.gif)
<
tr
align
="left"
>
<
td
>
Request Executor:
</
td
>
<
td
>
<
button
id
="Button6"
onclick
="WebRequestExecutor()"
>
Executor
</
button
>
</
td
>
![](/Images/OutliningIndicators/None.gif)
</
tr
>
![](/Images/OutliningIndicators/None.gif)
<
tr
align
="left"
>
<
td
>
Request Header:
</
td
>
<
td
>
<
button
id
="Button7"
onclick
="WebRequestHeader()"
>
Header
</
button
>
</
td
>
![](/Images/OutliningIndicators/None.gif)
</
tr
>
![](/Images/OutliningIndicators/None.gif)
</
table
>
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
<
hr
/>
![](/Images/OutliningIndicators/None.gif)
<
div
id
="ResultId"
style
="background-color:Aqua;"
></
div
>
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
</
body
>
![](/Images/OutliningIndicators/None.gif)
</
html
>
var
getPage;
var
postPage;
var
displayElement;
![](/Images/OutliningIndicators/None.gif)
function
pageLoad()
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
getPage = "getTarget.htm";
postPage = "postTarget.aspx";
displayElement = $get("ResultId");
}
![](/Images/OutliningIndicators/None.gif)
//
This function performs a GET Web request
//
to retrieve information from the Url specified in
//
the query string.
function
GetWebRequest()
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
alert("Performing Get Web request.");
![](/Images/OutliningIndicators/InBlock.gif)
// Instantiate the WebRequest object.
var wRequest = new Sys.Net.WebRequest();
![](/Images/OutliningIndicators/InBlock.gif)
// Set the request Url.
wRequest.set_url(getPage);
![](/Images/OutliningIndicators/InBlock.gif)
// Set the request verb.
wRequest.set_httpVerb("GET");
![](/Images/OutliningIndicators/InBlock.gif)
// Set user's context
wRequest.set_userContext("user's context");
![](/Images/OutliningIndicators/InBlock.gif)
// Set the web request completed event handler,
// for processing return data.
wRequest.add_completed(OnWebRequestCompleted);
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
// Clear the results page element.
displayElement.innerHTML = "";
![](/Images/OutliningIndicators/InBlock.gif)
// Execute the request.
wRequest.invoke();
![](/Images/OutliningIndicators/InBlock.gif)
}
![](/Images/OutliningIndicators/None.gif)
//
This function performs a POST Web request
//
to upload information to the resource
//
identified by the Url.
function
PostWebRequest()
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
// Instantiate the WebRequest object.
var wRequest = new Sys.Net.WebRequest();
![](/Images/OutliningIndicators/InBlock.gif)
// Set the request Url.
wRequest.set_url(postPage);
![](/Images/OutliningIndicators/InBlock.gif)
// Set the request verb.
wRequest.set_httpVerb("POST");
![](/Images/OutliningIndicators/InBlock.gif)
var body = "Message=Hello! Do you hear me?"
wRequest.set_body(body);
wRequest.get_headers()["Content-Length"] = body.length;
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
// Set the web request completed event handler,
// for processing return data.
wRequest.add_completed(OnWebRequestCompleted);
![](/Images/OutliningIndicators/InBlock.gif)
// Clear the results page element.
displayElement.innerHTML = "";
![](/Images/OutliningIndicators/InBlock.gif)
// Execute the request.
wRequest.invoke();
}
![](/Images/OutliningIndicators/None.gif)
//
This function adds and removes the
//
Web request completed event handler.
function
WebRequestCompleted()
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
// Instantiate the WebRequest.
var wRequest = new Sys.Net.WebRequest();
![](/Images/OutliningIndicators/InBlock.gif)
// Set the request Url.
wRequest.set_url(getPage);
![](/Images/OutliningIndicators/InBlock.gif)
// Set the web request completed event handler,
// for processing return data.
wRequest.add_completed(OnWebRequestCompleted);
alert("Added Web request completed handler");
![](/Images/OutliningIndicators/InBlock.gif)
// Remove the web request completed event handler.
// Comment the following two lines if you want to
// use the handler.
wRequest.remove_completed(OnWebRequestCompleted);
alert("Removed handler; the Web request return is not processed.");
![](/Images/OutliningIndicators/InBlock.gif)
// Execute the request.
wRequest.invoke();
}
![](/Images/OutliningIndicators/None.gif)
//
This function gets the resolved Url
//
of the Web request instance.
function
GetWebRequestResolvedUrl()
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
// Instantiate the WebRequest.
var wRequest = new Sys.Net.WebRequest();
![](/Images/OutliningIndicators/InBlock.gif)
// Set the request Url.
wRequest.set_url(getPage);
![](/Images/OutliningIndicators/InBlock.gif)
// Get the web request completed event handler.
var resUrl = wRequest.getResolvedUrl();
alert("Resolved Url: " + resUrl);
![](/Images/OutliningIndicators/InBlock.gif)
// Set the web request completed event handler,
// for processing return data.
wRequest.add_completed(OnWebRequestCompleted);
![](/Images/OutliningIndicators/InBlock.gif)
// Execute the request.
wRequest.invoke();
![](/Images/OutliningIndicators/InBlock.gif)
}
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
//
This function gets and sets the
//
Web request time out.
function
WebRequestTimeout()
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
// Instantiate the WebRequest.
var wRequest = new Sys.Net.WebRequest();
![](/Images/OutliningIndicators/InBlock.gif)
// Set the request Url.
wRequest.set_url(getPage);
![](/Images/OutliningIndicators/InBlock.gif)
var defaultTimeout =
wRequest.get_timeout();
![](/Images/OutliningIndicators/InBlock.gif)
// Set request timeout to 100 msec.
wRequest.set_timeout(100);
![](/Images/OutliningIndicators/InBlock.gif)
var newTimeout =
wRequest.get_timeout();
![](/Images/OutliningIndicators/InBlock.gif)
alert("Default timeout: " + defaultTimeout);
alert("New timeout: " + newTimeout);
![](/Images/OutliningIndicators/InBlock.gif)
// Set the web request completed event handler,
// for processing return data.
wRequest.add_completed(OnWebRequestCompleted);
![](/Images/OutliningIndicators/InBlock.gif)
// Execute the request.
wRequest.invoke();
}
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
//
This function sets the Web request
//
executor, replacing the default one.
function
WebRequestExecutor()
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
// Instantiate the WebRequest.
var wRequest = new Sys.Net.WebRequest();
![](/Images/OutliningIndicators/InBlock.gif)
// Create the executor. In this case it is an
// XMLHttpExecutor, equivalent to the default
// executor. But, you can create a custom one.
var executor = new Sys.Net.XMLHttpExecutor();
![](/Images/OutliningIndicators/InBlock.gif)
// Set the executor, replacing the default one.
// In this case the executor is equivalent to the
// default one.
wRequest.set_executor(executor);
![](/Images/OutliningIndicators/InBlock.gif)
// Get the current executor
var executor =
wRequest.get_executor();
![](/Images/OutliningIndicators/InBlock.gif)
alert("Response availabe: " + executor.get_responseAvailable())
}
![](/Images/OutliningIndicators/None.gif)
//
This function sets an HTTP header for
//
the Web request.
function
WebRequestHeader()
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
// Instantiate the WebRequest object.
var wRequest = new Sys.Net.WebRequest();
![](/Images/OutliningIndicators/InBlock.gif)
// Set the request Url.
wRequest.set_url(postPage);
![](/Images/OutliningIndicators/InBlock.gif)
// Set the request verb.
wRequest.set_httpVerb("POST");
![](/Images/OutliningIndicators/InBlock.gif)
var body = "Message=Hello! Do you hear me?"
wRequest.set_body(body);
![](/Images/OutliningIndicators/InBlock.gif)
// Set the value of the HTTP header's "Content-Length".
wRequest.get_headers()["Content-Length"] = body.length;
![](/Images/OutliningIndicators/InBlock.gif)
// Set the web request completed event handler,
// for processing return data.
wRequest.add_completed(OnWebRequestCompletedHeader);
![](/Images/OutliningIndicators/InBlock.gif)
// Clear the results page element.
displayElement.innerHTML = "";
![](/Images/OutliningIndicators/InBlock.gif)
// Execute the request.
wRequest.invoke();
}
![](/Images/OutliningIndicators/None.gif)
//
This the handler for the Web request completed event
//
that is used to display return data.
function
OnWebRequestCompleted(executor, eventArgs)
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
if(executor.get_responseAvailable())
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](/Images/OutliningIndicators/InBlock.gif)
// Clear the previous results.
displayElement.innerHTML = "";
![](/Images/OutliningIndicators/InBlock.gif)
// Display Web request status.
DisplayWebRequestStatus(executor);
![](/Images/OutliningIndicators/InBlock.gif)
// Display Web request headers.
DisplayWebRequestHeaders(executor);
![](/Images/OutliningIndicators/InBlock.gif)
// Display Web request body.
DisplayWebRequestBody(executor);
![](/Images/OutliningIndicators/InBlock.gif)
}
else
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
if (executor.get_timedOut())
alert("Timed Out");
else
if (executor.get_aborted())
alert("Aborted");
}
}
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
//
This the handler for the Web request completed event
//
that is used to display header information.
function
OnWebRequestCompletedHeader(executor, eventArgs)
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
if(executor.get_responseAvailable())
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](/Images/OutliningIndicators/InBlock.gif)
// Clear the previous results.
displayElement.innerHTML = "";
![](/Images/OutliningIndicators/InBlock.gif)
// Display Web request headers.
DisplayWebRequestHeaders(executor);
![](/Images/OutliningIndicators/InBlock.gif)
}
else
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](/Images/OutliningIndicators/InBlock.gif)
if (executor.get_timedOut())
alert("Timed Out");
![](/Images/OutliningIndicators/InBlock.gif)
else
![](/Images/OutliningIndicators/InBlock.gif)
if (executor.get_aborted())
alert("Aborted");
![](/Images/OutliningIndicators/InBlock.gif)
}
}
![](/Images/OutliningIndicators/None.gif)
//
This function is used to display the Web request status.
function
DisplayWebRequestStatus(executor)
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
displayElement.innerHTML +=
"Status: [" +
executor.get_statusCode() + " " +
executor.get_statusText() + "]" + "<br/>"
}
![](/Images/OutliningIndicators/None.gif)
//
This function is used to display Web request HTTP headers.
function
DisplayWebRequestHeaders(executor)
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
displayElement.innerHTML +=
"Headers: ";
displayElement.innerHTML +=
executor.getAllResponseHeaders() + "<br/>";
}
![](/Images/OutliningIndicators/None.gif)
//
This function is used to display the Web request body.
function
DisplayWebRequestBody(executor)
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
displayElement.innerHTML +=
"Body: ";
if (document.all)
displayElement.innerText +=
executor.get_responseData();
else
// Firefox
displayElement.textContent +=
executor.get_responseData();
}
![](/Images/OutliningIndicators/None.gif)
//
This function is used to display the Web request message.
function
DisplayInformation(message)
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
// Clear the previous results.
displayElement.innerHTML = "";
// Display information.
displayElement.innerHTML = "<br/>" + message;
}
![](/Images/OutliningIndicators/None.gif)
if
(
typeof
(Sys)
!==
"
undefined
"
) Sys.Application.notifyScriptLoaded();
可能上面的着个实例有一点复杂..但是一个很好的实力。