简体   繁体   English

为什么在ASP.Net中的StatusDescription中添加换行符会关闭连接?

[英]Why does adding a newline to StatusDescription in ASP.Net close the connection?

When I return a StatusDescription with a newline using the HttpStatusCodeResult from ASP.Net MVC 3.0, the connection to my client is forcibly closed. 当我从ASP.Net MVC 3.0使用HttpStatusCodeResult返回带有换行符的StatusDescription时,与客户端的连接被强制关闭。 App is hosted in IIS 7.0. 应用程序托管在IIS 7.0中。

Example controller: 控制器示例:

 public class FooController : Controller
 {
    public ActionResult MyAction()
    {
       return new HttpStatusCodeResult((int)HttpStatusCode.BadRequest, "Foo \n Bar");
    }
 }

Example client: 客户端示例:

using (WebClient client = new WebClient())
{
   client.DownloadString("http://localhost/app/Foo/MyAction");
}

Thrown Exception: 抛出异常:

System.Net.WebException: The underlying connection was closed: An unexpected error occurred on a receive. 
  System.IO.IOException: Unable to read data from the transport connection: An existing connection was forcibly closed by the remote host.
    System.Net.Sockets.SocketException: An existing connection was forcibly closed by the remote host

The behavior is consistent when using curl (curl 7.25.0 (i386-pc-win32) libcurl/7.25.0 zlib/1.2.6) 使用curl时,行为是一致的(curl 7.25.0(i386-pc-win32)libcurl / 7.25.0 zlib / 1.2.6)

curl http://localhost/app/Foo/MyAction

curl: (56) Recv failure: Connection was reset curl:(56)接收失败:连接已重置

Edit 编辑

I ended up using this custom ActionResult to get the desired results. 我最终使用此自定义ActionResult来获得所需的结果。

public class BadRequestResult : ActionResult
{
   private const int BadRequestCode = (int)HttpStatusCode.BadRequest;
   private int count = 0;

   public BadRequestResult(string errors)
      : this(errors, "")
   {
   }

   public BadRequestResult(string format, params object[] args)
   {
      if (String.IsNullOrEmpty(format))
      {
         throw new ArgumentException("format");
      }

      Errors = String.Format(format, args);

      count = Errors.Split(new string[] { Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries).Length;
   }

   public string Errors { get; private set; }

   public override void ExecuteResult(ControllerContext context)
   {
      if (context == null)
      {
         throw new ArgumentNullException("context");
      }

      HttpResponseBase response = context.HttpContext.Response;
      response.TrySkipIisCustomErrors = true;
      response.StatusCode = BadRequestCode;
      response.StatusDescription = String.Format("Bad Request {0} Error(s)", count);
      response.Write(Errors);
      response.End();
   }
}

You can't have a linebreak in the middle of an HTTP header. HTTP标头中间不能有换行符。

The HTTP protocol specifies the end of a header is a line break. HTTP协议指定标头的结尾是换行符。

Since the line break is in the middle of a header, the header is not a valid header and you are getting this error. 由于换行符位于标题的中间,因此标题不是有效的标题,并且您会收到此错误。

Fix: Don't put a line break in the middle of an HTTP header. 修复:不要在HTTP标头的中间放置换行符。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM