Question

I have this generic handler in C#:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace RequestResponse
{
    public class Handler : IHttpHandler
    {
        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            string function = context.Request.QueryString["function"];

            Methods m = new Methods();

            if(function.Equals("addition"))
            {
                int num1 = Convert.ToInt32(context.Request.QueryString["num1"]);
                int num2 = Convert.ToInt32(context.Request.QueryString["num2"]);
                int answer = m.addition(num1, num2);
                context.Response.Write(answer);
            }

            if (function.Equals("subtraction"))
            {
                int num1 = Convert.ToInt32(context.Request.QueryString["num1"]);
                int num2 = Convert.ToInt32(context.Request.QueryString["num2"]);
                int answer = m.subtraction(num1, num2);
                context.Response.Write(answer);
            }

            if(function.Equals("reverseString"))
            {
                string text = context.Request.QueryString["text"];
                text = m.reverseString(text);
                context.Response.Write(text);
            }

            if (function.Equals("CalculateMD5Hash"))
            {
                string text = context.Request.QueryString["text"];
                text = m.CalculateMD5Hash(text);
                context.Response.Write(text);
            }
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}

I also have this client application:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Net;

namespace Client
{
    public partial class HomePage : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (TextBox_Function.Text.Equals("addition"))
            {
                int num1 = Convert.ToInt32(TextBox_Num1.Text);
                int num2 = Convert.ToInt32(TextBox_Num2.Text);
                HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://localhost:4000/Handler.ashx?function=" + TextBox_Function.Text + "&num1=" + num1 + "&num2=" + num2);
                HttpWebResponse response = (HttpWebResponse)request.GetResponse();
                String answer = response.ToString();
                Session["answer"] = answer;
                Response.Redirect("Results.aspx");
            }

            if (TextBox_Function.Text.Equals("subtraction"))
            {
                int num1 = Convert.ToInt32(TextBox_Num1.Text);
                int num2 = Convert.ToInt32(TextBox_Num2.Text);
                HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://localhost:4000/Handler.ashx?function=" + TextBox_Function.Text + "&num1=" + num1 + "&num2=" + num2);
                HttpWebResponse response = (HttpWebResponse)request.GetResponse();
                String answer = response.ToString();
                Session["answer"] = answer;
                Response.Redirect("Results.aspx");
            }

            if (TextBox_Function.Text.Equals("reverseString"))
            {
                String text = TextBox_Text.Text;
                HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://localhost:4000/Handler.ashx?function=" + TextBox_Function.Text + "&text=" + text);
                HttpWebResponse response = (HttpWebResponse)request.GetResponse();
                String answer = response.ToString();
                Session["answer"] = answer;
                Response.Redirect("Results.aspx");
            }

            if (TextBox_Function.Text.Equals("CalculateMD5Hash"))
            {
                String text = TextBox_Text.Text;
                HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://localhost:4000/Handler.ashx?function=" + TextBox_Function.Text + "&text=" + text);
                HttpWebResponse response = (HttpWebResponse)request.GetResponse();
                String answer = response.ToString();
                Session["answer"] = answer;
                Response.Redirect("Results.aspx");
            }
        }
    }
}

Now, let us assume that in my client application, I provide "addition" as the function, "3" as num1 and "5" as num2.

Why does the answer generated (stored in variable answer) is:

System.Net.HttpWebResponse

What am I doing wrong? Please bear with me as I have never communicated between two applications in this way.

Was it helpful?

Solution

The problem is here:

HttpWebResponse response = (HttpWebResponse)request.GetResponse();
String answer = response.ToString();

You're just calling ToString() on the HttpWebResponse, whereas presumably what you really want to do is get the contents of the body of the response - which you'd do with something like this:

// It's important to dispose of responses...
using (var response = request.GetResponse())
{
   using (var reader = new StreamReader(response.GetResponseStream()))
   {
       string answer = reader.ReadToEnd();
   }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top