HTTP request to 127.0.0.1:80 fails - dart

Environment
Android Studio latest release
Android Emulator running Android Nougat
MAMP to serve as HTTP server
Problem
My simple method
Future<String> _getSimpleReply( String command, callback, errorCallback ) async {
try {
HttpClientRequest request = await _myClient.get( '127.0.0.1', 80 '/' );
HttpClientResponse response = await request.close();
await callback( response.toString() );
} on SocketException catch( e ) {
errorCallback( e.toString() );
}
}
fails with an error:
cbLoginFailed :: SocketException: OS Error:
Connection refused, errno = 111, address = 127.0.0.1, port = 46414
Currently using the Android emulator, which has been configured for internet access:
<uses-permission android:name="android.permission.INTERNET"/>
For test purpose, the HTTP server is MAMP, which executes the index.php script and returns some simple string, if I enter this link in a browser:
http://127.0.0.1/index.php
The situation does not change, if I replace 127.0.0.1 with localhost.
But if I do change 127.0.0.1 with a valid public FQDN, the method works just fine:
doLoginSuccess :: Instance of '_HttpClientResponse'
Question
Why does the android emulator / flutter / dart handles 127.0.0.1 different than a
valid FQDN?
And how to I change this behaviour?

As pointed out by the first comment, localhost or 127.0.0.1 is - when running an app on Android emulator - in fact the local machine on which the Android app is executing. With respect of the Android app, the local machine is the emulated Android system.
Therefore, the http request from the flutter code doesn't leave the emulator at all. Since the Flutter app doesn't provide a HTTP Server, the emulator refuses the http request.
Need to change to the address of the host machine, on which the emulator is executing.
The Android emulator uses special IP addresses to communicate with its host or the host gateway.
Habe a look in the Android documentation.
E.g., the host‘s loopback Interface is identified by this IP: 10.0.2.2
So, if normally one would use 127.0.0.1:80, one can substitute it with 10.0.2.2:80.

You are trying to access localhost which is your phone/emulator not your PC which I assume is running your test server.
You need to create a network between your PC and phone and use the local IP address instead

Related

How to connect to localhost on a real device in react native on iOS?

I have a server running on localhost:5000 on my desktop, and when I make http requests through my react-native app when running on the iOS simulation to it, it works. However, when I connect to a real device, the http requests don't work. What am I doing wrong? Is there something I have to configure?
localhost in your desktop is not the same ip of the localhost of your device.
Get the ip of your computer by typing ifconfig in a terminal, then replace localhost by this ip, you should type something like that in your device browser:
192.168.0.23:5000
Maybe my answer is too late but I ran into a similar situation today and I got it to work by running my node server using the IPv4 of my desktop and then making the request to that IP.
For example, I am running my nodejs development server at http://192.168.0.135:3000(local IP of my desktop) instead of localhost and then I make the fetch API request from my react-native app to http://192.168.0.135:3000/.
Just make sure your nodejs server and your app are on the same local network. My react-native server (metro bundler) is running on a separate PC also in the same wifi network.
If your are on macOS go to settings -> Wifi / and show more,
on bottom you see "IP address", it's your correct localhost for ios physical device !

Connect from ios device to localhost

I have a REST service running on my host. I can perform an HTTP request to this service from my flutter app when running on the iOS simulator using the code below:
final response = await http.get('http://localhost:8085/my/service');
However, it fails when I'm running the app on my iOS device. My device is connected via USB.
Runner[574:66922] flutter: Error SocketException: OS Error: Connection refused, errno = 61, address = localhost, port = 49575
What do I need to configure to connect to localhost from my iOS device?
I've already disabled in iTunes -> Summary "Sync with this iPad over Wi-Fi".
instead of localhost you can use your current ip
to get it In the command window type: ipconfig
Note that you must be on the same network

How can I access my localhost on my computer from my Android device?

I have a web application written in ASP.NET MVC 5. I simply can open it from the browser with this localhost:14920 in my computer, but I want to access the same application from my Android device's browser while my laptop and Android device are in the same network. I don't know how to access it from Android.
Note: when I type 127.0.0.1 in the Android browser I can access the IIS server, but when I type 127.0.0.1:14920 it prints Bad Request - Invalid Hostname. How can I fix this issue?
You can configure IISExpress for remote access
http://www.ryadel.com/en/iis-express-allow-external-requests-remote-clients-devices/
or just change the hosting to IIS as the earlier commenter mentioned.
Try with replacing 127.0.0.1 with your IPv4 address.
I have used ngrok in the past, when needing to hit my development machine, or have someone else hit it. It is a very useful tool and very simple to install.
Once you download it, it's just a simple command line:
https://ngrok.com/docs#expose
ngrok http 80
(That is for traffic on port 80) Since your port above looks different it would be:
ngrok http 14920
That will give a URL that you can hit or any machine or device.

Xamarin VS2015 ios app connect to web app api

I have a iOS app that I am building in Visual Studio 2015 with Xamarin. I also have a Web Application with an Api, no authorisation. But when I try to connect to the api I get an error saying "Connection Refused".
I call the api through the following code:
var client = new HttpClient();
var data = await client.GetAsync("http://localhost:7074/api/values/get");
Config your Web API with an IP Address, say local 192.168.x.x
And replace localhost by that IP
var data = await client.GetAsync("http://191.268.x.x:7074/api/values/get");
localhost will point to your iOS device/emulator, not the server. You need to specify the IP or FQDN in the url. Also be be sure the firewall is open and that IIS is setup to accept remote connections.
localhost is your iPhone / iPad or simulator (the device where you run your app). You have to change it to the IP/Domain of your server.
If the server is your dev machine, ensure, that your phone is connected to the same network (e.g. via WiFi), else you might have the same problem, because your dev machine might not be reachable.
You can find the IP of your dev machine by typing ipconfig | find "IPv4" into the console (on Windows).

Connect to local web service (running under MAMP) from iPhone via WIFI.

I have setup a local REST webservice with MAMP.
I have setup a virtual host with the url "http://aem.davidcasillas" pointing to my public folder.
MAMP is listening on port 80.
If I access via Safari to "http://aem.davidcasillas/webservice/rest/getEventosToday" I get the correct json data.
Now I'm trying to access this web service from my iPhone application. I run it from XCode into the device.
The device is connected to my local network via WIFI, and the connection is working (I can load webpages in safari)
I'm using RestKit to download the data and I'm trying to reach it with the same url I use in my Mac: "http://aem.davidcasillas.es/webservice/rest/getEventosToday".
In the device I get the error:
NSURLErrorDomain -1003 Found no server for the specified host name.
How should I write my url so I can reach the web service from the iPhone?
NOTE: I have read this post: test local websites with mamp on iphone? but none of this problems apply to my case. I have firewall disabled and my config.ini file shows the following listen line:
Listen 80
I have found a workaround.
I use the WIFI ip address in SystemPreferences->NetWork in my Mac and then the whole path to the resource, so instead of:
"http://aem.davidcasillas.es/webservice/rest/getEventosToday"
I am writing:
"http://192.168.1.6/aem.davidcasillas.es/public/webservice/rest/getEventosToday"
The 192.168.1.6part takes me to my local installation root folder /Applications/MAMP/htdocs/.
In the rest of the path I have to put the whole path to the resource. Notice that I have to include the public folder in the path. Hope I could use my virtual host setup, but have not found a way.
When I run hostname, it prints out
> hostname
ddopson.local
You can edit this in System Preferences ==> Sharing ==> Computer Name(text box at the top). Note that in the "computer name" textbox, I filled in "ddopson".
If you are on WiFi, you should be able to connect to that hostname (ddopson.local) from your iPhone. This works because both iOS and your Mac support Multicast DNS where a multi-cast UDP packet is emitted onto the L2 network to lookup local hostnames. When your Mac hears this multi-cast packet, it will reply to your iPhone with its ip-address and off you go.

Resources