Pular para o conteúdo
Guides 3 min de leitura 809 visualizações

Usando proxy em Delphi/Pascal

Configuração de proxy em Delphi e Free Pascal: Indy, Synapse, WinINet e o trabalho com proxies HTTP/SOCKS5.

Usando proxy em Delphi/Pascal

Proxies em Delphi/Pascal

Delphi e Free Pascal continuam populares para o desenvolvimento de aplicações Windows. O trabalho com proxies é feito pelas bibliotecas Indy (padrão do Delphi), Synapse e pela API WinINet.

Indy (Internet Direct)

Indy é a biblioteca nativa do Delphi para programação de rede.

Proxy HTTP

uses IdHTTP, IdSSLOpenSSL;

procedure FetchViaProxy;
var
  HTTP: TIdHTTP;
  SSL: TIdSSLIOHandlerSocketOpenSSL;
begin
  HTTP := TIdHTTP.Create(nil);
  SSL := TIdSSLIOHandlerSocketOpenSSL.Create(nil);
  try
    HTTP.IOHandler := SSL;

    // Configuração do proxy
    HTTP.ProxyParams.ProxyServer := 'proxy_ip';
    HTTP.ProxyParams.ProxyPort := 8080;
    HTTP.ProxyParams.ProxyUsername := 'username';
    HTTP.ProxyParams.ProxyPassword := 'password';
    HTTP.ProxyParams.BasicAuthentication := True;

    WriteLn(HTTP.Get('https://httpbin.org/ip'));
  finally
    SSL.Free;
    HTTP.Free;
  end;
end;

Proxy SOCKS5 via Indy

uses IdHTTP, IdSocks, IdSSLOpenSSL;

procedure FetchViaSocks5;
var
  HTTP: TIdHTTP;
  SSL: TIdSSLIOHandlerSocketOpenSSL;
  Socks: TIdSocksInfo;
begin
  HTTP := TIdHTTP.Create(nil);
  SSL := TIdSSLIOHandlerSocketOpenSSL.Create(nil);
  Socks := TIdSocksInfo.Create(nil);
  try
    // Configuração SOCKS5
    Socks.Host := 'proxy_ip';
    Socks.Port := 1080;
    Socks.Version := svSocks5;
    Socks.Username := 'username';
    Socks.Password := 'password';
    Socks.Authentication := saUsernamePassword;

    SSL.TransparentProxy := Socks;
    HTTP.IOHandler := SSL;

    WriteLn(HTTP.Get('https://httpbin.org/ip'));
  finally
    Socks.Free;
    SSL.Free;
    HTTP.Free;
  end;
end;

SOCKS4

Socks.Version := svSocks4;
// SOCKS4 não suporta autenticação
Socks.Authentication := saNoAuthentication;

Synapse

Synapse é uma biblioteca de rede alternativa para Free Pascal e Delphi.

Instalação

Baixe em synapse.ararat.cz e adicione ao seu projeto.

Proxy HTTP

uses httpsend;

procedure FetchViaSynapseProxy;
var
  HTTP: THTTPSend;
begin
  HTTP := THTTPSend.Create;
  try
    HTTP.ProxyHost := 'proxy_ip';
    HTTP.ProxyPort := '8080';
    HTTP.ProxyUser := 'username';
    HTTP.ProxyPass := 'password';

    if HTTP.HTTPMethod('GET', 'https://httpbin.org/ip') then
      WriteLn(HTTP.Document.DataString)
    else
      WriteLn('Error: ', HTTP.ResultCode);
  finally
    HTTP.Free;
  end;
end;

SOCKS via Synapse

uses blcksock;

procedure FetchViaSynapseSocks;
var
  Sock: TTCPBlockSocket;
begin
  Sock := TTCPBlockSocket.Create;
  try
    Sock.SocksIP := 'proxy_ip';
    Sock.SocksPort := '1080';
    Sock.SocksUsername := 'username';
    Sock.SocksPassword := 'password';
    Sock.SocksType := ST_Socks5;

    Sock.Connect('httpbin.org', '80');
    if Sock.LastError = 0 then begin
      Sock.SendString('GET /ip HTTP/1.1' + #13#10 +
                      'Host: httpbin.org' + #13#10 +
                      #13#10);
      WriteLn(Sock.RecvString(10000));
    end;
  finally
    Sock.Free;
  end;
end;

API WinINet (Windows)

Usando a API nativa do Windows:

uses WinInet;

procedure FetchViaWinINet;
var
  hInternet, hConnect: HINTERNET;
  Buffer: array[0..4095] of Char;
  BytesRead: DWORD;
begin
  // Abre com proxy
  hInternet := InternetOpen(
    'MyApp',
    INTERNET_OPEN_TYPE_PROXY,
    PChar('http://proxy_ip:8080'),
    nil,
    0
  );

  if hInternet <> nil then begin
    // Define a autenticação do proxy
    InternetSetOption(hInternet, INTERNET_OPTION_PROXY_USERNAME,
      PChar('username'), Length('username'));
    InternetSetOption(hInternet, INTERNET_OPTION_PROXY_PASSWORD,
      PChar('password'), Length('password'));

    hConnect := InternetOpenUrl(hInternet,
      'https://httpbin.org/ip', nil, 0,
      INTERNET_FLAG_RELOAD, 0);

    if hConnect <> nil then begin
      InternetReadFile(hConnect, @Buffer, SizeOf(Buffer), BytesRead);
      WriteLn(Copy(Buffer, 1, BytesRead));
      InternetCloseHandle(hConnect);
    end;

    InternetCloseHandle(hInternet);
  end;
end;

Usando os proxies do sistema

hInternet := InternetOpen(
  'MyApp',
  INTERNET_OPEN_TYPE_PRECONFIG,  // usa as configurações do sistema
  nil,
  nil,
  0
);

Rotação de proxies

type
  TProxyInfo = record
    Host: string;
    Port: Integer;
    Username: string;
    Password: string;
  end;

var
  Proxies: array of TProxyInfo;
  ProxyIndex: Integer = 0;

function GetNextProxy: TProxyInfo;
begin
  Result := Proxies[ProxyIndex mod Length(Proxies)];
  Inc(ProxyIndex);
end;

procedure FetchWithRotation(const URL: string);
var
  HTTP: TIdHTTP;
  Proxy: TProxyInfo;
begin
  Proxy := GetNextProxy;
  HTTP := TIdHTTP.Create(nil);
  try
    HTTP.ProxyParams.ProxyServer := Proxy.Host;
    HTTP.ProxyParams.ProxyPort := Proxy.Port;
    HTTP.ProxyParams.ProxyUsername := Proxy.Username;
    HTTP.ProxyParams.ProxyPassword := Proxy.Password;

    try
      WriteLn(HTTP.Get(URL));
    except
      on E: Exception do
        WriteLn('Error with proxy ', Proxy.Host, ': ', E.Message);
    end;
  finally
    HTTP.Free;
  end;
end;

Dicas

  1. Indy é a escolha padrão para Delphi, já vem com a IDE
  2. Synapse é para Free Pascal e projetos multiplataforma
  3. WinINet é para aplicações somente Windows, usando a pilha do sistema
  4. Para SOCKS, use TIdSocksInfo do Indy ou SocksType do Synapse
  5. DLL do OpenSSL — não esqueça de colocar libeay32.dll e ssleay32.dll ao lado do seu exe para HTTPS

Conclusão

Delphi/Pascal oferecem ferramentas maduras para trabalhar com proxies. Indy é a solução mais completa, com suporte a HTTP, SOCKS4, SOCKS5 e autenticação. Para tarefas simples no Windows, a API WinINet é uma opção rápida e sem dependências externas.

Atualizado: 06.03.2026
Voltar à categoria

Leia também

Experimente nossos proxies

20,000+ proxies em 100+ países do mundo

support_agent
GProxy Support
Usually replies within minutes
Hi there!
Send us a message and we'll reply as soon as possible.