İçeriğe geç
Guides 3 dk okuma 702 görüntülenme

Using Proxy in Dart/Flutter

Proxy Setup in Dart and Flutter: HttpClient, dio, SOCKS5, and Peculiarities of Working with Proxies in Mobile Applications.

Using Proxy in Dart/Flutter

Using Proxies in Dart/Flutter

Proxies in Dart/Flutter

Dart is a programming language for Flutter applications. Working with proxies in Dart is done through the standard HttpClient and popular libraries (dio, http).

Standard HttpClient

HTTP Proxy

import 'dart:io';

void main() async {
  final client = HttpClient();

  // Proxy configuration
  client.findProxy = (uri) {
    return "PROXY proxy_ip:8080";
  };

  final request = await client.getUrl(
    Uri.parse("https://httpbin.org/ip")
  );
  final response = await request.close();

  await response.transform(utf8.decoder).forEach(print);
  client.close();
}

With Authentication

import 'dart:io';

void main() async {
  final client = HttpClient();

  client.findProxy = (uri) {
    return "PROXY proxy_ip:8080";
  };

  // Handling proxy authentication
  client.authenticate = (uri, scheme, realm) async {
    client.addProxyCredentials(
      "proxy_ip",
      8080,
      "realm",
      HttpClientBasicCredentials("username", "password")
    );
    return true;
  };

  final request = await client.getUrl(
    Uri.parse("https://httpbin.org/ip")
  );
  final response = await request.close();

  await response.transform(utf8.decoder).forEach(print);
  client.close();
}

Direct Connection (No Proxy)

client.findProxy = (uri) => "DIRECT";

Conditional Routing

client.findProxy = (uri) {
  if (uri.host.contains("target-site.com")) {
    return "PROXY proxy_ip:8080";
  }
  return "DIRECT";
};

Dio Library

Dio is the most popular HTTP library for Flutter.

Installation

# pubspec.yaml
dependencies:
  dio: ^5.4.0
  dio_proxy_adapter: ^0.0.2  # for proxies

HTTP Proxy with Dio

import 'package:dio/dio.dart';
import 'dart:io';

void main() async {
  final dio = Dio();

  // Proxy configuration via HttpClient adapter
  (dio.httpClientAdapter as IOHttpClientAdapter).createHttpClient = () {
    final client = HttpClient();
    client.findProxy = (uri) {
      return "PROXY proxy_ip:8080";
    };
    // For self-signed certificates (testing)
    client.badCertificateCallback = (cert, host, port) => true;
    return client;
  };

  final response = await dio.get("https://httpbin.org/ip");
  print(response.data);
}

With Authentication

(dio.httpClientAdapter as IOHttpClientAdapter).createHttpClient = () {
  final client = HttpClient();
  client.findProxy = (uri) => "PROXY proxy_ip:8080";
  client.addProxyCredentials(
    "proxy_ip",
    8080,
    "",
    HttpClientBasicCredentials("user", "pass")
  );
  return client;
};

SOCKS5 Proxy

Via socks5_proxy

# pubspec.yaml
dependencies:
  socks5_proxy: ^1.0.4
import 'dart:io';
import 'package:socks5_proxy/socks_client.dart';

void main() async {
  // Creating a SOCKS5 client
  final proxy = SocksTCPClient.connect(
    [ProxySettings(InternetAddress("proxy_ip"), 1080,
      password: Password("username", "password"))],
    InternetAddress("93.184.216.34"), // target IP
    80,
  );

  // Use the socket for HTTP requests
}

Via System Proxy with SOCKS

client.findProxy = (uri) {
  return "SOCKS5 proxy_ip:1080";
};

Note: The standard HttpClient in Dart only supports PROXY (HTTP) and DIRECT. For SOCKS, third-party libraries are required.

Flutter Specifics

Android Configuration

Android requires network_security_config.xml setup to work with cleartext HTTP proxies:

<!-- android/app/src/main/res/xml/network_security_config.xml -->
<network-security-config>
    <base-config cleartextTrafficPermitted="true">
        <trust-anchors>
            <certificates src="system" />
        </trust-anchors>
    </base-config>
</network-security-config>

iOS Configuration

For iOS, proxies work through URLSession system settings. A Flutter application automatically uses the system proxy.

Environment Variables

In debug mode, proxies can be set via variables:

// Via launch arguments
// flutter run --dart-define=HTTP_PROXY=http://proxy_ip:8080

Proxy Rotation

import 'dart:io';
import 'dart:math';

final proxies = [
  "PROXY proxy1:8080",
  "PROXY proxy2:8080",
  "PROXY proxy3:8080",
];

final random = Random();

void main() async {
  final client = HttpClient();

  client.findProxy = (uri) {
    return proxies[random.nextInt(proxies.length)];
  };

  // Requests will use different proxies
  for (var i = 0; i < 10; i++) {
    final request = await client.getUrl(
      Uri.parse("https://httpbin.org/ip")
    );
    final response = await request.close();
    final body = await response.transform(utf8.decoder).join();
    print("Request $i: $body");
  }

  client.close();
}

Error Handling

try {
  final request = await client.getUrl(uri);
  final response = await request.close();
  // handle response
} on SocketException catch (e) {
  print("Connection error: $e");
} on HttpException catch (e) {
  print("HTTP error: $e");
} on HandshakeException catch (e) {
  print("TLS/SSL error: $e");
} catch (e) {
  print("Unexpected error: $e");
}

Conclusion

Dart and Flutter provide basic but sufficient tools for working with HTTP proxies. For advanced scenarios (SOCKS5, authentication), use Dio with a custom HttpClient adapter. When developing Flutter applications, consider the platform-specific features of Android and iOS.

Güncellendi: 06.03.2026
Kategoriye dön

Bunları da okuyun

Guides 2 dk

Nike SNKRS için Proxy: Sınırlı Sürümler ve Çoklu Katılım

Nike SNKRS'ta birden fazla hesapla çekilişlere katılmak, IP limitlerini aşmak ve ban yemeden sınırlı sürümleri kapmak için residential veya mobil proxy kullanın. Hangi türü seçmeli ve nasıl kurmalı.

Guides 2 dk

iPhone'da Proxy Nasıl Kurulur (Wi-Fi ve SOCKS5)

iPhone'da proxy'yi yerleşik Wi-Fi ayarlarından (HTTP/HTTPS) veya SOCKS5 ile hücresel kapsama için Shadowrocket gibi bir uygulamayla kurun. Adım adım iOS kurulumu ve bilinmesi gereken kısıtlar.

Guides 3 dk

Tinder için proxy: çoklu hesap ve ban'lerden kaçınma

Birden fazla profil işletmek ve IP shadowban'lerinden kaçınmak için Tinder'da mobil veya residential proxy kullanın. Mobilin neden kazandığı, kurulumu ve proxy'nin şehri neden değiştirmediği.

Guides 2 dk

StockX için proxy: fiyat takibi ve hesap yönetimi

StockX'te fiyatları izlemek, birden fazla hesap çalıştırmak ve drop'ları yasaklanmadan kapmak için residential, ISP veya mobil proxy kullanın. Hangi türü seçeceğinizi ve nasıl kuracağınızı anlatıyoruz.

Guides 3 dk

OpenAI API için Proxy: Erişim, Hız Limitleri ve Kurulum

OpenAI API'yi residential veya ISP proxy üzerinden yönlendirerek desteklenen bölgelere erişin, 429 hız limitlerinden kaçının ve hesapları izole edin. Hangi proxy'yi seçmeli ve Python kurulum örnekleri.

Guides 1 dk

E2E testleri için Cypress'te proxy kurulumu

Cypress'te proxy kurulumu: HTTP_PROXY değişkenleri, cy-proxy-middleware ve coğrafi konuma bağlı içeriğin test edilmesi.

Proxy'lerimizi deneyin

100+ ülkede 20,000+ proxy

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