Sys.Net.XmlHttpExecutor executeRequest 方法

更新:2007 年 11 月

执行在关联的 WebRequest 实例中指定的 Web 请求。

MyExecutor.executeRequest;

异常

异常类型

条件

Sys.InvalidOperationException

启动请求后,再次调用了 executeRequest 方法。

- 或 -

执行器未与 WebRequest 类的实例关联。

备注

若要进行 Web 请求,不要直接调用 executeRequest 方法。而应调用 WebRequest.invoke 方法或 WebRequestManager.executeRequest 方法。executeRequest 方法用于未来的扩展或者和自定义执行器一起使用。

说明:

如果直接调用执行器的 executeRequest 方法,则 WebRequestManager 实例将不管理 Web 请求。因此,将不会调用通过调用 WebRequestManager 类的 add_completedRequestadd_invokingRequest 方法添加的任何处理程序。

当您调用 executeRequest 方法时,执行器通过使用为关联的 WebRequest 实例指定的 HTTP 谓词、HTTP 标头和请求正文构造和发送 HTTP 请求。执行器通过使用浏览器的 XMLHTTP 对象发送 HTTP 请求。有关更多信息,请参见 About Native XMLHTTP(关于本机 XMLHTTP)。

如果没有设置关联请求对象的 Content-Type 标头,则执行器将为 POST HTTP 请求将该标头设置为 application/x-www-form-urlencoded,这是默认设置。如果没有设置请求正文,而浏览器要求请求正文,则执行器自动将请求正文设置为空字符串 ("")。此操作强制浏览器发送 Content-Length 标头。执行器确保根据需要调用 Web 请求的已完成事件处理程序。

在调用 executeRequest 后,执行器可能处于以下状态中的一种:

  • 已启动。在执行器成功提交对浏览器中 XMLHTTP 对象的调用后,XmlHttpExecutor.started 属性设置为 true。当执行器处于活动状态时,它将在该实例的生存期内保持此状态。

  • 完成。如果 XMLHTTP 调用成功,则 XmlHttpExecutor.startedXmlHttpExecutor.responseAvailable 属性设置为 true。

    如果浏览器的 XMLHTTP 对象将控制权返回给执行器的内部完成处理程序,则视网络请求为已完成。但是,状态代码、状态文本或响应文本可能指示请求过程中发生了错误。调用代码负责检查响应信息,并确定请求是否返回了有效数据。例如,处理 Web 服务的元素负责检查此信息并确定调用服务时是否发生了错误。

  • 超时。如果关联的请求具有非零超时值,并且超时之前没有发生其他完成事件,则执行器将其状态设置为超时。这指示网络调用没有及时完成,执行器未中止。执行器实例的 startedtimedOut 属性设置为 true。

    说明:

    如果请求的超时值设置为 0,并且 XMLHTTP 请求超时,则执行器将其状态设置为已完成。它将引发请求对象的已完成事件。这意味着执行器实例的 startedresponseAvailable 属性返回 true。调用代码负责检查执行器的状态代码,以检验是否有有效数据(即使请求已完成)。

如果不存在事件处理程序,则当执行器达到完成状态(完成或超时)时,将执行内部清理操作并自行返回。

说明:

如果将 started 属性设置为 true,则无法多次调用 executeRequest。但是如果 XMLHTTP 对象在调用 executeRequest 方法后引发异常,则可以尝试修复此问题,然后再次调用 executeRequest

示例

下面的示例演示如何使用 executeRequest 函数进行 Web 请求。此代码摘自 Sys.Net.XmlHttpExecutor 类概述中的一个完整示例。

// 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);
}

请参见

参考

Sys.Net.WebRequestManager 类

Sys.Net.WebRequestExecutor 类

Sys.Net.XMLHttpExecutor 类

其他资源

About Native XMLHTTP(关于本机 XMLHTTP)