Making Web Requests in the ASP.NET AJAX
One of the very cool aspects of the ASP.NET AJAX is its integrated capabilities with the .NET framework and its main namespaces. This has turned the ASP.NET AJAX to a general framework for client-side development that most likely you have used in your projects or will use someday.
However, this framework comes handy in some circumstances and one of them was a case that happened to me when developing Waegis. The scenario was making HTTP calls to other pages in order to fetch their content without posting back to the server and without putting the overhead on the server. The solution could be making calls in clients-side using a technique. Thank to Professional ASP.NET 2.0 AJAX (which is an amazing book on the ASP.NET AJAX), I knew that this is possible via ASP.NET AJAX framework.
Even though I didn’t go live with this solution (for some security and quality reasons) but that was a good experience to have in a few minutes because I never had such experience with the ASP.NET AJAX in this way!
The fundamental of making HTTP request calls in the ASP.NET AJAX is pretty simple. You use the AJAX framework and its objects to make a request. Of course, these objects are corresponded to BCL classes especially System.Net classes.
Here Sys.Net.WebRequest in the ASP.NET AJAX framework comes into the play to help you make HTTP calls with different verbs.
The simplest form of the usage of this object is making a GET HTTP call. Here I create a web page that will fetch and display the HTML content of another page in a dialogue on load event.
I create a new Sys.Net.WebRequest object and use its set_url property to set the address of the target web page. Then add a handler for the completed event of the call in order to fetch and display the data in this handler and finally call the invoke method to make the request.
In completed event handler I use get_responseAvailable of result object to check if there is any response and then display the content of get_responseData. This simply does the job that I want!
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="AjaxWebRequestsSample._Default" %>
<!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>Making Web Requests in ASP.NET AJAX</title>
<script type="text/javascript">
function pageLoad()
{
var webRequest = new Sys.Net.WebRequest();
webRequest.set_url("SamplePage.aspx");
webRequest.add_completed(completed);
webRequest.invoke();
}
function completed(result, args)
{
if (result.get_responseAvailable())
{
alert(result.get_responseData());
}
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>
Making a web request ...
</div>
</form>
</body>
</html>
Note that I have access to some other properties as well. For example, I could use get_statusCode and get_statusText to get the HTTP status code and description of he response.
This simple object can be used for other HTTP verbs as well. As another example, I use it to make a POST call to another page.
So I create a new page named SamplePage2.aspx that displays the value of Param1 of its HTTP parameter in a label. The code-behind for this page looks like this:
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
namespace AjaxWebRequestsSample
{
public partial class SamplePage2 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
this.lblOutput.Text = Server.HtmlDecode(Request.Form["Param1"]);
}
}
}
Now I update my caller page to set the HTTP verb to POST. This is possible via set_httpVerb. I also need to use set_body to set the value of Param1 parameter.
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="AjaxWebRequestsSample._Default" %>
<!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>Making Web Requests in ASP.NET AJAX</title>
<script type="text/javascript">
function pageLoad()
{
var webRequest = new Sys.Net.WebRequest();
webRequest.set_url("SamplePage2.aspx");
webRequest.set_httpVerb("POST");
webRequest.set_body("Param1=Keyvan Nayyeri");
webRequest.add_completed(completed);
webRequest.invoke();
}
function completed(result, args)
{
if (result.get_responseAvailable())
{
alert(result.get_responseData());
}
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>
Making a web request ...
</div>
</form>
</body>
</html>
In general WebRequest object has some helpful properties to make HTTP requests. They are very similar to System.Net.WebRequest class properties and methods. For example, you can set a timeout limit for your requests with set_timeout.
You can also download the source code sample of this post.
[advertisement] Axosoft OnTime 2008 is four developer tools in one: bug tracking, project wiki, feature management, and help desk. It manages your development process so developers can focus on coding. Installed or Hosted – Free Single-user license -- Free 30-day team trial.
3 Comments : 06.16.08
Feedbacks
@Thomas:
Are you advertising something?!
Pingback from Dew Droplet - June 17, 2008 | Alvin Ashcraft's Morning Dew
#1
Thomas Hansen
06.16.2008 @ 7:54 PM
Have you tried http://ajaxwidgets.com ?
You don't have the WebRequest classes, but you do have "callPageMethod" which in addition to the ASP.NET AJAX stuff also can call Page Methods in UserControls and even MasterPages and even nested hierarchies of both of those constructs. Though you rarely need to use those constructs due to its WebControl coupling...