flutter http post fails connection blocked from android emulator on windows 10

十年热恋 提交于 2019-12-11 14:54:36

问题


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}");
});

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

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

painful


回答1:


finally figured it out as tried calling my service using 127.0.0.1 from Postman and got the same bad request. turnsout IIS Express is listening on localhost so needs to be convinced to listen on 127.0.0.1 by editing the
applicationhost.config file in the .vs folder in your solution. add the following line <binding protocol="http" bindingInformation="*:49912:127.0.0.1" />




回答2:


The most possible resolution for the error maybe

Try http://192.168.43.84:49912/api/Account/Register

Instead of http://localhost:49912/api/Account/Register

localhost must be replaced with your system ip address

for getting system ip simply go to cmd and type ipconfig



来源:https://stackoverflow.com/questions/56177444/flutter-http-post-fails-connection-blocked-from-android-emulator-on-windows-10

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!