Sys.Net.XMLHttpExecutor 类总结
Sys.Net.XMLHttpExecutor Class
使用XMLHTTP产生一个异步请求.
var executor = new Sys.Net.XMLHttpExecutor();
XmlHttpExecutor类是默认的执行者和是一个WebRequestExecutor抽像类的实现.因为默认执行者已经被设置,你不能创建这个 类的实例并且和它相结合去做一个Web请求.可是如果你定义自定义的执行者,你必须创建执行者的实例并且设置它为默认Web请求的实例.
网络调用完成之后,XmlHttpExecutor对象应该只是使用来获得响应的数据,之后它应该被抛弃.
Sys.Net.XMLHttpExecutor Constructor
当实现一个派生类的时初始化一个Sys.Net.XMLHttpExecutor类的实例.
Sys.Net.XmlHttpExecutor abort Method
停止执行者等待网络请求发行.
一个Web request被停止后,要等待time-out时间退出后才会结束.
当调用abort时,这个executor设置aborted 属性为true.这样 XmlHttpExecutor实例的get_started和get_aborted 的方法都返回为true.
WebRequestManager实例后结合请求对象调用completed事件的handler,abort方法调用后,响应返回的状态是underfined.
你能调用abort方法多次,但是第二调用是并发调用是没有处理的,所以completed事件hander是只能添加一次.
如果你调用abort之前你调用executeRequest方法,将会有一个异常被抛出.
Sys.Net.XmlHttpExecutor executeRequest Method
与WebRequest实例相结合执行一个Web请求.
产生一个Web请求时,不要立即调用executeRequest方法.而上,你调用WebRequest.invoke方法或WebRequestManager.executeRequest方法.这个executeRequest是提供给未来自定义执行者而使用的.
当你调用executeRequest方法,这个执行者构造器和使用一个由WebRequest实例指定的HTTP verb,HTTP headers和请求body发送一个HTTP请求.使用浏览器的XMLHTTP对象执行者发送HTTP请求.
如果请求对象没有设置header的Content-Type,这个执行者就会为一个a Post HTTP设置它的application/x-www-form-urlencoded ,这个其实也是默认设置.如果请求对象没有设置body和浏览器所需的请求body,这个执行者会自动设置body为空字符传("");这是强制浏览器发送一个Conten-Length header.这个执行者确保Web 请求的completed事件handler调用所需.
调用executeRequest时的情况为有下面:
(1)Started:执行者已经成功提交调用XMLHTTP对象,XmlHttpExecutor.started属性设置为true.当执行被激活,它将包括在这个实例的整个生命周期.
(2)Complete:如果XMLHttp调用成功,XmlHttpExecutor.started和XmlHttpExecutor.responseAvailable属性设置为"true".
(3)Timed-out:如请求的time-out已经不是"0"值,并且time-out时间已经过去没有其他完成事件触发,执行者会为timed-out的状态设置,并且执行者没有调用 aborted.这 个执行者实例的stated和time-out属性设置为true.
当执行者达到完成状态,如果现在没有handers,它会执行内部清楚和默默的返回.
Sys.Net.XmlHttpExecutor.getAllResponseHeaders Method
返回所有的响应Headers
如果成功请求,这个getAllResponseHeader方法返回一个包括所有响应headers的字符串.如果请求没有成功,一个error是返回值.
你能调用getAllResponseHeaders方法的代码可以放在Competed 事件handler中运行,或运行在调用Completed事件handler的堆中.如果你调用getAllResponseHeaders方法时候,当执行者没有返回成功,就会有一个异常被抛出.因为执行者已经发布,将引用浏览器的XMLHTTP对象发布出去.
测试:
resultElementId.innerHTML +=
"All Request Headers: " +
executor.getAllResponseHeaders() + "<br/>";
Sys.Net.WebRequestExecutor.getResponseHeader Method
得到响应Header的值,返回值是响应Header.
Sys.Net.XmlHttpExecutor.getResponseHeader Method
得到响应header的值基于Header's name.
如果Web请求completed成功,你能调用getResponseHeader方法得到响应header的值基于Header's name.如果header现在没得,就会返回空字符串("");
Sys.Net.XmlHttpExecutor aborted Property
返回一个值反映执行者是否已经停止,aborted属性返回true只是在如果执行者停止为true.否则为false.注意;如果网络请求发送到到浏览器的XMLHTTP对象返回不成功,而因为它被停止或因为超时.或其他方面返回false.
Sys.Net.XmlHttpExecutor responseAvailable Property
返回一个表示网络请求返回没有被aborted或超时的值.返回值如果网络请求被aborted或超时;返回true,否则为false.
responseAvailable属性返回true只有如果网络请求发送发送到浏览器XMLHTTP对象返回潜在会成功没有被停止或超时间.其他方面为false.
Sys.Net.XmlHttpExecutor responseData Property
得到响应body的文本表达.返回响应body的文本表达.如果有;就返回,否则返回空字符串(""),
如果请求成功,你能使用responseData属性值得到响应body的文本呈现形式.如果没有响应body,返回值为空字符串.("").
执行者不想将任何字符转换为响应数据.而是,它依赖浏览器的XMLHTTP实现Unicode转换行为.为Microsoft Internet Explorer,这个响应返回的如UTF-8文本除非服务端已经包括一个byte有序的标记(BOM)在响应的开始指出一个不同的Unicode变化.
你能使用responseData属性代码只能运行在Completed事件handler里面 ,或直接从Completed事件handler的运行.如果你使用responseData属性而当你执行者没有返回有效响应数据就要抛出一个异常,或如果已经没有响应.如果你使用responseData属性在Completed事件已经触发之后.就会抛出一个异常.因为执行者已经发布它引用的浏览器的XMLHTTP对象.
Sys.Net.XmlHttpExecutor started Property
返回值表示执行者是否之前已经请求浏览器XMLHTTP对象.返回值如果执行者之前请求已经请求浏览器XMLHTTP对象为true,其他为false,started属性返回true,执行折已经调用发送XMLHTTP对象的sender方法.这个属性返回true,直到请求已经发送,这时返回false.
Sys.Net.XmlHttpExecutor statusCode Property
得到浏览器的XMLHTTP对象的status code.返回值一个字符串("200 ok"为请求成功),你能使用statusCode属性的代码只能运行在completed事件handler,或运行在调用completed事件handler的堆.如果使用statusCode属性当执行者返回不成功,就抛出异常,或如果responseAvailable属性返回false.如果使用statusCode属性使用在completed event后触发.就会有异常被抛出,这是因为执行者已经将浏览器的XMLHTTP对象的引用发送.
Sys.Net.XmlHttpExecutor statusText Property
得到浏览器 XMLHTTP object的status text.
如果请求completed成功,你status属性能得到浏览器XMLHTTP 对象中的status text值.
Sys.Net.XmlHttpExecutor timedOut Property
返回值代表是否请求超时,返回false.
Sys.Net.XmlHttpExecutor xml Property
返回XMLDOM object包括浏览器XMLHTTP对象响应的XML.
var xml = MyExecutor.get_xml();
当响应文本从服务端发来有效的XML,执行者转换踏进入一个XMLDOM对象.如执行不能转换响应文本为一个XMLDOM对象哪个。它就为NUll.
你只能使用这个Xml属性的代码放在a completed事件的handler, or 运行在completed事件 handler调用的堆中.如果你使用XML属性在执行者不返回值成功,就会抛出一个异常,或如果XmlHttpExecutor.get_responseAvailable方法返回false.如果你使用xml属性在Completed事件触发之前.也会抛出一个异常.
var resultElementId;
function pageLoad()
{
resultElementId = $get("ResultId");
}
// This function aborts a Web request.
function AbortWebRequest()
{
// Create the WebRequest object.
wRequest = new Sys.Net.WebRequest();
// Set the request Url.
wRequest.set_url("getTarget.htm");
// Clear the results area.
resultElementId.innerHTML = "";
// Set the Completed event handler,
// for processing return data
wRequest.add_completed(OnCompleted);
// Make the request.
wRequest.invoke();
// Get the current executor.
var executor = wRequest.get_executor();
// Abort the request.
executor.abort();
// Check if the executor is aborted.
var execAborted =
executor.get_aborted();
alert("Executor aborted: " + execAborted);
}
// This function executes a Web request.
function ExecuteWebRequest()
{
// Create the WebRequest object.
wRequest = new Sys.Net.WebRequest();
// Set the request Url.
wRequest.set_url("getTarget.htm");
// Set the Completed event handler, for processing return data
wRequest.add_completed(OnCompleted);
// Clear the results area.
resultElementId.innerHTML = "";
// To use executeRequest you must instantiate the
// executor, assign it to the Web request instance,
// then call the executeRequest function.
// Note: Normally to make a Web request you use
// the invoke method of the WebRequest instance.
var executor = new Sys.Net.XMLHttpExecutor();
wRequest.set_executor(executor);
executor.executeRequest();
var started = executor.get_started();
alert("Executor started: " + started);
}
// This is the event handler called after
// the Web request returns.
function OnCompleted(executor, eventArgs)
{
if(executor.get_responseAvailable())
{
// Get the Web request instance.
var webReq = executor.get_webRequest();
// Display request Url.
alert(webReq.get_url());
// Clear the previous results.
resultElementId.innerHTML = "";
// Display the Web request status.
resultElementId.innerHTML +=
"Request Status: [" + executor.get_statusCode() + " " +
executor.get_statusText() + "]" + "<br/>";
// Display the Web request headers.
resultElementId.innerHTML += "Headers: <br/>";
// Get all the headers.
resultElementId.innerHTML +=
"All Request Headers: " +
executor.getAllResponseHeaders() + "<br/>";
// Get a specific header.
resultElementId.innerHTML +=
"Content-Type Header: " +
executor.getResponseHeader("Content-Type") +
"<br/>";
// Display Web request body.
resultElementId.innerHTML += "Body: <br/>";
resultElementId.innerText +=
executor.get_responseData();
}
else
{
if (executor.get_timedOut())
alert("Timed Out");
else
if (executor.get_aborted())
alert("Aborted");
}
}
// This is the event handler called after
// the Web request returns. It is designed
// for Web requests that return XML.
function OnSucceededXml(executor, eventArgs)
{
if (executor.get_responseAvailable())
{
// Display XML.
if (document.all)
resultElementId.innerText += executor.get_xml().xml;
else
// Firefox
resultElementId.textContent += "First node: " +
executor.get_xml().documentElement.nodeName;
}
else
{
if (executor.get_timedOut())
alert("Timed Out");
else
if (executor.get_aborted())
alert("Aborted");
}
}
// This function executes a Web request
// to get XML data.
function GetXml()
{
// Create the WebRequest object.
wRequest = new Sys.Net.WebRequest();
// Set the request Url.
wRequest.set_url("getTarget.xml");
// Set the Completed event handler
// for processing return data.
wRequest.add_completed(OnSucceededXml);
// Clear the results area.
resultElementId.innerText = "";
// Invoke the Web request.
wRequest.invoke();
}
function pageLoad()
{
resultElementId = $get("ResultId");
}
// This function aborts a Web request.
function AbortWebRequest()
{
// Create the WebRequest object.
wRequest = new Sys.Net.WebRequest();
// Set the request Url.
wRequest.set_url("getTarget.htm");
// Clear the results area.
resultElementId.innerHTML = "";
// Set the Completed event handler,
// for processing return data
wRequest.add_completed(OnCompleted);
// Make the request.
wRequest.invoke();
// Get the current executor.
var executor = wRequest.get_executor();
// Abort the request.
executor.abort();
// Check if the executor is aborted.
var execAborted =
executor.get_aborted();
alert("Executor aborted: " + execAborted);
}
// This function executes a Web request.
function ExecuteWebRequest()
{
// Create the WebRequest object.
wRequest = new Sys.Net.WebRequest();
// Set the request Url.
wRequest.set_url("getTarget.htm");
// Set the Completed event handler, for processing return data
wRequest.add_completed(OnCompleted);
// Clear the results area.
resultElementId.innerHTML = "";
// To use executeRequest you must instantiate the
// executor, assign it to the Web request instance,
// then call the executeRequest function.
// Note: Normally to make a Web request you use
// the invoke method of the WebRequest instance.
var executor = new Sys.Net.XMLHttpExecutor();
wRequest.set_executor(executor);
executor.executeRequest();
var started = executor.get_started();
alert("Executor started: " + started);
}
// This is the event handler called after
// the Web request returns.
function OnCompleted(executor, eventArgs)
{
if(executor.get_responseAvailable())
{
// Get the Web request instance.
var webReq = executor.get_webRequest();
// Display request Url.
alert(webReq.get_url());
// Clear the previous results.
resultElementId.innerHTML = "";
// Display the Web request status.
resultElementId.innerHTML +=
"Request Status: [" + executor.get_statusCode() + " " +
executor.get_statusText() + "]" + "<br/>";
// Display the Web request headers.
resultElementId.innerHTML += "Headers: <br/>";
// Get all the headers.
resultElementId.innerHTML +=
"All Request Headers: " +
executor.getAllResponseHeaders() + "<br/>";
// Get a specific header.
resultElementId.innerHTML +=
"Content-Type Header: " +
executor.getResponseHeader("Content-Type") +
"<br/>";
// Display Web request body.
resultElementId.innerHTML += "Body: <br/>";
resultElementId.innerText +=
executor.get_responseData();
}
else
{
if (executor.get_timedOut())
alert("Timed Out");
else
if (executor.get_aborted())
alert("Aborted");
}
}
// This is the event handler called after
// the Web request returns. It is designed
// for Web requests that return XML.
function OnSucceededXml(executor, eventArgs)
{
if (executor.get_responseAvailable())
{
// Display XML.
if (document.all)
resultElementId.innerText += executor.get_xml().xml;
else
// Firefox
resultElementId.textContent += "First node: " +
executor.get_xml().documentElement.nodeName;
}
else
{
if (executor.get_timedOut())
alert("Timed Out");
else
if (executor.get_aborted())
alert("Aborted");
}
}
// This function executes a Web request
// to get XML data.
function GetXml()
{
// Create the WebRequest object.
wRequest = new Sys.Net.WebRequest();
// Set the request Url.
wRequest.set_url("getTarget.xml");
// Set the Completed event handler
// for processing return data.
wRequest.add_completed(OnSucceededXml);
// Clear the results area.
resultElementId.innerText = "";
// Invoke the Web request.
wRequest.invoke();
}
// This function aborts a Web request.
function AbortWebRequest()
{
// Create the WebRequest object.
wRequest = new Sys.Net.WebRequest();
// Set the request Url.
wRequest.set_url("getTarget.htm");
// Clear the results area.
resultElementId.innerHTML = "";
// Set the Completed event handler,
// for processing return data
wRequest.add_completed(OnCompleted);
// Make the request.
wRequest.invoke();
// Get the current executor.
var executor = wRequest.get_executor();
// Abort the request.
executor.abort();
// Check if the executor is aborted.
var execAborted =
executor.get_aborted();
alert("Executor aborted: " + execAborted);
}
function AbortWebRequest()
{
// Create the WebRequest object.
wRequest = new Sys.Net.WebRequest();
// Set the request Url.
wRequest.set_url("getTarget.htm");
// Clear the results area.
resultElementId.innerHTML = "";
// Set the Completed event handler,
// for processing return data
wRequest.add_completed(OnCompleted);
// Make the request.
wRequest.invoke();
// Get the current executor.
var executor = wRequest.get_executor();
// Abort the request.
executor.abort();
// Check if the executor is aborted.
var execAborted =
executor.get_aborted();
alert("Executor aborted: " + execAborted);
}