views:

420

answers:

2

Hi, all!

I want to make own browser application with own value of User-Agent.

Browser based on CHtmlView. MFC.

But exist strange problem with User-Agent from Ajax requests...

I did:

  1. User-Agent value is used as argument to Navigate(). A Navigate() requests use right User-Agent.

  2. Overload of OnAmbientProperty() method of CHtmlView class.

    BOOL MyHtmlView::OnAmbientProperty(COleControlSite *pSite, 
DISPID dispid, VARIANT *pvar) 
    {
      USES_CONVERSION;
      // Change user agent for this web browser host during hyperlinks
      if (dispid == DISPID_AMBIENT_USERAGENT)
      {
        pvar->vt = VT_BSTR;
        pvar->bstrVal = ::SysAllocString(m_userAgent);
        return TRUE;
      }
      return CHtmlView::OnAmbientProperty(pSite, dispid, pvar);
    }

This solve problem with hyper link.

But I have to use this browser for some Ajax application. And here is problem. For Ajax requests it use original IE User-Agent value.

My PC is WinXP based with IE7.

Any idea how to solve this? How to change User-Agent for any request from my browser?

Thanks!

+1  A: 

On ajax request, you can set the HTTP header "User-Agent" : http://www.w3.org/TR/2007/WD-XMLHttpRequest-20070618/#dfn-setrequestheader

Alsciende
A: 

I solved problem with UrlMkSetSessionOption(), it changed IE settings for current session only:

    const char ua[] = "My user agent string";
    HRESULT rez = UrlMkSetSessionOption(URLMON_OPTION_USERAGENT, (LPVOID)ua, sizeof(ua), 0);

Just one but, it change settings for once. Another calls return no error and no changes.

Some references: http://stackoverflow.com/questions/937573/changing-the-useragent-of-the-webbrowser-control-winforms-c

Silamantex