我在 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");
}
}
}
}
现在,让我们假设在我的客户端应用程序中,我提供“addition”作为函数,“3”作为 num1,“5”作为 num2。
为什么生成的答案(存储在变量答案中)是:
System.Net.HttpWebResponse
我究竟做错了什么?请多多包涵,因为我从未以这种方式在两个应用程序之间进行通信。