Safari Books Online is a digital library providing on-demand subscription access to thousands of learning resources.
To send an HTTP request from the client, follow these steps:
1. | Create an instance of the Sys.Net.WebRequest object. |
2. | Set its url property to the file on the server. |
3. | Next, attach an event handler to the add_completed function, which is called when the response is received at the client. Finally, call the invoke method to initiate the asynchronous request. |
4. | Create a new aspx page in the existing solution and write the code shown here:
Code View:
Scroll
/
Show All <%@ Page Language="C#" AutoEventWireup="true"
CodeFile="HttpRequest.aspx.cs" Inherits="HttpRequest" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Example for making HTTP Request</title>
<script language="javascript" type="text/javascript">
var outputElement;
// This function performs a GET Web request.
function WebRequest()
{
alert("Making web request...");
// Instantiate a WebRequest.
var request = new Sys.Net.WebRequest();
// Set the request URL.
request.set_url("Items.xml");
// Set the request callback function.
request.add_completed(OnWebRequestCompleted);
outputElement = document.getElementById("divOutput");
// Clear the output area.
outputElement.innerHTML = "";
// Execute the request.
request.invoke();
}
// This callback function processes the
// request return values. It is called asynchronously
// by the current executor.
function OnWebRequestCompleted(executor, eventArgs)
{
alert("Fetching Response...");
if(executor.get_responseAvailable())
{
// Clear the previous results.
outputElement.innerHTML = "";
// Display Web request status.
outputElement.innerHTML +=
"Status: [" + executor.get_statusCode() + " " +
executor.get_statusText() + "]" + "<br/>";
// Display Web request headers.
outputElement.innerHTML +=
"Headers: ";
outputElement.innerHTML +=
executor.getAllResponseHeaders() + "<br/>";
// Display Web request body.
outputElement.innerHTML +=
"Body:";
if(document.all)
outputElement.innerText +=
executor.get_responseData();
else
outputElement.textContent +=
executor.get_responseData();
}
}
</script>
</head>
<body>
<form id="form1" runat="server">
<asp:ScriptManager runat="server" ID="ScriptManagerId">
</asp:ScriptManager>
<table>
<tr align="left">
<td>
Make HTTP Request:</td>
<td>
<button id="Button1" onclick="WebRequest()" type="button">
Request</button>
</td>
</tr>
</table>
<hr />
<div id="divOutput"/>
</form>
</body>
</html>
|