HTTP报头上传文件后,服​​务器无法追加标头

时间:2015-07-08 08:30:08

标签: c# asp.net-mvc

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

  

在发送HTTP标头后,服务器无法追加标头。

只是将图像上传到S3方法(Web Api控制器)。

Global.asax中的presendrequestheaders

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


    }

编辑:更多信息...我的Elastic BeanStalk中托管的Web App带有负载均衡器,负载均衡器已安装SSL证书,负载均衡器与EC2之间的连接实例在端口80.也许它可能会有所帮助。

Elmah日志:

  

System.Web.HttpException(0x80004005):在发送HTTP标头后,服务器无法附加标头。      在System.Web.HttpHeaderCollection.SetHeader(String name,String value,Boolean replace)      在Microsoft.Owin.Host.SystemWeb.CallHeaders.AspNetResponseHeaders.Set(String key,String [] values)      在Microsoft.Owin.Infrastructure.OwinHelpers.AppendHeaderUnmodified(IDictionary 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 1.d__0.MoveNext()   ---从抛出异常的先前位置开始的堆栈跟踪结束---      在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 2.d__0.MoveNext()   ---从抛出异常的先前位置开始的堆栈跟踪结束---      在System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(任务任务)      在System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(任务任务)      在Microsoft.AspNet.Identity.Owin.IdentityFactoryMiddleware`2.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&amp; completedSynchronously)

谢谢!

1 个答案:

答案 0 :(得分:1)

你试过删除app.Context.Response.Headers.Remove(&#34; Server&#34;);我认为这是问题吗?