2005. augusztus 12., péntek

Detect an HTTP proxy


Problem/Question/Abstract:

Detect an HTTP proxy

Answer:

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.


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;
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.

Nincsenek megjegyzések:

Megjegyzés küldése