简体   繁体   中英

How to use webview flutter with proxy?

I'm using Webview Flutter package to embed some websites to my flutter project. For some websites I need to use proxy server (ip:port) to connect (country access restrictions). How can I add proxy server to my project and use Webview Flutter package through it?

Unfortunately Flutter ignores your device settings for proxy, so you'll need to override it within your Flutter app like so:

class MyProxyHttpOverride extends HttpOverrides {
  @override
  HttpClient createHttpClient(SecurityContext context) {
    return super.createHttpClient(context)
      ..findProxy = (uri) {
        return "PROXY localhost:8888;";
      }
      ..badCertificateCallback =
          (X509Certificate cert, String host, int port) => true;
  }
}

// In your main.dart
HttpOverrides.global = MyProxyHttpOverride();

Of course, you'll need to change localhost:8888 to whatever your proxy settings are - and likely in your case this will have to be dynamic that the user can change.

I wrote a more detailed write up about this solution here - although this is about getting it to work with Charles Proxy, the overall problem and solution is the same.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM