Zum Inhalt springen
GProxy
Registrierung
Гайды 2 Min. Lesezeit 25 Aufrufe

Verwendung von Proxy in Delphi/Pascal

Proxy-Einrichtung in Delphi und Free Pascal: Indy, Synapse, WinINet und die Arbeit mit HTTP-/SOCKS5-Proxies.

Verwendung von Proxy in Delphi/Pascal

Proxies in Delphi/Pascal

Delphi und Free Pascal sind weiterhin beliebt für die Entwicklung von Windows-Anwendungen. Die Arbeit mit Proxies erfolgt über die Bibliotheken Indy (Delphi-Standard), Synapse und die WinINet API.

Indy (Internet Direct)

Indy ist Delphis integrierte Bibliothek für die Netzwerkprogrammierung.

HTTP Proxy

uses IdHTTP, IdSSLOpenSSL;

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

    // Proxy-Konfiguration
    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;

SOCKS5 Proxy über 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
    // SOCKS5-Konfiguration
    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 unterstützt keine Authentifizierung
Socks.Authentication := saNoAuthentication;

Synapse

Synapse ist eine alternative Netzwerkbibliothek für Free Pascal und Delphi.

Installation

Herunterladen von synapse.ararat.cz und zum Projekt hinzufügen.

HTTP Proxy

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('Fehler: ', HTTP.ResultCode);
  finally
    HTTP.Free;
  end;
end;

SOCKS über 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;

WinINet API (Windows)

Verwendung der System-Windows-API:

uses WinInet;

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

  if hInternet <> nil then begin
    // Proxy-Authentifizierung setzen
    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;

System-Proxies verwenden

hInternet := InternetOpen(
  'MyApp',
  INTERNET_OPEN_TYPE_PRECONFIG,  // Systemeinstellungen verwenden
  nil,
  nil,
  0
);

Proxy-Rotation

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('Fehler mit Proxy ', Proxy.Host, ': ', E.Message);
    end;
  finally
    HTTP.Free;
  end;
end;

Tipps

  1. Indy ist die Standardwahl für Delphi, im Lieferumfang der IDE enthalten
  2. Synapse ist für Free Pascal und plattformübergreifende Projekte
  3. WinINet ist für reine Windows-Anwendungen, die den System-Stack verwenden
  4. Für SOCKS verwenden Sie Indy TIdSocksInfo oder Synapse SocksType
  5. OpenSSL DLL — vergessen Sie nicht, libeay32.dll und ssleay32.dll für HTTPS neben Ihrer EXE-Datei zu platzieren

Fazit

Delphi/Pascal bieten ausgereifte Tools für die Arbeit mit Proxies. Indy ist die umfassendste Lösung, die HTTP, SOCKS4, SOCKS5 und Authentifizierung unterstützt. Für einfache Aufgaben unter Windows ist die WinINet API eine schnelle Option ohne externe Abhängigkeiten.

Aktualisiert: 06.03.2026
Zurück zur Kategorie

Testen Sie unsere Proxys

20.000+ Proxys in über 100 Ländern weltweit

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