WebRequest.js 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  1. // WebRequest.js
  2. var getPage;
  3. var postPage;
  4. var displayElement;
  5. function pageLoad()
  6. {
  7. getPage = "getTarget.htm";
  8. postPage = "postTarget.aspx";
  9. displayElement = $get("ResultId");
  10. }
  11. // This function performs a GET Web request
  12. // to retrieve information from the Url specified in
  13. // the query string.
  14. function GetWebRequest()
  15. {
  16. alert("Performing Get Web request.");
  17. // Instantiate the WebRequest object.
  18. var wRequest = new Sys.Net.WebRequest();
  19. // Set the request Url.
  20. wRequest.set_url(getPage);
  21. // Set the request verb.
  22. wRequest.set_httpVerb("GET");
  23. // Set user's context
  24. wRequest.set_userContext("user's context");
  25. // Set the web request completed event handler,
  26. // for processing return data.
  27. wRequest.add_completed(OnWebRequestCompleted);
  28. // Clear the results page element.
  29. displayElement.innerHTML = "";
  30. // Execute the request.
  31. wRequest.invoke();
  32. }
  33. // This function performs a POST Web request
  34. // to upload information to the resource
  35. // identified by the Url.
  36. function PostWebRequest()
  37. {
  38. // Instantiate the WebRequest object.
  39. var wRequest = new Sys.Net.WebRequest();
  40. // Set the request Url.
  41. wRequest.set_url(postPage);
  42. // Set the request verb.
  43. wRequest.set_httpVerb("POST");
  44. var body = "Message=Hello! Do you hear me?"
  45. wRequest.set_body(body);
  46. wRequest.get_headers()["Content-Length"] = body.length;
  47. // Set the web request completed event handler,
  48. // for processing return data.
  49. wRequest.add_completed(OnWebRequestCompleted);
  50. // Clear the results page element.
  51. displayElement.innerHTML = "";
  52. // Execute the request.
  53. wRequest.invoke();
  54. }
  55. // This function adds and removes the
  56. // Web request completed event handler.
  57. function WebRequestCompleted()
  58. {
  59. // Instantiate the WebRequest.
  60. var wRequest = new Sys.Net.WebRequest();
  61. // Set the request Url.
  62. wRequest.set_url(getPage);
  63. // Set the web request completed event handler,
  64. // for processing return data.
  65. wRequest.add_completed(OnWebRequestCompleted);
  66. alert("Added Web request completed handler");
  67. // Remove the web request completed event handler.
  68. // Comment the following two lines if you want to
  69. // use the handler.
  70. wRequest.remove_completed(OnWebRequestCompleted);
  71. alert("Removed handler; the Web request return is not processed.");
  72. // Execute the request.
  73. wRequest.invoke();
  74. }
  75. // This function gets the resolved Url
  76. // of the Web request instance.
  77. function GetWebRequestResolvedUrl()
  78. {
  79. // Instantiate the WebRequest.
  80. var wRequest = new Sys.Net.WebRequest();
  81. // Set the request Url.
  82. wRequest.set_url(getPage);
  83. // Get the web request completed event handler.
  84. var resUrl = wRequest.getResolvedUrl();
  85. alert("Resolved Url: " + resUrl);
  86. // Set the web request completed event handler,
  87. // for processing return data.
  88. wRequest.add_completed(OnWebRequestCompleted);
  89. // Execute the request.
  90. wRequest.invoke();
  91. }
  92. // This function gets and sets the
  93. // Web request time out.
  94. function WebRequestTimeout()
  95. {
  96. // Instantiate the WebRequest.
  97. var wRequest = new Sys.Net.WebRequest();
  98. // Set the request Url.
  99. wRequest.set_url(getPage);
  100. var defaultTimeout =
  101. wRequest.get_timeout();
  102. // Set request timeout to 100 msec.
  103. wRequest.set_timeout(100);
  104. var newTimeout =
  105. wRequest.get_timeout();
  106. alert("Default timeout: " + defaultTimeout);
  107. alert("New timeout: " + newTimeout);
  108. // Set the web request completed event handler,
  109. // for processing return data.
  110. wRequest.add_completed(OnWebRequestCompleted);
  111. // Execute the request.
  112. wRequest.invoke();
  113. }
  114. // This function sets the Web request
  115. // executor, replacing the default one.
  116. function WebRequestExecutor()
  117. {
  118. // Instantiate the WebRequest.
  119. var wRequest = new Sys.Net.WebRequest();
  120. // Create the executor. In this case it is an
  121. // XMLHttpExecutor, equivalent to the default
  122. // executor. But, you can create a custom one.
  123. var executor = new Sys.Net.XMLHttpExecutor();
  124. // Set the executor, replacing the default one.
  125. // In this case the executor is equivalent to the
  126. // default one.
  127. wRequest.set_executor(executor);
  128. // Get the current executor
  129. var executor =
  130. wRequest.get_executor();
  131. alert("Response availabe: " + executor.get_responseAvailable())
  132. }
  133. // This function sets an HTTP header for
  134. // the Web request.
  135. function WebRequestHeader()
  136. {
  137. // Instantiate the WebRequest object.
  138. var wRequest = new Sys.Net.WebRequest();
  139. // Set the request Url.
  140. wRequest.set_url(postPage);
  141. // Set the request verb.
  142. wRequest.set_httpVerb("POST");
  143. var body = "Message=Hello! Do you hear me?"
  144. wRequest.set_body(body);
  145. // Set the value of the HTTP header's "Content-Length".
  146. wRequest.get_headers()["Content-Length"] = body.length;
  147. // Set the web request completed event handler,
  148. // for processing return data.
  149. wRequest.add_completed(OnWebRequestCompletedHeader);
  150. // Clear the results page element.
  151. displayElement.innerHTML = "";
  152. // Execute the request.
  153. wRequest.invoke();
  154. }
  155. // This the handler for the Web request completed event
  156. // that is used to display return data.
  157. function OnWebRequestCompleted(executor, eventArgs)
  158. {
  159. if(executor.get_responseAvailable())
  160. {
  161. // Clear the previous results.
  162. displayElement.innerHTML = "";
  163. // Display Web request status.
  164. DisplayWebRequestStatus(executor);
  165. // Display Web request headers.
  166. DisplayWebRequestHeaders(executor);
  167. // Display Web request body.
  168. DisplayWebRequestBody(executor);
  169. }
  170. else
  171. {
  172. if (executor.get_timedOut())
  173. alert("Timed Out");
  174. else
  175. if (executor.get_aborted())
  176. alert("Aborted");
  177. }
  178. }
  179. // This the handler for the Web request completed event
  180. // that is used to display header information.
  181. function OnWebRequestCompletedHeader(executor, eventArgs)
  182. {
  183. if(executor.get_responseAvailable())
  184. {
  185. // Clear the previous results.
  186. displayElement.innerHTML = "";
  187. // Display Web request headers.
  188. DisplayWebRequestHeaders(executor);
  189. }
  190. else
  191. {
  192. if (executor.get_timedOut())
  193. alert("Timed Out");
  194. else
  195. if (executor.get_aborted())
  196. alert("Aborted");
  197. }
  198. }
  199. // This function is used to display the Web request status.
  200. function DisplayWebRequestStatus(executor)
  201. {
  202. displayElement.innerHTML +=
  203. "Status: [" +
  204. executor.get_statusCode() + " " +
  205. executor.get_statusText() + "]" + "<br/>"
  206. }
  207. // This function is used to display Web request HTTP headers.
  208. function DisplayWebRequestHeaders(executor)
  209. {
  210. displayElement.innerHTML +=
  211. "Headers: ";
  212. displayElement.innerHTML +=
  213. executor.getAllResponseHeaders() + "<br/>";
  214. }
  215. // This function is used to display the Web request body.
  216. function DisplayWebRequestBody(executor)
  217. {
  218. displayElement.innerHTML +=
  219. "Body: ";
  220. if (document.all)
  221. displayElement.innerText +=
  222. executor.get_responseData();
  223. else
  224. // Firefox
  225. displayElement.textContent +=
  226. executor.get_responseData();
  227. }
  228. // This function is used to display the Web request message.
  229. function DisplayInformation(message)
  230. {
  231. // Clear the previous results.
  232. displayElement.innerHTML = "";
  233. // Display information.
  234. displayElement.innerHTML = "<br/>" + message;
  235. }
  236. if (typeof(Sys) !== "undefined") Sys.Application.notifyScriptLoaded();