473,242 Members | 3,912 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,242 software developers and data experts.

Using Navigate2 to add Custom HTTP Header

Hello,

I am developing a BHO that should add a custom HTTP header on a
specific domain only. Don't want the header globally, otherwise I
could just add a registry key.

So, on BEFORENAVIGATE2, I am canceling the current navigation and
renavigating with the new custom header using Navigate2. This usually
works fine, but in some cases (particularly when selecting a link that
starts a new window), the Navigate2 call fails.

Running out of ideas, so any help would be greatly appreciated!

Here is my current code to add custom headers (yes, it's in C++/ATL but
a .NET/C# solution would be just as helpful, thanks.)

Note, this code fails with Navigate2 returning HRESULT 0x80004005.

Any ideas?

---

STDMETHODIMP CBho::Invoke(DISPID dispidMember, REFIID riid, LCID lcid,
WORD wFlags,
DISPPARAMS* pDispParams, VARIANT* pvarResult,
EXCEPINFO* pExcepInfo, UINT* puArgErr)
{
if (!pDispParams)
return E_INVALIDARG;

switch (dispidMember)
{

case DISPID_BEFORENAVIGATE2:
{
CComQIPtr<IWebBrowser2, &IID_IWebBrowser2> WebBrowser;
WebBrowser = pDispParams->rgvarg[6].pdispVal;
CW2CT pszUrl(pDispParams->rgvarg[5].pvarVal->bstrVal);
CStdString strUrl( (LPCTSTR)pszUrl );

// This is an "action cancelled" page. After the renav, we
sometimes
// get a brief action cancelled page showing up before the final
// renavigated page. Just block it so it doesn't appear at all.
if (strUrl.Right(12).CompareNoCase(_T("navcancl.htm") ) == 0 ) {
WebBrowser->Stop(); // Sometimes Stop() re-enters here, other
times it doesn't
*V_BOOLREF(&pDispParams->rgvarg[0]) = VARIANT_TRUE;
break;
}

if (pDispParams->rgvarg[1].vt != (VT_BYREF|VT_VARIANT)) {
break;
}

// Is this a xyzinc.com domain page? If so, add our HTTP header.
// Note: this block adds "xyzinc-Header:" as a header to outgoing
// GET/POST requests iff "xyzinc.com" is the target domain
if (WebBrowser && m_spWebBrowser2 == WebBrowser) {

CUrl url;
if (url.CrackUrl( (LPCTSTR)pszUrl ) == FALSE) {
break;
}

CStdString strHostName ( url.GetHostName() );

// Is this an "xyzinc.com" url
int lenxyzinc = ( strHostName.GetLength() > 11 ) ? 11 :
strHostName.GetLength();
if (strHostName.Right(lenxyzinc).CompareNoCase(
_T(".xyzinc.com") ) != 0)
break; // not our domain - don't add header

// Dig out the aleady specifed headers. Note, this is only
// new headers added specifically by a "Navigate2()" call.
// The standard headers from the registry are not included.
CComVariant varHEADERS(*pDispParams->rgvarg[1].pvarVal);
varHEADERS.ChangeType(VT_BSTR);
CW2CT pszHeaders( varHEADERS.bstrVal );
// This call is recursive, so we'll get back here again after
// the header is added. So, check if we already added the
header
// and abort everything if this is the second call.
if ( strlen(pszHeaders) == 0 || strstr(pszHeaders,
"xyzinc-Header:") == NULL ) {

// Now we know this is a page to add "xyzinc-Header:" to and
// that this is the first of two calls (recursive).
// So, the way to do it is (1) stop the current navigate.
// (2) reissue a new navigate keeping all params the same
// except adding our "xyzinc-Header:" header.

// Stop the original navigation immediately so it does not
// have enough time to show. Canceling a nav requires
// a "Stop()" call plus returning "TRUE" in the cancel param.
WebBrowser->Stop();
ATLASSERT(V_VT(&pDispParams->rgvarg[0]) == (VT_BOOL |
VT_BYREF)); // Just in case
*V_BOOLREF(&pDispParams->rgvarg[0]) = VARIANT_TRUE;

// Now, specify our header and renav. But first, make sure
// we add our header to any existing headers passed in.
CComVariant vtHeaders;
CStdString strHeader;
strHeader.Format("%sxyzinc-Header: %s\r\n",
(LPCTSTR)pszHeaders, (LPCTSTR)m_pszMyHeader);
vtHeaders = strHeader.c_str();

// Note PostData argument passes pvarVal->pvarVal.
Otherwise,
// postdata is dropped and forms loose the data during a
post.
HRESULT hr = WebBrowser->Navigate2( &pDispParams->rgvarg[5],
&pDispParams->rgvarg[4], &pDispParams->rgvarg[3],
(pDispParams->rgvarg[2].pvarVal)->pvarVal, &vtHeaders );
if (FAILED(hr)) {
LOGFAILURE("Navigate2() failed HRESULT=%x", hr);
// !!!!!! THIS FAILS
// with HRESULT=0x80004005
}
}
}
}

Jun 15 '06 #1
0 3698

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Tobias Hesselmann | last post by:
Hi folks, i have a problem using a PHP script as a custom handler in Apache. What i wanna do is this: Whenever a .html file is requested by a browser, i want Apache to call a CGI that outputs...
0
by: mic | last post by:
I'm stuck with following problem. Using webbrowser control I try to make custom-made request. I found working example of this written in VBS but porting it to Python doesn't seem to work. The key...
1
by: Kevin | last post by:
Hi, My application is a MDI and for each document, I create a WebBrowser control and call Navigate2 to get the HTML view of each document. I have tried opened two documents and Navigate2 to...
3
by: Tim Bücker | last post by:
Hello. I am trying to launch the internet explorer from my program using the code seen below but unfortunately the Navigate2 command is ignored. So Navigate2 is not working but calling GoHome()...
4
by: Matthew Roche | last post by:
Greetings: I am developing an application that uses an ASP.NET Web Forms application for its UI and ASP.NET web services for its business tier, and I am looking for assistance in improving my...
2
by: asnowfall | last post by:
I am creating MailMessage out of already existing email message. My existing message has few custom properties. I wan to add them to System.Net.Mail.MailMessage How to add custom properties? ...
1
by: mikeh3275 | last post by:
I have a header control that I include in my .aspx page. In this control, I'm trying to retrieve a cookie value and store it into a string, but I am receiving an "Object reference not set to an...
5
by: zhang | last post by:
error C3861: 'Navigate2': identifier not found why???
3
by: skcsknathan001 | last post by:
I have 2 projects, and accessing a custom control , fron another project. Inside the custom control, it calls a function It compiles and when it runs I get a NullReference error message on the...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.