Reputation: 147
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.
Upvotes: 3
Views: 6111
Reputation: 511616
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';
.
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}/`);
});
Upvotes: 2