You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Flutter WebView加载指定网站出现net::ERR_CONNECTION_TIMED_OUT问题求助

问题描述

我开发了一款Flutter应用,使用webview_flutter包加载网站https://pagoumbria.regione.umbria.it/pagoumbria/时,始终收到net::ERR_CONNECTION_TIMED_OUT错误。该网站在浏览器中可正常访问,我无法定位问题。

WebViewController代码

late final WebViewController webViewController = WebViewController()
  ..setJavaScriptMode(JavaScriptMode.unrestricted)
  ..setNavigationDelegate(
    NavigationDelegate(
      onProgress: (int progress) {
        if (progress == 100) {
          print("webView progress $progress% - done");
          isLoading.value = false;
        } else {
          print("webView progress $progress%");
          isLoading.value = true;
        }
      },
      onPageStarted: (String url) {
        print("onPageStarted: $url");
        isLoading.value = true;
      },
      onHttpError: (HttpResponseError httpError) {
        if (isWebViewShown.isTrue) {
          print("onHttpError: ${httpError}");
        }
      },
      onWebResourceError: (WebResourceError webResourceError) {
        if (isWebViewShown.isTrue) {
          print("onWebResourceError: ${webResourceError.description}");
        }
      },
      onNavigationRequest: (request) {
        print("onNavigationRequest: ${request.url}");
        return NavigationDecision.navigate;
      },
      onUrlChange: (change) {
        print("onUrlChange: ${change.url}");
      },
    ),
  );

初始化代码

@override
void onInit() async {
  super.onInit();
  try {
    webViewController.loadRequest(
        Uri.parse("https://pagoumbria.regione.umbria.it/pagoumbria/"));
  } catch (e) {
    error.value = 'Errore durante il caricamento della pagina di pagamento';
  }
}

错误截图

错误截图

WebView显示结果截图

WebView显示结果

解决方案

针对net::ERR_CONNECTION_TIMED_OUT错误,可按以下步骤排查修复:

  • 检查网络权限配置
    Android端需在AndroidManifest.xml中添加网络权限:

    <uses-permission android:name="android.permission.INTERNET" />
    

    iOS端需在Info.plist中添加允许HTTP/HTTPS访问的配置:

    <key>NSAppTransportSecurity</key>
    <dict>
      <key>NSAllowsArbitraryLoads</key>
      <true/>
    </dict>
    

    注意:若仅需允许特定域名,可配置NSExceptionDomains替代NSAllowsArbitraryLoads,更安全。

  • 验证WebView代理设置
    若设备或应用使用了代理,可能导致WebView无法连接目标网站。可尝试关闭代理后重新测试,或在代码中为WebView配置正确代理。

  • 检查目标网站TLS版本支持
    部分旧版本WebView可能不支持网站使用的高版本TLS协议:

    1. 在浏览器中查看网站的TLS版本(如Chrome开发者工具-安全标签)
    2. 若网站使用TLS 1.3,需确保Android设备系统版本≥Android 10(API 29),或iOS版本≥13.0;若设备版本过低,可考虑升级系统或使用webview_flutter_android包自定义WebView设置,启用对高版本TLS的支持。
  • 测试其他网络环境
    切换到不同网络(如从Wi-Fi切换到移动数据),排除当前网络环境的限制(如防火墙、DNS污染等)。

  • 更新webview_flutter包
    确保使用的webview_flutter是最新版本,旧版本可能存在已知的网络连接bug,更新后可修复问题。

内容的提问来源于stack exchange,提问作者SonOfGillas

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.16 17:44:58