与通用处理程序连接

时间:2013-04-14 19:51:17

标签: c# request response generic-handler

我在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;
            }
        }
    }
}

我也有这个客户端应用程序:

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");
            }
        }
    }
}

现在,让我们假设在我的客户端应用程序中,我提供“添加”作为函数,“3”作为num1,“5”作为num2。

为什么生成的答案(存储在变量答案中)是:

System.Net.HttpWebResponse

我做错了什么?请耐心等待,因为我从未以这种方式在两个申请之间进行沟通。

1 个答案:

答案 0 :(得分:1)

问题在于:

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

您只是在ToString()上拨打HttpWebResponse,而大概是 想要做的就是获取回复正文的内容 - 你要“ d做这样的事情:

// It's important to dispose of responses...
using (var response = request.GetResponse())
{
   using (var reader = new StreamReader(response.GetResponseStream()))
   {
       string answer = reader.ReadToEnd();
   }
}