WebRequestManager.js 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. // WebRequestManager.js
  2. var displayElement;
  3. function pageLoad()
  4. {
  5. displayElement = $get("ResultId");
  6. }
  7. // Adds invokingRequest and completedRequest
  8. // handlers, and performs a Web request.
  9. function MakeWebRequest()
  10. {
  11. // Clear the previous results.
  12. displayElement.innerHTML = "";
  13. // Instantiate a Web request.
  14. wRequest = new Sys.Net.WebRequest();
  15. // Set the handler to process the Web request.
  16. Sys.Net.WebRequestManager.add_completedRequest(On_WebRequestCompleted);
  17. alert("Added On_WebRequestCompleted handler.");
  18. // Set the handler to call before the Web request
  19. // is executed.
  20. Sys.Net.WebRequestManager.add_invokingRequest(On_InvokingRequest);
  21. alert("Added On_InvokingRequest handler.");
  22. // Set the request Url.
  23. wRequest.set_url("getTarget.htm");
  24. // Execute the request.
  25. // Notice that you do not use the executeRequest method of
  26. // the WebRequestManager which is intended for internal
  27. // use only as in: Sys.Net.WebRequestManager.executeRequest(wRequest).
  28. // The correct way to execute a request is the following:
  29. // wRequest.invoke();
  30. Sys.Net.WebRequestManager.executeRequest(wRequest);
  31. }
  32. // Removes the event handlers that were previusly added.
  33. function RemoveDefaultHandlers()
  34. {
  35. // Clear the previous results.
  36. displayElement.innerHTML = "";
  37. Sys.Net.WebRequestManager.remove_completedRequest(On_WebRequestCompleted);
  38. alert("Removed On_WebRequestCompleted handler.");
  39. Sys.Net.WebRequestManager.remove_invokingRequest(On_InvokingRequest);
  40. alert("Removed On_InvokingRequest handler.");
  41. }
  42. // Gets and sets the default executor.
  43. function DefaultExecutor()
  44. {
  45. // Clear the previous results.
  46. displayElement.innerHTML = "";
  47. // Get system default executor type.
  48. var sysDefaultExecutor =
  49. Sys.Net.WebRequestManager.get_defaultExecutorType();
  50. alert("Get default executor:" + sysDefaultExecutor);
  51. // Modify the default executor type.
  52. Sys.Net.WebRequestManager.set_defaultExecutorType(
  53. "Sys.Net.CustomExecutor");
  54. var customDefaultExecutor =
  55. Sys.Net.WebRequestManager.get_defaultExecutorType();
  56. alert("Set default executor: " + customDefaultExecutor);
  57. // Set the executor back to the system default. This is
  58. // to allow the WebRequest script to run.
  59. executor = "Sys.Net.XMLHttpExecutor";
  60. Sys.Net.WebRequestManager.set_defaultExecutorType(
  61. sysDefaultExecutor);
  62. }
  63. // Gets and sets the default timeout.
  64. function DefaultTimeout()
  65. {
  66. // Clear the previous results.
  67. displayElement.innerHTML = "";
  68. // Get system default timeout.
  69. var sysDefaultTimeout =
  70. Sys.Net.WebRequestManager.get_defaultTimeout();
  71. alert("Get default timeout: " + sysDefaultTimeout);
  72. // Set custom default timeout.
  73. Sys.Net.WebRequestManager.set_defaultTimeout(100);
  74. var customDefaultTimeout =
  75. Sys.Net.WebRequestManager.get_defaultTimeout();
  76. alert("Set default timeout: " + customDefaultTimeout);
  77. // Set the timeout back to the system default.
  78. Sys.Net.WebRequestManager.set_defaultTimeout(
  79. sysDefaultTimeout);
  80. }
  81. // The On_InvokingRequest can be used to perform
  82. // processing prior to the Web request being executed.
  83. function On_InvokingRequest(executor, eventArgs)
  84. {
  85. alert("Executing OnInvokingRequest handler, before the Web request.");
  86. // Add custom code to perform processing prior
  87. // to the request being executed or to abort the
  88. // request.
  89. alert("The current executor is: " +
  90. executor.get_defaultExecutorType());
  91. // Use the eventArgs of type
  92. // NetworkRequestEventArgs to access the
  93. // current WebRequest instance.
  94. var currentRequest = eventArgs.get_webRequest();
  95. var requestUrl = currentRequest.getResolvedUrl();
  96. alert("Current request URL: " + requestUrl);
  97. }
  98. // The On_WebRequestComplete occurs after the
  99. // Web request has returned, and can be used to
  100. // get error status, process returned data, etc...
  101. function On_WebRequestCompleted(executor, eventArgs)
  102. {
  103. if(executor.get_responseAvailable())
  104. {
  105. // Clear the previous results.
  106. displayElement.innerHTML = "";
  107. // Display Web request status.
  108. displayElement.innerHTML +=
  109. "Status: [" + executor.get_statusCode() + " " +
  110. executor.get_statusText() + "]" + "<br/>";
  111. // Display Web request headers.
  112. displayElement.innerHTML +=
  113. "Headers: ";
  114. displayElement.innerHTML +=
  115. executor.getAllResponseHeaders() + "<br/>";
  116. // Display Web request body.
  117. displayElement.innerHTML +=
  118. "Body: ";
  119. displayElement.innerHTML +=
  120. executor.get_responseData();
  121. }
  122. }
  123. if (typeof(Sys) !== "undefined") Sys.Application.notifyScriptLoaded();