上传文件后,服务器不能附加HTTP头

本文关键字:HTTP 不能 服务器 文件 | 更新日期: 2023-09-27 18:01:45

我在asp.net mvc 5 c# web应用程序中间歇性地得到此异常:

服务器不能在发送HTTP报头后附加报头。

上传图片到S3方法(Web Api Controller)。

Global.asax中的presentrequesheaders

protected void Application_PreSendRequestHeaders(object sender, EventArgs e)
        {
            HttpApplication app = sender as HttpApplication;
            if (app != null &&
                app.Context != null)
            {
                app.Context.Response.Headers.Remove("Server");
            }
        }

触发错误的方法:

    [HttpPost]
    [Route("UploadImageJSON")]
    public IHttpActionResult UploadImageJSON(HttpRequestMessage request)
    {
        var httpRequest = HttpContext.Current.Request;
        // Check if files are available
        if (httpRequest.Files.Count != 1) return BadRequest();
        var postedFile = httpRequest.Files[0];
        var contentType = postedFile.ContentType;
        if (!contentType.Contains("image"))
        {
            return StatusCode(HttpStatusCode.NotAcceptable);
        }


        var keyUploadFiles = Constants.UrlS3Amazon +
                             S3.UploadToS3WithStream(postedFile.InputStream, contentType);

        return Json(JsonConvert.SerializeObject(keyUploadFiles));

    }

EDIT:更多信息…我有我的Web应用程序托管在Elastic BeanStalk与负载均衡器,负载均衡器已经安装了SSL证书,负载均衡器和EC2实例之间的连接在端口80。也许会有帮助。

Elmah日志:

包含。HttpException (0x80004005):服务器在发送HTTP报头后无法添加报头。在System.Web.HttpHeaderCollection。SetHeader(字符串名称,字符串值,布尔替换)在Microsoft.Owin.Host.SystemWeb.CallHeaders.AspNetResponseHeaders。设置(字符串键,字符串[]值)在Microsoft.Owin.Infrastructure.OwinHelpers。AppendHeaderUnmodified(字典2 headers, String key, String[] values) at Microsoft.Owin.ResponseCookieCollection.Append(String key, String value, CookieOptions options) at Microsoft.Owin.Security.Cookies.CookieAuthenticationHandler.<ApplyResponseGrantAsync>d__b.MoveNext() --- End of stack trace from previous location where exception was thrown --- at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at Microsoft.Owin.Security.Infrastructure.AuthenticationHandler.<ApplyResponseCoreAsync>d__8.MoveNext() --- End of stack trace from previous location where exception was thrown --- at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at Microsoft.Owin.Security.Infrastructure.AuthenticationHandler.<TeardownAsync>d__5.MoveNext() --- End of stack trace from previous location where exception was thrown --- at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at Microsoft.Owin.Security.Infrastructure.AuthenticationMiddleware .d__0. moveext ())——抛出异常的前一个位置的堆栈跟踪结束——在System.Runtime.CompilerServices.TaskAwaiter。ThrowForNonSuccess(工作任务)在System.Runtime.CompilerServices.TaskAwaiter。HandleNonSuccessAndDebuggerNotification(工作任务)在Microsoft.AspNet.Identity.Owin.IdentityFactoryMiddleware 2.<Invoke>d__0.MoveNext() --- End of stack trace from previous location where exception was thrown --- at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at Microsoft.AspNet.Identity.Owin.IdentityFactoryMiddleware .d__0. movenext ()——抛出异常的前一个位置的堆栈跟踪结束——在System.Runtime.CompilerServices.TaskAwaiter。ThrowForNonSuccess(工作任务)在System.Runtime.CompilerServices.TaskAwaiter。HandleNonSuccessAndDebuggerNotification(工作任务)2. Microsoft.AspNet.Identity.Owin.IdentityFactoryMiddleware"d__0.movenext ()——抛出异常的前一个位置的堆栈跟踪结束——在System.Runtime.CompilerServices.TaskAwaiter。ThrowForNonSuccess(工作任务)在System.Runtime.CompilerServices.TaskAwaiter。HandleNonSuccessAndDebuggerNotification(工作任务)在Microsoft.Owin.Mapping.MapMiddleware.d__0.MoveNext ()——抛出异常的前一个位置的堆栈跟踪结束——在System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw ()在Microsoft.Owin.Host.SystemWeb.IntegratedPipeline.IntegratedPipelineContext。EndFinalWork (IAsyncResult ar)在System.Web.HttpApplication.AsyncEventExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute ()在System.Web.HttpApplication。ExecuteStep(IExecutionStep step, Boolean&completedSynchronously)

谢谢! !

上传文件后,服务器不能附加HTTP头

您是否尝试过删除app.Context.Response.Headers.Remove("Server");我认为这是问题所在?