How can I add an extra http header using IHTTPNego

2019-04-10 07:09发布

问题:

How can I add an extra header to the request using IHTTPNegotiate? I added the interface but functions BeginningTransaction & OnResponse never get called.

TNameSpaceHandler = class(TComObject, IInternetProtocol, IHttpNegotiate)  
...
    function BeginningTransaction(szURL, szHeaders: LPCWSTR; dwReserved: DWORD;
        out szAdditionalHeaders: LPWSTR): HResult; stdcall;
    function OnResponse(dwResponseCode: DWORD; szResponseHeaders, szRequestHeaders: LPCWSTR;
        out szAdditionalRequestHeaders: LPWSTR): HResult; stdcall;
...
end;

回答1:

I'm silently assuming you're intercepting traffic by both implementing IInternetProcol and the IInternetProtocolSink and IInternetBindInfo interfaces, and call the original IInternetProtocol to have the work done. In that case it's important to know the direction of who calls who.

If the original handler want the additional headers, it will first cast your IInternetProtocolSink into a IServiceProvider interface (with QueryInterface), and call QueryService for an IHttpNegotiate instance. By convenience you can add the current object instance and also implement IHttpNegotiate on the same object, but this is not required.

When the BeginTransaction method of your IHttpNegotiate gets called, get a IHttpNegotiate instance on the ProtSink of the Start call, call BeginTransaction and add your header(s) before passing them to the caller.