【问题标题】:How can I catch a pound sign(#) from URL with multiple values in ASHX files如何从 ASHX 文件中具有多个值的 URL 中捕获井号 (#)
【发布时间】:2016-10-26 07:22:12
【问题描述】:

这是我的ASHX 文件,我使用httpcontext 从请求URL 捕获多个参数,它工作正常,但是当我通过URL 在文本参数中包含哈希(#)值时。它不采用另一个参数(文本参数旁边)的 FLOW 的值。

所以它适用于:

http://localhost:10326/ussd.ashx?user=MSL&pass=MSL663055&tid=65506&msisdn=8801520101525&text=***3333**&flow=begin&momt=mo

它不适用于:

http://localhost:10326/ussd.ashx?user=MSL&pass=MSL663055&tid=65506&msisdn=8801520101525&text=***3333#**&flow=begin&momt=mo

我的 ASHX 文件:

public void ProcessRequest(HttpContext context)
{
    HttpRequest httpRequest = context.Request;
    string user = httpRequest.QueryString["user"].ToString();
    string pass = httpRequest.QueryString["pass"].ToString();
    string tid = httpRequest.QueryString["tid"].ToString();
    string msisdn = httpRequest.QueryString["msisdn"].ToString();
    string text = httpRequest.QueryString["text"].ToString();
    flow = httpRequest.QueryString["flow"].ToString();
    HttpContext.Current.Session["user"] = user;
    HttpContext.Current.Session["pass"] = pass;
    HttpContext.Current.Session["tid"] = tid;
    HttpContext.Current.Session["msisdn"] = msisdn;
    HttpContext.Current.Session["text"] = text;
    HttpContext.Current.Session["flow"] = flow;

【问题讨论】:

    标签: asp.net httprequest webrequest httpcontext ashx


    【解决方案1】:

    在将参数值添加到 URL 之前,您需要对参数值进行 URI 编码。这样,服务器就不会被不安全的字符弄糊涂,例如“#”,当作为 URL 的一部分包含时,它有自己的含义。见RFC 3986 Section 2

    请参阅Encode URL in JavaScript 作为示例,了解如何使用 JavaScript 对发送的数据进行编码。无论在 URL 中发送什么数据都需要进行编码。一旦请求到达服务器,您就无能为力了。在不知道您的 URL 是如何创建的情况下,我无法提供更多信息。

    简而言之:问题在于您的客户端代码而不是您的 ASHX 文件。

    【讨论】:

    • 正确,url 编码函数为 .Net : Httputility.UrlEncode() , Javascript : encodeURI()encodeURIComponent()
    • 第 3 节甚至有一个很好的 ASCII graph 显示 URI 部分,包括片段
    猜你喜欢
    • 2015-10-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-07-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-02-04
    相关资源
    最近更新 更多