为什么在ASP.Net中向StatusDescription添加换行符会关闭连接?
发布时间:2020-05-28 13:01:18 所属栏目:asp.Net 来源:互联网
导读:当我使用来自ASP.Net MVC 3.0的HttpStatusCodeResult返回带有换行符的StatusDescription时,强制关闭与我的客户端的连接.应用程序托管在IIS 7.0中. 示例控制器: public class FooController : Controller { public ActionResult MyAction() { re
|
当我使用来自ASP.Net MVC 3.0的HttpStatusCodeResult返回带有换行符的StatusDescription时,强制关闭与我的客户端的连接.应用程序托管在IIS 7.0中. 示例控制器: public class FooController : Controller
{
public ActionResult MyAction()
{
return new HttpStatusCodeResult((int)HttpStatusCode.BadRequest,"Foo n Bar");
}
}
示例客户端: using (WebClient client = new WebClient())
{
client.DownloadString("http://localhost/app/Foo/MyAction");
}
抛出异常: 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
使用curl时的行为是一致的(curl 7.25.0(i386-pc-win32)libcurl / 7.25.0 zlib / 1.2.6)
卷曲:(56)Recv失败:重置连接 编辑 我最终使用这个自定义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();
}
}
解决方法您不能在HTTP标头的中间使用换行符.HTTP协议指定标头的结尾是换行符. 由于换行符位于标题的中间,因此标题不是有效的标题,您将收到此错误. 修复:不要在HTTP标头的中间放置换行符. (编辑:安卓应用网) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
相关内容
- asp.net – 如何通过LINQ获得第一级的孩子
- asp.net – .Net更改元素ID
- asp.net-mvc – 禁用为特定提交按钮启用不显眼的验证
- asp.net-mvc – 最初在使用AuthorizeAttribute时设置Return
- asp.net-mvc – ASP.Net MVC 5带范围的Google身份验证
- asp.net – 如何将流excel文件转换为数据表C#?
- asp.net-mvc-3 – ASP.NET MVC3项目并不总是发布所有的视图
- asp.net-mvc-3 – 如何让User.Identity在控制器外工作
- asp.net – 将样式应用于CheckBoxList中的ListItems
- asp.net – 在Visual Studio 2012中创建控制器时出错
推荐文章
站长推荐
热点阅读
