C# 无法在发送 HTTP 标头之后进行重定向

在调试中发现错误如下:

     Response.Redirect引起的“无法在发送HTTP标头之后进行重定向” 跳转失败

 

解决方案如下:

 使用js方法来跳转地址

1 const string url="<script>window.location.href='/login/index'</script>;

2 System.Web.HttpContext.Current.Response.Write(url);

3 System.Web.HttpContext.Current.Response.End();

 

展开阅读全文

什么叫“无法发送 HTTP 标头之后进行重定向。”?

09-14

以下是我在网上抄的一段文件下载的代码,了解的朋友就不用看了,直接看最下面。rnrnrn[color=#0000FF]rn另外顺便帮我回答下面这个问题的,独得10分:rn下面的代码中,int sleep = (int)Math.Floor(1000 * pack / 256000) + 1; 这行是什么意思?主要我想知道这个 256000 是什么含义呢?[/color]rnrn[code=C#]rnrnpublic static bool ResponseFile(HttpRequest request, HttpResponse response, string fileName, string fullPath)rn rn response.Clear();rnrn tryrn rn FileStream myFile = new FileStream(fullPath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);rn BinaryReader br = new BinaryReader(myFile);rnrn tryrn rn response.AddHeader("Accept-Ranges", "bytes");rn response.Buffer = false;rn long fileLength = myFile.Length;rn long startBytes = 0;rnrn double pack = 10240; //10K bytesrn //int sleep = 200; //每秒5次 即5*10K bytes每秒rn int sleep = (int)Math.Floor(1000 * pack / 256000) + 1;rn if (request.Headers["Range"] != null)rn rn response.StatusCode = 206;rn string[] range = request.Headers["Range"].Split(new char[] '=', '-' );rn startBytes = Convert.ToInt64(range[1]);rn rn response.AddHeader("Content-Length", (fileLength - startBytes).ToString());rn if (startBytes != 0)rn rn //Response.AddHeader("Content-Range", string.Format(" bytes 0-1/2", startBytes, fileLength-1, fileLength));rn rnrn response.AddHeader("Connection", "Keep-Alive");rn response.ContentType = "application/octet-stream";rn response.AddHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(fileName, System.Text.Encoding.UTF8));rnrn br.BaseStream.Seek(startBytes, SeekOrigin.Begin);rn int maxCount = (int)Math.Floor((fileLength - startBytes) / pack) + 1;rnrn for (int i = 0; i < maxCount; i++)rn rn if (response.IsClientConnected)rn rn response.BinaryWrite(br.ReadBytes(int.Parse(pack.ToString())));rn Thread.Sleep(sleep);rn rn elsern rn i = maxCount;rn rn rn rn catchrn rn return false;rn rn finallyrn rn br.Close();rnrn myFile.Close();rn rn rn catchrn rn return false;rn rnrn return true;rn rnrn[/code]rnrn我对该方法传递了 Page.Response 和 Page.Request 两个参数。rnrn我在执行完这个方法后,想让页面进行重定向,于是就写了个 Response.Redirect("...");,可是却发生了一个错误:无法在发送 HTTP 标头之后进行重定向。rnrn请问是何原因?谢谢。 论坛

服务器无法发送 HTTP 标头之后追加标头 3005错误

06-03

服务器无法在发送 HTTP 标头之后追加标头。 具体代码如下:rn(而且错误的全部是.jpg .js .css这些,希望高手帮忙解决)rnrn分不多了,请大家帮忙(服务器我上个星期刚重装过,网站目录一直没动过)rn事件代码: 3005 rn事件消息: 发生了未处理的异常。 rn事件时间: 2011-6-2 13:07:16 rn事件时间(UTC): 2011-6-2 5:07:16 rn事件 ID: 10889952d4bc47b8a6cf8d12322f6607 rn事件序列: 282336 rn事件匹配项: 71708 rn事件详细信息代码: 0 rn rn应用程序信息: rn 应用程序域: /LM/W3SVC/71766476/Root-1-129514621322343750 rn 信任级别: Full rn 应用程序虚拟路径: / rn 应用程序路径: D:\***\website\ rn 计算机名: Z-E41871A772FD4 rn rn进程信息: rn 进程 ID: 1848 rn 进程名: w3wp.exe rn 帐户名: NT AUTHORITY\NETWORK SERVICE rn rn异常信息: rn 异常类型: HttpException rn 异常消息: 服务器无法在发送 HTTP 标头之后追加标头。 rn rn请求信息: rn 请求 URL: http://china.*.com/Ads.css rn 请求路径: /GoogleAds.css rn 用户主机地址: 59.72.84.78 rn 用户: rn 是否已经过身份验证: False rn 身份验证类型: rn 线程帐户名: NT AUTHORITY\NETWORK SERVICE rn rn线程信息: rn 线程 ID: 1 rn 线程帐户名: NT AUTHORITY\NETWORK SERVICE rn 是否正在模拟: False rn 堆栈跟踪: 在 System.Web.HttpResponse.AppendHeader(String name, String value)rn 在 DC.Web.HttpCompress.HttpModule.context_PostReleaseRequestState(Object sender, EventArgs e)rn 在 System.Web.HttpApplication.SyncEventExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute()rn 在 System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously)rn rn rn自定义事件详细信息: rnrn有关更多信息,请参阅在 http://go.microsoft.com/fwlink/events.asp 的帮助和支持中心。rnrn 论坛

web下载有时候会出现 服务器无法发送 HTTP 标头之后追加标头

10-26

[code=C#]rn #region Web下载文件rn /// rn /// Web下载文件rn /// rn /// 要下载文件的物理路径rn /// 文件类型,如 excel是 application/vnd.ms-excel word 是application/mswordrn public static void DownloadFile(string physicalPath, string contentType)rn rnrn HttpContext.Current.Response.Clear();rnrn // Buffer to read 10K bytes in chunk:rn byte[] buffer = new Byte[10000];rnrn // Length of the file:rn int length;rnrn // Total bytes to read:rn long dataToRead;rnrn string filename = Path.GetFileName(physicalPath);rn using (Stream iStream = new FileStream(physicalPath, FileMode.Open, FileAccess.Read, FileShare.Read))rn rn // Total bytes to read:rn dataToRead = iStream.Length;rn HttpContext.Current.Response.ContentEncoding = Encoding.UTF8;rn HttpContext.Current.Response.ContentType = contentType;rn HttpContext.Current.Response.AddHeader("Content-Length", iStream.Length.ToString());rn HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment; filename=" + HttpUtility.UrlEncode(filename, Encoding.UTF8));rnrn // Read the bytes.rn while (dataToRead > 0)rn rn // Verify that the client is connected.rn if (HttpContext.Current.Response.IsClientConnected)rn rn // Read the data in buffer.rn length = iStream.Read(buffer, 0, 10000);rnrn // Write the data to the current output stream.rn HttpContext.Current.Response.OutputStream.Write(buffer, 0, length);rnrn // Flush the data to the HTML output.rn //HttpContext.Current.Response.Flush();// 测试服务器无法在发送 HTTP 标头之后追加标头rn buffer = new Byte[10000];rn dataToRead = dataToRead - length;rn rn elsern rn //prevent infinite loop if user disconnectsrn dataToRead = -1;rn rn rnrn rnrn rn #endregionrn[/code] 论坛

没有更多推荐了,返回首页