Solved

changing OS information with TWebBrwser

Posted on 2004-09-30
2
174 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Delphi OLE Error 8 96
add combobox item based on numbers 9 125
Controlled Assessment GCSE - desperate help needed 4 76
Multi-layered image in FireMonkey 9 19
A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…

863 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

22 Experts available now in Live!

Get 1:1 Help Now