如何使用自定义 IHttpModule 和 HttpRequest 过滤器修改 POST 请求? [英] How can I modify a POST request using a custom IHttpModule and an HttpRequest filter?

查看:29
本文介绍了如何使用自定义 IHttpModule 和 HttpRequest 过滤器修改 POST 请求?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

概述

我希望能够修改对 3rd 方 Web 服务(ArcGIS Server)的请求参数和内容.这将用于创建存在于任何客户端应用程序和服务器应用程序之间的安全层.

I want to be able to modify request parameters and content to 3rd party web services (ArcGIS Server). This will be used to create a security layer that exists between any client application and the server application.

我认为我已经找到了解决方案,但我目前在实施过程中遇到了一些困难.

I think that I have found a solution but I am current having some difficulties in the implementation.

潜在解决方案:使用自定义请求过滤器修改请求

对于解决方案,我基于 MSDN 上显示的示例.我已经增强"了代码,以便我可以使用正则表达式搜索和替换必要的内容.这包括:

For the solution I implemented a custom request filter loosely based on the sample shown on MSDN. I have 'enhanced' the code so that I can search and replace the necessary content using regular expressions. This involves:

  1. 将内容(存储在字节数组中)转换为字符串.
  2. 搜索字符串并执行任何必要的修改.
  3. 将修改后的字符串转换为字节数组并将其写入缓冲区.

示例如下:

public override int Read(byte[] buffer, int offset, int count)
{
    int bytesRead = _stream.Read(buffer, offset, count);

    string orgContent = Encoding.UTF8.GetString(buffer, offset, bytesRead);
    string orgContentDecoded = HttpUtility.UrlDecode(orgContent);
    
    string layersPattern = @"&layers=(show|hide|include|exclude):([0-9]+,?)+";
    Regex layersRegex = new Regex(layersPattern, RegexOptions.IgnoreCase);
    
    string[] permittedLayers = new string[] { "0" , "1" };
    string replacementLayers = "&layers=show:" + String.Join(",", permittedLayers);
    string newContentDecoded = layersRegex.Replace(orgContentDecoded, replacementLayers);
    
    string newContent =  newContentDecoded.Replace(",", "%2C").Replace(":", "%3A");

    byte[] newBuffer = Encoding.UTF8.GetBytes(newContent);
    int newByteCountLength = Encoding.UTF8.GetByteCount(newContent);
    
    Encoding.UTF8.GetBytes(newContent, 0, Encoding.UTF8.GetByteCount(newContent), buffer, 0);
    
    return bytesRead;
}

只要修改后的内容长度与原始内容长度没有不同,这似乎效果很好.例如,如果我用 2 替换 1,一切正常.但是,如果我将 1 替换为 10(从而将消息大小增加 1),那么我会收到来自 ArcGIS Server 的错误,指出该格式不受支持.

This seems to work well so long as the modified content length is not different than the original content length. For instance, if I replace a 1 with a 2 everything works. However, if I replace a 1 with a 10 (thereby increasing the message size by 1) then I receive an error from ArcGIS Server that the format is unsupported.

这引起了我的注意:

  1. 当前的实现不处理分块请求.也就是说,如果请求 sie 足够大,则可能会为单个请求多次调用 Read.在这种情况下应该如何处理分块?
  2. 错误消息的根本原因是什么?问题是否与内容长度与流长度不同?如何正确修改内容,使其长度不再成为问题?

有什么想法吗?

推荐答案

这个问题第二部分的答案是返回修改后的内容大小,而不是原始流的大小.看!

The answer to part two of this question is to return the modified content size, not the size of the original stream. Behold!

// return bytesRead;
return newByteCountLength;

这篇关于如何使用自定义 IHttpModule 和 HttpRequest 过滤器修改 POST 请求?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆