本文介绍了是否有可能修改的HttpRequest POST在一个IIS HTTP模块的内容?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要修改某些HTTPRequests的在IIS中的内容(SSAS连接字符串)。基本上,我需要添加到包含在请求中的SOAP的元素。



我的做法至今一直到过滤器添加到HttpRequest中,并执行在改变过滤器的Read方法。至于我可以告诉大家,虽然从来没有被执行读取。



我的Request.Filter的理解是,它会从当IIS处理请求读取,因此IIS应该看到我的修改要求。



是我想要做的其实可以使用一个HttpModule,是我筛选的方法正确吗?



如果这样,这将导致读取不被打到



下面是我的代码的简化版本:

  
公共类CustomHttpModule:IHttpModule的{
私人HttpApplication的应用;
公共字符串模块名{
{返回CustomHttpModule }
}
公共无效初始化(HttpApplication的上下文){
=应用环境;
context.PreRequestHandlerExecute + =新的EventHandler(context_PreRequestHandlerExecute);
}
无效context_PreRequestHandlerExecute(对象发件人,EventArgs五){
VAR请求= app.Context.Request;
request.Filter =新CustomHttpFilter(request.Filter);
}
}

公共类CustomHttpFilter:流{
专用流的OutputStream;
公共CustomHttpFilter(流OutputFilter输出){
的OutputStream = OutputFilter输出;
}
公共覆盖INT读(字节[]缓冲区,诠释抵消,诠释计数){
//读取并进行必要的更改
}
}


解决方案

我不相信这是可以修改的要求与HTTP模块,但它是可以修改的响应。在的HttpRequest 对象大多是只读的,所以它是通常是不可修改在任何情况下,不只是从一个模块。



如果你是真的绝望了,你可以尝试使用反射来获取更多的HTTP请求对象的访问。访问,修改,或者调用非公共成员,但是,你的过程将需要的完全信任的权限, 这是在网络环境非常危险


I need to modify the content of certain HttpRequests (SSAS connection strings) in IIS. Basically, I need to add an element to the SOAP contained in the request.

My approach so far has been to add a Filter to the HttpRequest, and perform the change in the filter's Read method. As far as I can tell, though, Read is never being executed.

My understanding of the Request.Filter is that it gets read from when IIS processes the request, so IIS should see my modified Request.

Is what I'm trying to do actually possible using an HttpModule and is my Filter approach correct?

If so, what would cause Read to not be hit?

Here's a simplified version of my code:


public class CustomHttpModule : IHttpModule {
    private HttpApplication app;
    public string ModuleName {
        get { return "CustomHttpModule"; }
    }
    public void Init(HttpApplication context) {
        app = context;
        context.PreRequestHandlerExecute += new EventHandler(context_PreRequestHandlerExecute);
    }
    void context_PreRequestHandlerExecute(object sender, EventArgs e) {
        var request = app.Context.Request;
        request.Filter = new CustomHttpFilter(request.Filter);
    }
}

public class CustomHttpFilter : Stream {
    private Stream outputStream;
    public CustomHttpFilter(Stream outputFilter) {
        outputStream = outputFilter;
    }
    public override int Read(byte[] buffer, int offset, int count) {
        // read and make the necessary changes
    }
}
解决方案

I do not believe it is possible to modify the request with an http module, however it is possible to modify the response. The HttpRequest object is mostly read-only, so it is generally immutable in any context, not just from a module.

If you were really desperate, you might try using reflection to get access to more of the http request object. To access, modify, or invoke non-public members, however, your process would need full trust permissions, which is very risky in a web environment.

这篇关于是否有可能修改的HttpRequest POST在一个IIS HTTP模块的内容?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-13 20:02