我从以下 http 处理程序中收到“byte[] param = [...]”错误。其他 ashx 文件正在运行。如果您需要更多信息,请告诉我...
请求在此上下文中不可用
描述:执行当前 Web 请求期间发生未处理的异常。请查看堆栈跟踪以获取有关错误及其在代码中的来源的更多信息。
异常详细信息:System.Web.HttpException:请求在此上下文中不可用
public class Handler1 : IHttpHandler
{
public void ProcessRequest(HttpContext context)
{
context.Response.ContentType = "text/plain";
context.Response.Write("Hello World");
//Post back to either sandbox or live
string strSandbox = "https://www.sandbox.paypal.com/cgi-bin/webscr";
string strLive = "https://www.paypal.com/cgi-bin/webscr";
HttpWebRequest req = (HttpWebRequest)WebRequest.Create(strSandbox);
//Set values for the request back
req.Method = "POST";
req.ContentType = "application/x-www-form-urlencoded";
System.Web.UI.Page pa = new System.Web.UI.Page();
//HERE>HERE>HERE>HERE>
byte[] param = pa.Request.BinaryRead(HttpContext.Current.Request.ContentLength);
string strRequest = Encoding.ASCII.GetString(param);
strRequest += "&cmd=_notify-validate";
req.ContentLength = strRequest.Length;
最佳答案
为什么会调用System.Web.UI.Page的Request对象?它不会有一个,因为尚未与请求关联。
您的代码:
System.Web.UI.Page pa = new System.Web.UI.Page();
//HERE>HERE>HERE>HERE>
byte[] param = pa.Request.BinaryRead(HttpContext.Current.Request.ContentLength);
string strRequest = Encoding.ASCII.GetString(param);
不应该读
string strRequest;
StreamReader reader = new StreamReader(context.Request.InputStream);
strRequest = reader.ReadToEnd();
如果您只想获取传入请求的原始字符串。
关于c# - ashx 错误 http 处理程序 2,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7540363/