Solved

changing OS information with TWebBrwser

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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

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…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

680 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