Reputation: 2301
I need to run every Network Request made from my Flutter Application through a local Proxy localhost:3128
. This proxy is set up globally in my system, and the iOS Simulator picks up the Settings (i.e. using Safari on the Simulator works fine). The Network Connection of my Flutter App works fine if I specify my Proxy explicitly like this:
HttpClient client = HttpClient();
client.findProxy = (uri) {
return "PROXY localhost:3128;";
};
....
However, I naturally can't use abstractions that build upon HttpClient
, and don't expose methods to set the Proxy manually, such as NetworkImage.
Is there a way to set the Proxy globally for all HttpClient's used in the application for my development purposes? Alternatively, what is the best way to set the appropriate Environment Variables in the iOS Simulator so findProxyFromEnvironment picks up the correct one? Finally, is there a way to make the Proxy transparent in the iOS Simulator?
Upvotes: 2
Views: 1274
Reputation: 78
You can use HttpOverrides.global to have a single interceptor that can be used to create the HTTP client when instantiated.
class MyHttpOverrides extends HttpOverrides {
@override
HttpClient createHttpClient(SecurityContext context) {
return super.createHttpClient(context)
..findProxy = (uri) {
return "PROXY localhost:3128;";
}
..badCertificateCallback =
(X509Certificate cert, String host, int port) => true;
}
}
void main() {
HttpOverrides.global = new MyHttpOverrides();
runApp(MyApp());
}
Upvotes: 3