Windows 10上的Flutter http发布失败连接被android模拟器阻止 [英] flutter http post fails connection blocked from android emulator on windows 10

查看:81
本文介绍了Windows 10上的Flutter http发布失败连接被android模拟器阻止的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的Flutter代码如下:

my Flutter code looks like :

var url = "http://localhost:49912/api/Account/Register";
http
    .post(url,
    headers: {"Content-Type": "application/json"},
    body: js)
    .then((response) {
  print("Response status: ${response.statusCode}");
  print("Response body: ${response.body}");
});

http响应为: [错误:flutter/lib/ui/ui_dart_state.cc(148)]未处理的异常:SocketException:操作系统错误:连接被拒绝,errno = 111,地址= localhost,端口= 37316

the http response is: [ERROR:flutter/lib/ui/ui_dart_state.cc(148)] Unhandled Exception: SocketException: OS Error: Connection refused, errno = 111, address = localhost, port = 37316

现在,我知道我的服务器代码可以在仿真器之外运行,并进行一些挖掘,然后尝试从仿真器中使用Chrome,然后尝试导航至127.0.0.1,并得到相同的错误.经过一些挖掘后,我发现我应该使用10.0.2.2(主机回送接口的特殊别名(即开发计算机上的127.0.0.1) 所以我在Chrome模拟器中输入10.0.2.2,然后查看我的IISexpress服务器.感觉很好,但随后我将uri更改为10.0.2.2:49912 因为我的应用程序正在侦听端口49912并收到400错误的请求-无效的主机名

now I know that my server code works outside of the emulator and do some digging and try using Chrome from the emulator and try navigating to 127.0.0.1 and get same error. after some digging I find I should be using 10.0.2.2 ( Special alias to your host loopback interface (i.e., 127.0.0.1 on your development machine) so I type 10.0.2.2 into emulator chrome and see my IISexpress server. so feeling good but then I change my uri to 10.0.2.2:49912 as my app is listening on port 49912 and get 400 Bad Request - Invalid Hostname

痛苦

推荐答案

错误的最可能解决方法

尝试http://192.168.43.84:49912/api/Account/Register

代替http://localhost:49912/api/Account/Register

本地主机必须替换为您的系统IP地址

localhost must be replaced with your system ip address

要获取系统ip,只需转到cmd并键入ipconfig

这篇关于Windows 10上的Flutter http发布失败连接被android模拟器阻止的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆