【问题标题】:Can't send http requests on Android if connecting to WIFI through app如果通过应用程序连接到 WIFI,则无法在 Android 上发送 http 请求
【发布时间】:2021-04-18 14:09:00
【问题描述】:

我正在构建一个在 Android 9 上运行的应用。

一旦我使用此库wifi_configuration 从我的应用程序连接到 WIFI 网络,我就无法使用 HTTP 库向http://192.168.80.1:4443 发出成功的 HTTP 请求,而是向http://www.google.com 发出成功的 HTTP 请求。我对http://192.168.80.1:4443 的请求挂起并导致以下错误:SocketException: OS Error: Connection timed out, errno = 110, address = 192.168.80.1, port = 55496 图书馆无法毫无问题地连接到网络。

如果我直接从 Android UI 连接到 WIFI,我可以向http://192.168.80.1:4443 发出 HTTP 请求。所以从我的应用程序连接到 WIFI 并调用http://192.168.80.1:4443 时会出现问题。会不会是因为不是域名而是IP地址?

是否有任何权限/限制影响向您的应用连接到的网络发出 HTTP 请求?

var ssid = 'ssid';
var password = 'password';

WifiConnectionStatus connectionStatus = await WifiConfiguration.connectToWifi(ssid, password, "com.appname");

switch (connectionStatus) {
  case WifiConnectionStatus.connected:
    var response = await http.get('http://192.168.80.1:4443/'); // NOT WORKING, the response hangs when connected to the WIFI from the app.
    var response = await http.get('http://www.google.com'); // WORKING
    break;

  case WifiConnectionStatus.alreadyConnected:
    print("alreadyConnected");
    break;

  case WifiConnectionStatus.notConnected:
    print("notConnected"); 
    break;

  case WifiConnectionStatus.platformNotSupported:
    print("platformNotSupported");
    break;

  case WifiConnectionStatus.profileAlreadyInstalled:
    print("profileAlreadyInstalled");
    break;

  case WifiConnectionStatus.locationNotAllowed:
    print("locationNotAllowed");
    break;
}

【问题讨论】:

    标签: android flutter http android-permissions wifimanager


    【解决方案1】:

    Will,我不知道这个包,但我使用this 一个没有问题的包,它可以做你想做的事情,比如连接到 wifi 网络。 还有一个问题,它可能会对您有所帮助,因为有关于发出 HTTP 请求的更新,而出于安全原因,您应该发出 HTTPS 请求,但可以通过 solution

    修复它

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-04-18
      • 1970-01-01
      • 2020-06-26
      • 2020-05-26
      • 1970-01-01
      • 2022-10-23
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多