HTTPS 重定向导致错误“服务器无法在发送 HTTP 标头后附加标头”
我需要检查我们的访问者是否使用 HTTPS。在 BasePage 中,我检查请求是否通过 HTTPS 发出。如果不是,我会使用 HTTPS 重定向回来。但是,当有人访问该网站并使用此功能时,我收到错误:
System.Web.HttpException:服务器 无法在 HTTP 之后附加标头 标头已发送。在 System.Web.HttpResponse.AppendHeader(字符串 名称,字符串值)位于 System.Web.HttpResponse.AddHeader(字符串 名称,字符串值)位于 Premier.Payment.Website.Generic.BasePage..ctor()
这是我开始使用的代码:
// If page not currently SSL
if (HttpContext.Current.Request.ServerVariables["HTTPS"].Equals("off"))
{
// If SSL is required
if (GetConfigSetting("SSLRequired").ToUpper().Equals("TRUE"))
{
string redi = "https://" +
HttpContext.Current.Request.ServerVariables["SERVER_NAME"].ToString() +
HttpContext.Current.Request.ServerVariables["SCRIPT_NAME"].ToString() +
"?" + HttpContext.Current.Request.ServerVariables["QUERY_STRING"].ToString();
HttpContext.Current.Response.Redirect(redi.ToString());
}
}
我还尝试在其上方添加此代码(我在另一个网站中使用了一点来解决类似问题):
// Wait until page is copletely loaded before sending anything since we re-build
HttpContext.Current.Response.BufferOutput = true;
我在 .NET 中使用 c# IIS 6 上的 3.5。
I need to check that our visitors are using HTTPS. In BasePage I check if the request is coming via HTTPS. If it's not, I redirect back with HTTPS. However, when someone comes to the site and this function is used, I get the error:
System.Web.HttpException: Server
cannot append header after HTTP
headers have been sent. at
System.Web.HttpResponse.AppendHeader(String
name, String value) at
System.Web.HttpResponse.AddHeader(String
name, String value) at
Premier.Payment.Website.Generic.BasePage..ctor()
Here is the code I started with:
// If page not currently SSL
if (HttpContext.Current.Request.ServerVariables["HTTPS"].Equals("off"))
{
// If SSL is required
if (GetConfigSetting("SSLRequired").ToUpper().Equals("TRUE"))
{
string redi = "https://" +
HttpContext.Current.Request.ServerVariables["SERVER_NAME"].ToString() +
HttpContext.Current.Request.ServerVariables["SCRIPT_NAME"].ToString() +
"?" + HttpContext.Current.Request.ServerVariables["QUERY_STRING"].ToString();
HttpContext.Current.Response.Redirect(redi.ToString());
}
}
I also tried adding this above it (a bit I used in another site for a similar problem):
// Wait until page is copletely loaded before sending anything since we re-build
HttpContext.Current.Response.BufferOutput = true;
I am using c# in .NET 3.5 on IIS 6.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
乍得,
您是否尝试在重定向时结束输出?您可以将第二个参数设置为 true,以告诉输出在发出重定向标头时停止。或者,如果您正在缓冲输出,那么可能需要在执行重定向之前清除缓冲区,以便标头不会与重定向标头一起发送出去。
布莱恩
Chad,
Did you try ending the output when you redirect? There is a second parameter that you'd set to true to tell the output to stop when the redirect header is issued. Or, if you are buffering the output then maybe you need to clear the buffer before doing the redirect so the headers are not sent out along with the redirect header.
Brian
此错误通常意味着在启动重定向之前已将某些内容写入响应流。因此,您应该确保 https 测试是在页面加载函数的相当高的位置完成的。
This error usually means that something has bee written to the response stream before a redirection is initiated. So you should make sure that the test for https is done fairly high up in the page load function.