【问题标题】:Json HTTP Module stream issueJson HTTP 模块流问题
【发布时间】:2011-01-26 16:32:55
【问题描述】:

我有一个 HTTP 模块,用于清理我的 Web 服务返回的 JSON(有关此示例,请参阅 http://www.codeproject.com/KB/webservices/ASPNET_JSONP.aspx?msg=3400287#xx3400287xx。)基本上它与从 javascript 调用跨域 JSON Web 服务有关。

有这个 JsonHttpModule 使用 JsonResponseFilter Stream 类来写出 JSON,并且重载的 Write 方法应该将回调函数的名称包装在 JSON 周围,否则 JSON 会出错,因为需要标签。但是,如果 JSON 真的很长,Stream 类中的 Write 方法会被多次调用,导致回调函数错误地插入到 JSON 中途。 Stream 类中是否有办法在最后将回调函数包装在流周围,或者指定它将所有 JSON 写入 1 Write 方法而不是块中??

这里是调用 JsonHttpModule 中的 JsonResponseFilter 的地方:

public void OnReleaseRequestState(object sender, EventArgs e)
        {
            HttpApplication app = (HttpApplication)sender;

            if (!_Apply(app.Context.Request)) return;

            // apply response filter to conform to JSONP
            app.Context.Response.Filter =
                new JsonResponseFilter(app.Context.Response.Filter, app.Context);
        }

这是 JsonResponseFilter Stream 类中被多次调用的 Write 方法:

public override void Write(byte[] buffer, int offset, int count)
        {
            var b1 = Encoding.UTF8.GetBytes(_context.Request.Params["callback"] + "(");
            _responseStream.Write(b1, 0, b1.Length);

            _responseStream.Write(buffer, offset, count);

            var b2 = Encoding.UTF8.GetBytes(");");
            _responseStream.Write(b2, 0, b2.Length);
        }

感谢您的帮助! 贾斯汀

【问题讨论】:

    标签: asp.net ajax json


    【解决方案1】:

    另一种解决方案是在 Flush 方法中编写 ResponseStream。就像在this 示例中一样。

    我修改了 JsonHttpModules Flush 方法,并使用 StringBuilder 将流存储在像 Justin 一样的 Write 方法中。

        /// <summary>
        /// Override flush by writing out the cached stream data
        /// </summary>
        public override void Flush()
        {
    
            if (_sb.Length > 0)
            {
                string message = _context.Request.Params["callback"] + "(" + _sb.ToString() + ");";
                byte[] buffer = System.Text.Encoding.UTF8.GetBytes(message);
                _responseStream.Write(buffer, 0, buffer.Length);
            }
    
            // default flush behavior
            _responseStream.Flush();
        }
    
        public override void Write(byte[] buffer, int offset, int count)
        {
            string json = System.Text.Encoding.UTF8.GetString(buffer, offset, count);
            _sb.Append(json);
        }
    

    这样您就不必尝试猜测传入流的结尾。

    【讨论】:

      【解决方案2】:

      它多次触发该方法的原因是它会缓冲内容,然后将其发送到输出流。这是一个展示如何创建 ViewState mover HttpModule 的示例。你可以从实现中得到一些想法。向下滚动到底部并查看结果。

      http://www.highoncoding.com/Articles/464_Filtering_Responses_Using_ASP_NET_Response_Filters.aspx

      【讨论】:

      • 谢谢,那篇文章给了我我需要的东西! public override void Write(byte[] buffer, int offset, int count) { string json = System.Text.Encoding.UTF8.GetString(buffer, offset, count); _sb.Append(json); var endOfFile = new Regex("]"); if (endOfFile.IsMatch(json)) { string message = _context.Request.Params["callback"] + "(" + _sb.ToString() + ");";缓冲区 = System.Text.Encoding.UTF8.GetBytes(message); _responseStream.Write(buffer, 0, buffer.Length); } }
      猜你喜欢
      • 2018-08-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-10-13
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多