Solved

changing OS information with TWebBrwser

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

Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Delphi Mdi application Child forms get behind control 7 216
Delphi Form ownership 4 90
QRReport  TQrmemo vertical stretching 1 47
Base1 Encode/Decode 3 77
In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
In a recent question (https://www.experts-exchange.com/questions/28997919/Pagination-in-Adobe-Acrobat.html) here at Experts Exchange, a member asked how to add page numbers to a PDF file using Adobe Acrobat XI Pro. This short video Micro Tutorial sh…
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.

831 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