JG Vimalan's Blog

It is all about coding!

Sending response back from http handlers

In this post, I will explain about sending a response back from http handler to the calling page. The page that invoked http handler can be either .NET or Java.

I am sending response back in json format.

In the following method, AuthResponse is my custom class. You can use you own class there.

/// <summary>
/// This method sends the response in json format
/// </summary>
/// <param name=”context”></param>
/// <param name=”authResponse”></param>
private void SendResponseInJson(HttpContext context, AuthResponse authResponse)
{
context.Response.ContentType = “application/json”;
context.Response.ContentEncoding = Encoding.UTF8;

System.Web.Script.Serialization.JavaScriptSerializer jsSerializer =
new System.Web.Script.Serialization.JavaScriptSerializer();
string jResponse = jsSerializer.Serialize(authResponse);

context.Response.Write(jResponse);
context.Response.Flush();
}

Read the response from http handler in an ASP.NET page,

HttpWebResponse myResponse = (HttpWebResponse)myRequest.GetResponse();
Stream stream = myResponse.GetResponseStream();
StreamReader reader = new StreamReader(stream);
string result = reader.ReadToEnd();

The result string will contain the actual text that was sent from the http handler.

This can be useful when you want to send and receive data between two different web applications.

Advertisements

August 21, 2011 - Posted by | ASP.NET, C#.NET

No comments yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: