来自 IWebBrowser2 的 HTTP 响应 header

HTTP response header from IWebBrowser2

与此 question, I am trying to get a specific value in the response header. It references a pass-through solution using Asynchronous Pluggable Protocol (APP) 处理程序非常相似,但是由于解决方案已经过时,因此链接已失效。

也许我处理的不正确。

概览: 我正在扩展我用 C++(桌面应用程序)编写的 OAuth2 库,它适用于 Google API,现在允许与 Microsoft 协商。如响应 header 的 this site (under 'Redirect request after successful sign in'), the auth code is returned in the Location 字段所引用。使用 HTTP 调试器,我可以看到发送的值(见下文)。

当尝试获取 IHTMLLocation interface from IHTMLDocument2 时,none 的属性包含我可以使用 HTTP 调试器看到的值,即使 'Location'字段在响应 headers 中不存在(这显然不是所需要的)。

这是唯一的hang-up。所有后续调用和请求,我使用 WinHTTP API,响应 header 是 easily accessed。然而,这个阶段仍然是用户可以在浏览器中正确验证和授权应用程序的阶段。感谢您的宝贵时间和帮助。

更新: 响应类似于以下内容:

HTTP/1.1 302 Found
Cache-Control: private
Content-Type: text/html; charset=utf-8
Content-Encoding: gzip
Location: urn:ietf:wg:oauth:2.0:oob?code=[VALUE]

在这种情况下,不需要获得响应 header。授权码也作为查询参数传递给重定向 url.

实现DWebBrowserEvents2,得到Invoke函数中的url:

HRESULT __stdcall DWebBrowserEvents2::Invoke(_In_ DISPID dispIdMember,
                                             _In_ REFIID riid,
                                             _In_ LCID lcid,
                                             _In_ WORD flags,
                                             _In_ DISPPARAMS *pDispParams,
                                             _Out_opt_ VARIANT *pVarResult,
                                             _Out_opt_ EXCEPINFO *pExcepInfo,
                                             _Out_opt_ UINT *puArgErr);

检测时间

// new document goes ReadyState_Complete
dispIdMember == DISPID_DOCUMENTCOMPLETE  [ 259 ]

url 是位于此处的变体:

VARIANT *vurl = pDispParams->rgvarg[0].pvarVal; // <-- not bstrVal

转换为 BSTR,然后是字符串:

BSTR url = (vurl->vt & VT_BYREF) ? *vurl->pbstrVal : vurl->bstrVal;
std::wstring ws(url, SysStringLen(url));

// url will look similar to the following:
// http://localhost/?code=[...]&session_state=[...]