zoukankan      html  css  js  c++  java
  • 解决远程主机关闭了连接错误(正在中止线程)

    Response.Buffer = false;
    Response.AddHeader("Connection", "Keep-Alive");
    Response.ContentType = "application/octet-stream";
    Response.HeaderEncoding = System.Text.Encoding.UTF8;
    Response.AddHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(FileName,System.Text.Encoding.UTF8).Replace("+", "%20"));
    Response.AddHeader("Content-Length", r.Length.ToString());
    byte[] buffer;
    buffer = r.ToArray();
    Response.BinaryWrite(buffer);
    r.Close(); //关闭下载文件 
    Response.Flush();
    Response.End(); //结束文件下载

    程序在运行时会报出“正在中止线程”的错误,可以确定的是引起错误的代码就是Response.End();

    Response.Close()的解释是关闭到客户端的连接。对Response.End()的解释是停止该页的执行,并引发Application_EndRequest。

    也就说用Response.End()程序就直接停止后面的工作了转而触发Application_EndRequest,那么当程序在后面还有代码需要运行时,程序就会抛出ThreadAbortException的异常。

    Response.End()是终止服务器输出,但有可能会throw出线程异常,不处理即可,ms官方对该问题进行了解释,如果你不希望throw出线程错误,则可以采用相关解决方法。

    CompleteRequest()是通知结束请求。

    若要解决此问题,请使用下列方法之一:

    • 对于 Response.End,调用 ApplicationInstance.CompleteRequest 方法而不调用 Response.End,以便跳过 Application_EndRequest 事件的代码执行。
    • 对于 Response.Redirect,使用重载 Response.Redirect(String url, bool endResponse),对 endResponse 参数它传递 false以取消对 Response.End 的内部调用。例如:
        Response.Redirect ("nextpage.aspx", false);
      如果使用这种解决方法,Response.Redirect 后面的代码将得到执行。
    • 对于 Server.Transfer,请改用 Server.Execute 方法。
  • 相关阅读:
    Libvirt错误总结
    linux学习
    HMC 命令行登陆设置
    AIX扩VG和扩文件系统
    Bootstrap CSS2
    Bootstrap CSS
    JQuery的实例集合
    JQuery的noConflict()方法
    android的intent实现页面的跳转
    android的activity
  • 原文地址:https://www.cnblogs.com/shy1766IT/p/5370541.html
Copyright © 2011-2022 走看看