Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Flutter: Not able to make HTTP-GET request

My code looks like this:

HttpClient client = new HttpClient();
  client.get('192.168.4.1', 80, '/').then((HttpClientRequest req) {
    print(req.connectionInfo);
    return req.close();
  }).then((HttpClientResponse rsp) {
  print(rsp);
});

I'm trying to make a HTTP-Get request in the local wifi-network, that has no internet-connection, but I always get the following Error:

E/flutter ( 8386): [ERROR:flutter/shell/common/shell.cc(184)] Dart Error: Unhandled exception: E/flutter ( 8386): SocketException: Connection failed (OS Error: Network is unreachable, errno = 101), address = 192.168.4.1, port = 80 E/flutter ( 8386): #0 _rootHandleUncaughtError. (dart:async/zone.dart:1112:29) E/flutter ( 8386): #1 _microtaskLoop (dart:async/schedule_microtask.dart:41:21) E/flutter ( 8386): #2 _startMicrotaskLoop (dart:async/schedule_microtask.dart:50:5)

I'm using an android device.

like image 226
philippG777 Avatar asked Nov 25 '25 08:11

philippG777


1 Answers

Flutter

I am able to make an http GET request from Flutter like this:

String androidEmulatorLocalhost = 'http://10.0.2.2:3000';
Response response = await get(androidEmulatorLocalhost);
String bodyText = response.body;

which requires import 'package:http/http.dart';.

Server

Here is my Node.js server running locally:

const http = require('http');

const hostname = '127.0.0.1';
const port = 3000;

const server = http.createServer((req, res) => {
  res.statusCode = 200;
  res.setHeader('Content-Type', 'text/plain');
  res.end('Hello World\n');
});

server.listen(port, hostname, () => {
  console.log(`Server running at http://${hostname}:${port}/`);
});

See also

  • How to make HTTP requests in Flutter
  • Becoming a backend developer - Part 3: Connecting to the server from a mobile app
like image 174
Suragch Avatar answered Nov 27 '25 21:11

Suragch



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!