0

I'm writing a Flutter application which involves getting some data from an HTTP server. Here's my code:

// the local IP of my testing server
final String SERVER = 'http://192.168.1.13:5000';

class Database {
  Future<String> _getJob(int jobID) async {
    var response = http.get('$SERVER?jobID=$jobID');
    return response.body;
  }
}

Currently this is the output:

[ERROR:flutter/lib/ui/ui_dart_state.cc(177)] Unhandled Exception: SocketException: OS Error: Connection refused, errno = 111, address = 192.168.1.13, port = 41966

Obviously this is happening because there isn't anything on port 41966, but the request shouldn't be going to that port, it should be going to port 5000. Is there a different way of specifying the port?

(my async code probably isn't very good, i'm going to work on it once i've figured this out)

louis Schneider
  • 101
  • 2
  • 9

2 Answers2

0

Please specify which platform (Web, iOS, Android, Windows, Linux, mac, Flutter is amazing ) you're using. Its probably because Android/ iOS do not allow HTTP connections as they can be read by others on the same network. You need to use HTTPS.

I think for debugging, its better if you just called an API that already exists. Pick one from here: https://apilist.fun/ Then in the future, you can figure out how to get a TLS/SSL certificate assigned to your server (e.g. Using LetsEncrypt)

Alternatively you can go into the configuration of your application (platform specific config) to allow insecure connections (HTTP). For example, on Android and on iOS. However, Android and iOS default to this to make sure you consider security, not just send your data in plain text.

Note: its not actually because the server doesn't exist or the port is wrong. If the server didn't exist, it would say something like connection timed out or server unreachable. In your case, your OS/ platform immediately tells you OS Error: Connection refused, because it can see you're using HTTP.

Ben Butterworth
  • 22,056
  • 10
  • 114
  • 167
0

I had the same issue.

Try using ngrok or a similar proxy. I think the dart HTTP client picks a random port when the host is not resolvable.

If you are pushing to an mobile device for testing your app, there is probably no reason a local IP address will be resolved.

Mori Bellamy
  • 151
  • 6