【发布时间】:2018-09-05 01:51:27
【问题描述】:
我为演示创建了两个 aspx 页面,
page1 - WebForm1.aspx
<asp:TextBox ID="txtTest" runat="server" Width="100px"></asp:TextBox>
<asp:Button ID="btnClick" runat="server" Text="test" Width="100px" OnClick="btnClick_Click"/>
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
if (Request.QueryString["text"] == null || string.IsNullOrEmpty(Request.QueryString["text"].ToString()))
txtTest.Text = "ö";
else
txtTest.Text = Request.QueryString["text"].ToString();
}
}
public void btnClick_Click(object sender, EventArgs e)
{
HttpResponse response = HttpContext.Current.Response;
response.Write(string.Format("<script>window.location = '{0}';</script>", HttpUtility.JavaScriptStringEncode("WebForm2.aspx?text=" + HttpUtility.UrlEncode(txtTest.Text))));
response.End();
}
page2 - WebForm2.aspx
<asp:TextBox ID="txtResult" runat="server" Width="200px"></asp:TextBox>
<asp:Button ID="btnBack" runat="server" Text="back" Width="50px" OnClick="btnBack_Click"/>
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
if (Request.QueryString["text"] == null || string.IsNullOrEmpty(Request.QueryString["text"].ToString()))
txtResult.Text = "empty";
else
txtResult.Text = Request.QueryString["text"].ToString();
}
}
public void btnBack_Click(object sender, EventArgs e)
{
HttpResponse response = HttpContext.Current.Response;
response.Write(string.Format("<script>window.location = '{0}';</script>", HttpUtility.JavaScriptStringEncode("WebForm1.aspx?text=" + HttpUtility.UrlEncode(txtResult.Text))));
response.End();
}
然后我使用 Fiddler 跟踪网络,点击测试按钮,然后点击返回按钮。
# Result Protocol Host URL Body Caching Content-Type Process Comments Custom
6 200 HTTP localhost:56484 /WebForm2.aspx?text=%c3%b6 835 private text/html; charset=utf-8 iexplore:12316
8 200 HTTP localhost:56484 /WebForm2.aspx?text=%u00f6 175 private text/html; charset=utf-8 iexplore:12316
9 200 HTTP localhost:56484 /WebForm1.aspx?text=%c3%b6 830 private text/html; charset=utf-8 iexplore:12316
10 200 HTTP localhost:56484 /WebForm1.aspx?text=%u00f6 175 private text/html; charset=utf-8 iexplore:12316
11 200 HTTP localhost:56484 /WebForm2.aspx?text=%c3%b6 834 private text/html; charset=utf-8 iexplore:12316
我们可以看到 URL 正文有奇怪的编码,为什么会生成 %u00f6?可以回到 %c3%b6 吗?
当我们单击返回按钮返回第 1 页时,它的引荐来源网址丢失了。实际上我认为奇怪的编码导致了这个问题,因为当我使用 F12 开发工具更改操作(从“%u00f6”到“%c3%b6”),然后单击返回按钮时,生成了引用。
click here to see the screenshot
如果您能给出答案,非常感谢。
【问题讨论】:
标签: c# encoding request.querystring