473,378 Members | 1,396 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,378 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 3702

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...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.