DelphiFAQ Home Search:

Detect an HTTP proxy

 

commentsThis article has not been rated yet. After reading, feel free to leave comments and rate it.

If you write a core http client, e.g. from socket level, you may need to detect whether there is an http proxy used. This includes the name of the proxy server and the port number it operates on.
Such proxy servers are often used where a firewall is installed.

Luckily IE is installed on many Windows systems, and IE puts this information in the registry under

\Software\Microsoft\Windows\CurrentVersion\Internet Settings

The following procedure GetProxy retrieves the host name, port number and whether the proxy is enabled. You can use it as shown in the FormCreate() event handler.

Note:
The value ProxyEnable should be a DWord, but sometimes it may be stored as binary or as a string, depending on the version of IE that the user has installed. The code below evaluates the type and reads it appropriately.

Edit:
The declaration for the constant sProxyEnable was missing. Thanks to S. Neigaard for pointing this out.

unit fProxy;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics,
  Controls, Forms, Dialogs, Registry;

type
  TForm1 = class(TForm)
    procedure FormCreate(Sender: TObject);
  private
    { private declarations }
  public
    { public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.DFM}

function GetProxy(var Host : string;
                  var Port : integer; 
                  var ProxyEnabled : boolean) : boolean;
const
  sProxyEnable = 'ProxyEnable';
var
  s : string;
  p : integer;
begin
  with TRegistry.Create do
  begin
    RootKey := HKEY_CURRENT_USER;
    ProxyEnabled := false;
    s := '';
    OpenKey ('\Software\Microsoft\Windows\CurrentVersion\Internet Settings',
             True);
    if ValueExists('ProxyServer') then
      s := ReadString('ProxyServer');

    if s <> '' then
    begin
      p := pos(':', s);
      if p=0 then
        p := length(s)+1;
      Host := copy(s, 1, p-1);
      try
        Port := StrToInt(copy (s,p+1,999));
      except
        Port := 80;
      end;

      ProxyEnabled := true;
    end;

    if ValueExists('ProxyEnable') then
    begin
        case GetDataType(sProxyEnable) of
        rdString,
        rdExpandString:
        begin
          sPortTmp := AnsiLowerCase(ReadString(sProxyEnable));
          ProxyEnabled := true;
          if pos(' '+sPortTmp+' ', ' yes true t enabled 1 ') > 0 then
            ProxyEnabled := true
          else
          if pos(' '+sPortTmp+' ', ' no false f none disabled 0 ') > 0 then
            ProxyEnabled := false
        end;
        rdInteger:
        begin
          ProxyEnabled := ReadBool(sProxyEnable);
        end;
        rdBinary:
        begin
          ProxyEnabled := true;
          ReadBinaryData(sProxyEnable, ProxyEnabled, 1);
        end;
        end;
    end;

    Free;
  end;

  Result := s<>'';
end;

procedure TForm1.FormCreate(Sender: TObject);
var
  Host : string;
  Port : integer;
  ProxyEnabled : boolean;
const
  YesNo : array [false..true] of string = (' not ', '');
begin
  // get proxy information
  if GetProxy(Host, Port, ProxyEnabled) then
    ShowMessage(Format('Your proxy is %s on port %d, it is%s enabled.',
                [Host, Port, YesNo[ProxyEnabled]]))
  else
    ShowMessage('No proxy detected');
end;

end.
You don't like the formatting? Check out SourceCoder then!
Content-type: text/html

Comments:

2007-06-08, 21:44:38
anonymous from United States  
Good
2012-09-04, 14:01:58
anonymous from Iceland  
z18-05.opera-mini.net

 

 

NEW: Optional: Register   Login
Email address (not necessary):

Rate as
Hide my email when showing my comment.
Please notify me once a day about new comments on this topic.
Please provide a valid email address if you select this option, or post under a registered account.
 

Show city and country
Show country only
Hide my location
You can mark text as 'quoted' by putting [quote] .. [/quote] around it.
Please type in the code:

Please do not post inappropriate pictures. Inappropriate pictures include pictures of minors and nudity.
The owner of this web site reserves the right to delete such material.

photo Add a picture: