Solved

changing OS information with TWebBrwser

Posted on 2004-09-30
2
192 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

On Demand Webinar: Networking for the Cloud Era

Ready to improve network connectivity? Watch this webinar to learn how SD-WANs and a one-click instant connect tool can boost provisions, deployment, and management of your cloud connection.

Question has a verified solution.

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

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…
Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.
Suggested Courses
Course of the Month10 days, 17 hours left to enroll

631 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