Solved

changing OS information with TWebBrwser

Posted on 2004-09-30
2
170 Views
Last Modified: 2010-04-05
Hello,
i want to ask is it possible to change the browser and os info sent by Twebbrowser to server.
the header looks like:
User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1)
is it possible to change it?
Thanks
0
Comment
Question by:owui_games
2 Comments
 
LVL 26

Accepted Solution

by:
Russell Libby earned 500 total points
ID: 12203137
Yes, but it requires catching the BeforeNavigate2 event (to get the navigation info), and then reissuing the Navigate2 call using the updated headers. Its a bit of a pain that they can't be modified (they can, but the browser ignores the changes) the first time through, but the below technique does work

Just change the const below to set the USER_AGENT you wish to send. The real work is done in the WebBrowser1BeforeNavigate2 method.

Regards,
Russell

unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  StdCtrls, OleCtrls, SHDocVw;

type
  TForm1            = class(TForm)
     WebBrowser1:   TWebBrowser;
     Button1:       TButton;
     Edit1:         TEdit;
     procedure      Button1Click(Sender: TObject);
     procedure      WebBrowser1BeforeNavigate2(Sender: TObject; const pDisp: IDispatch; var URL, Flags, TargetFrameName, PostData, Headers: OleVariant; var Cancel: WordBool);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

const
  USER_AGENT     =   'User-Agent: Testing of the new user agent';

var
  Form1: TForm1;

implementation
{$R *.DFM}

procedure TForm1.Button1Click(Sender: TObject);
var  ovURL:      OleVariant;
begin

  // Navigate to whatever is in the edit box
  ovUrl:=Edit1.Text;
  WebBrowser1.Navigate2(ovURL);

end;

procedure TForm1.WebBrowser1BeforeNavigate2(Sender: TObject; const pDisp: IDispatch; var URL, Flags, TargetFrameName, PostData, Headers: OleVariant; var Cancel: WordBool);
var  HeaderList:    TStringList;
     dwIndex:       Integer;
     bFound:        Boolean;
     bMatch:        Boolean;
begin

  // Create string list for handling headers
  HeaderList:=TStringList.Create;
  try
     // Get the header text
     HeaderList.Text:=Headers;
     // Set default match and found state
     bFound:=False;
     bMatch:=False;
     // Walk the items
     for dwIndex:=0 to Pred(HeaderList.Count) do
     begin
        // Attempt to find the user agent
        if (Pos('user-agent', LowerCase(HeaderList[dwIndex])) = 1) then
        begin
           // Found the user agent
           bFound:=True;
           // Check for match
           bMatch:=(CompareText(HeaderList[dwIndex], USER_AGENT) = 0);
           // Check match state
           if not(bMatch) then HeaderList[dwIndex]:=USER_AGENT;
           // Done
           break;
        end;
     end;
     // If not found then we need to add the line
     if not(bFound) then HeaderList.Add(USER_AGENT);
     // Should we cancel the navigation?
     if not(bFound) or not(bMatch) then
     begin
        // Cancel
        Cancel:=True;
        // Update the headers
        Headers:=HeaderList.Text;
        // Navigate again
        WebBrowser1.Navigate2(URL, FLags, TargetFrameName, PostData, Headers);
     end;
  finally
     // Free the string list
     HeaderList.Free;
  end;

end;
0
 

Author Comment

by:owui_games
ID: 12204036
Works great, i am impressed :)
Thanks very much, if i could i  give you 5k points ;)

0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
This video discusses moving either the default database or any database to a new volume.
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now