gpt4 book ai didi

asp.net - MVC4中计算响应长度的简单方法

转载 作者:行者123 更新时间:2023-12-04 18:09:40 24 4
gpt4 key购买 nike

我试图计算 HTTP Response 的长度.
似乎流不想播放。 (不允许读取。内容长度似乎没有设置)
我希望简单地调用 HttpContent 上的一些长度属性回复。
从那以后,我在谷歌上搜索并看到了在我看来像我不理解的过滤器周围的复杂解决方案。
是否可以访问长度(内容本身是选项 extra )
如果没有,我会很感激包含“mvc4/.net 4.5 过滤器”示例的链接
我应该工作,直到我明白。 :-)

   public override void Init()
{
base.Init();
EndRequest += new EventHandler(EndRequestHandler);
}
public void EndRequestHandler(object sender, EventArgs e) {
var admService = new AdminServices();
admService.HTTPTrace(Context);
}


public void HTTPTrace(HttpContext httpContext) {
try {
var eventTrace = new MasterEventTrace();
eventTrace.RemoteAddress = req.UserHostAddress;
eventTrace.RequestLengthBytes = req.ContentLength;

// var targetMemoryStream = new MemoryStream();
// res.OutputStream.CopyTo(targetMemoryStream);
int len;
int.TryParse(res.Headers["Content-Length"], out len );
eventTrace.StatusCode = res.StatusCode;
eventTrace.ResponseLengthBytes = len; // <<<<<<< HOW to calculate this

编辑:根据 Darin 的回复,我得到了这个工作,谢谢 Darin
我做了一些调整以适应这种情况,但除此之外按照建议进行。
它显示了 Global.asax.cs 中的更多内容,并根据需要记录请求和响应信息。
//Global.asax.cs
public override void Init() {
base.Init();
BeginRequest += new EventHandler(BeginRequestHandler);
EndRequest += new EventHandler(EndRequestHandler);
}

public void EndRequestHandler(object sender, EventArgs e)
{
var adminService = new AdminServices();
var handler = Context.Response.Filter as ResponseStreamHandler;
adminService.HTTPTrace(Context, handler);
}

public void BeginRequestHandler(object sender, EventArgs e)
{
BootStrapUnauthentiated();
Context.Response.Filter = new ResponseStreamHandler(Context.Response.Filter);
}

public void HTTPTrace(HttpContext httpContext, ResponseStreamHandler responseStreamFilter)
{
try {
var _ILuwMaster = BosGlobal.BGA.ILuwMaster();

var req = httpContext.Request;
var res = httpContext.Response;
var eventTrace = new MasterEventTrace();
eventTrace.EventName = req.RequestType +":"+ req.Url.LocalPath;
eventTrace.EventDateTime = BosGlobal.BGA.Calendar.Now;
eventTrace.RemoteAddress = req.UserHostAddress;
eventTrace.RequestLengthBytes = req.ContentLength;
eventTrace.ResponseLengthBytes = responseStreamFilter.ResponseSize; //<<<<<<HERE
eventTrace.StatusCode = res.StatusCode;
// save trace entry in DB
_ILuwMaster.GetRepository<MasterEventTrace>().Add(eventTrace);
_ILuwMaster.Commit();
}
catch (Exception ex ) {} // DONT KILL Live traffic when logging errors occur
}

public class ResponseStreamHandler : MemoryStream {
private readonly Stream _responseStream;
public long ResponseSize { get; private set; }
public ResponseStreamHandler(Stream responseStream) {
this._responseStream = responseStream;
ResponseSize = 0;
}

public override void Write(byte[] buffer, int offset, int count) {
this.ResponseSize += count;
this._responseStream.Write(buffer, offset, count);
}

public override void Flush() {
base.Flush();
}
}

最佳答案

你可以写一个 custom Response filter :

public class ResponseLengthCalculatingStream: MemoryStream
{
private readonly Stream responseStream;
private long responseSize = 0;
public ResponseLengthCalculatingStream(Stream responseStream)
{
this.responseStream = responseStream;
}

public override void Write(byte[] buffer, int offset, int count)
{
this.responseSize += count;
this.responseStream.Write(buffer, offset, count);
}

public override void Flush()
{
var responseSize = this.responseSize;
// Here you know the size of the response ...
base.Flush();
}
}

并在您的 Global.asax 中注册:
protected void Application_BeginRequest()
{
Context.Response.Filter = new ResponseLengthCalculatingStream(Context.Response.Filter);
}

如果您只想将此过滤器应用于特定的 Controller 操作,您可以编写自定义操作过滤器,而不是将其应用于 Global.asax 中的 BeginRequest 事件:
public class ResponseLengthCapturingAttribute: ActionFilterAttribute
{
public override void OnActionExecuting(ActionExecutingContext filterContext)
{
var response = filterContext.HttpContext.Response;
response.Filter = new ResponseLengthCalculatingStream(response.Filter);
}
}

然后剩下的就是用相应的 Action 过滤器装饰 Controller Action :
[ResponseLengthCapturing]
public ActionResult Index()
{
...
return View();
}

关于asp.net - MVC4中计算响应长度的简单方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17462995/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com