SocketException: OS Error: Connection refused, errno = 111 in flutter using django backend
SocketException: OS Error: Connection refused, errno = 111 in flutter using django backend
我正在使用 django rest-framework 构建一个 flutter 应用程序。注册 api 在 Postman 中运行良好,但在从 flutter 应用程序成功注册后,它显示上述错误。请求已发送到 https 地址。
删除了 csrf。没有任何反应。
要求:
var data = {'email':signupemailidcontroller.text,
'password1':passwordcontroller.text,
'password2':confirmpasswordcontroller.text,
};
//http request here
await http.post(websitesignupurl,
headers: headers,
body: json.encode(data))
.then((onResponse){
print(onResponse.body);
}).catchError((onerror){
print(onerror.toString());
});
控制台输出:
SocketException: OS 错误:连接被拒绝,errno = 111
我希望此请求的响应是包含用户和令牌的 Json 对象。
Harnish,需要更多详细信息才能调试此错误。
- 您是 运行 本地服务器还是与远程服务器通信?
- 你是 运行 Android 模拟器上的应用程序吗?
可能的解决方案:
如果您是 运行 本地服务器并使用 Android 模拟器,那么您的服务器端点应该是 10.0.2.2:8000
而不是 localhost:8000
,因为 AVD 使用 10.0.2.2
作为主机环回接口的别名(即)localhost
期货注意事项
我注意到上面的代码使用了 await 然后在同一行。这可能会造成混淆,需要明确的是,await
用于暂停执行直到未来完成,而 then
是在未来完成后执行的回调函数。同样可以写成下面
void myFunction() async {
var data = {};
var response = await http.post(URL, headers:headers, body:data);
if (response.statusCode == 200) {
print(reponse.body);
} else {
print('A network error occurred');
}
}
或非async/await方法
void myFunction() {
var data = {};
http.post(URL, headers:headers, body:data)
.then((response) => print(response.body))
.catchError((error) => print(error));
}
有关 Dart 中期货的更多详细信息,请阅读
https://www.dartlang.org/tutorials/language/futures
尝试将 <uses-permission android:name="android.permission.INTERNET"/>
添加到您的清单中。这为我解决了这个问题。
除了Rohan的回答你还可以阅读:
我试图 运行 使用本地服务器在物理设备上运行我的应用程序,因此,为此我不得不使用下面提到的命令,请记住根据您自己的端口号更改端口号
adb reverse tcp:3001 tcp:3001
As @maheshmnj pointed that this command is redirecting your phone's
port 3001 to your computer's port 3001. In case if you want to
redirect port 2000 of your phone to port 3000 of your computer then
you could do this adb reverse tcp:2000 tcp:3000
您还可以通过下面提到的页面阅读有关 adb 和网络的信息
https://developer.android.com/studio/command-line/adb.html
- 如果在 localhost 而不是 localhost 使用您网络的 IP
- 提示:在 cmd 运行
ipconfig
中查找 Ip 并获取 ipv4
1:示例:
await http.post("http://127.68.34.23/wp-json/...",
headers: headers,
body: json.encode(data))
如果在服务器中写 url 中的 main url
我用 $ adb tcpip <port-your-local-server>
解决了这个问题
这是我必须更改以修复我的案例中的相同错误:
- 使用
python manage.py runserver 0:8000
而不仅仅是 python manage.py runserver
启动 django(0:8000 是 0.0.0.0:8000 的缩写并使您的 Web 应用程序在本地网络上可见 - 请参阅 https://docs.djangoproject.com/en/3.0/ref/django-admin/#runserver)
- 在你的 flutter 应用程序中使用你的 pc 本地 ip 指定服务器地址,即而不是 http://localhost:8000 使用像 http://192.168.1.2:8000 这样的东西。如果在 Windows 上,则在终端中检索您当前的 IP 地址 运行
ipconfig
,否则 ifconfig
.
- 如有必要,在 django 设置中更新允许的主机。例如设置
ALLOWED_HOSTS = ['*', ]
(但仅限于您的开发环境!)
您需要做的是获取您的 public PC IP 并在您的 URL 中使用它,例如:
http://192.168.0.67:3000,其中 3000 是端口号
我去了终端并使用了ifconfig
命令
我得到了我的 IP 地址:192.168.0.109
然后我在 flutter app http 请求中简单地将这个 IP 地址替换为我的“localhost”。而且效果非常好!
我曾经遇到过同样的问题,
在经历了一些过于复杂的 Whosebug 答案和 google 搜索之后,我终于弄明白了,
在这种情况下,您很可能使用的是 android 模拟器,因此只需在模拟器上打开移动数据,然后再试一次,我相信这次它会起作用。
我在真实设备上遇到了同样的问题 运行,仅在 API link 中更改 IP 不起作用。
我的解决方案是将服务器 运行 置于以下命令中:
manage.py runserver 0.0.0.0:8000
然后将您的机器 IP 传递给 API link 而不是 localhost,如下例所示:
http://192.168.0.67:8000
我在 flutter with spring 后端遇到了同样的问题。而不是使用
'http://localhost:8080/' 在我的请求中,我将其更改为 'http://143.235.7.641:8080/',其中“143.235.7.641”是我的 Wi-Fi IPv4 地址,并且有效。
(注意:我假设您正在物理 phone 中测试 flutter,并且 phone 和计算机连接相同 wi-fi。)
在 C#[.net core api] 中,转到属性选项卡下的 launchSettings.json 并将以下代码更改为此(在 applicationURl 上将 localhost 更改为 0.0.0.0)和应用程序( flutter),使用你的本地 IPV4 IP。例如:(192.169.0.10)
"coreWebAPI": {
"commandName": "Project",
"launchBrowser": false,
"launchUrl": "api/values",
"applicationUrl": "http://0.0.0.0:5000",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
}
如果您在 url 中使用 'localhost',请将其更改为您的 ipv4 地址,这样就可以了
final response = await client.post(
"http://192.xxx.xx.xx/api/signin",
headers: {"Authorization": apiKey},
body: jsonEncode({
"username": username,
"password": password,
}));
即使我遇到了 SocketException 错误,但我替换了我的 IP 地址并且没有提及任何端口号并且它对我有用,如果我提及端口号它会抛出错误。
希望这会成功。
我正在使用 Flask 作为后端,使用 blocs。
当端点不正确或您的主机和服务器后端(示例 Laravel API)不共享相同的 IP 地址时,会出现此问题。
解决方案是。
- 将您的工作区(计算机、模拟器、设备)连接到同一平台
网络。
- 通过在终端
ifconfig
上输入来查找您的 IP。
- 如果你用Laravel构建你的API,启动你电脑
IP ADDRESS
指定的服务器,sudo php artisan serve --host YOUR_COMPUTER_IP_ADDRESS --port NUMBER
示例:
`sudo php artisan serve --host 192.168.1.11 --port 80`
- 在 Postman 或类似应用程序中测试您的端点并输入
http://192.168.1.11:80/Your_Route
- 如果一切正常,您可以 运行 使用应用程序。去吧!!!
在我的机器上有效的解决方案
[√] Flutter (Channel stable, 2.5.3, on Microsoft Windows [Version 10.0.19043.1288], locale en-US)
[√] Android toolchain - develop for Android devices (Android SDK version 31.0.0)
[√] Chrome - develop for the web
[√] Android Studio (version 2020.3)
[√] VS Code (version 1.61.2)
[√] Connected device (3 available)
步骤:1(获取您的 ipv4)
在terminal/cmd中输入
ifconfig
对于 Mac/Linux/Unix
ipconfig
对于 Windows
你会得到一些结果 windows 看起来像
第 2 步(更改您的要求 url)
❌ 不做
http.get('localhost/my-local-rest-api/etc'
127.0.0.1//my-local-rest-api/etc
.
✅ 做
- for android
http.get('10.0.2.2/my-local-rest-api/etc
- for ios
http.get('192.168.8.155/my-local-rest-api/etc
重要
⚠ 总是传递 Uri 而不是原始字符串
示例:http.get(Uri.parse('my-url-in-raw-string'))
第 3 步:启用未加密的网络连接
对于Android
在android/app/src/debug/AndroidManifest.xml
<manifest ...other params...>
// STEP: 1 make sure you have INTERNET permission
<uses-permission android:name="android.permission.INTERNET" />
<application
android:label="YourAppName"
android:usesCleartextTraffic="true"> <-- STEP: 2 add this line
<activity
</application>
</manifest>
对于iOS
在ios/Runner/Info.plist
中添加这个
<key>NSAppTransportSecurity</key>
<dict>
<key>NSAllowsLocalNetworking</key>
<true/>
</dict>
mac 用户查看 ip 地址
在终端类型中:ipconfig getifaddr en0
并使用如下 IP 地址
import 'dart:io';
import 'package:http/http.dart' as http;
import 'package:fluttertoast/fluttertoast.dart';
class AuthAction {
static String BASE_URL = 'http://192.168.0.105:27017/';
static Future<Null> signUpAction(
firstName, lastName, email, password, userName) async {
try {
http.Response res = await http.post( Uri.parse(BASE_URL + 'signUp' ));
print(res);
if (res.statusCode == 200) {
}
} catch (err) {
print(err);
// Fluttertoast.showToast(msg: 'err');
}
}
}
Note: I did not touch the port on which my mongo-node server is running. Which is 27017
尝试这些 link 延迟功能,您将不会收到此错误
Future.delayed(const Duration(milliseconds: 500), () {
widget.callback();
});
如果您是 运行 真实 phone,那么即使您将本地主机地址添加到允许的主机,您也会收到此错误。
对我来说,通过 python manage.py runserver 将我的笔记本电脑连接到 phone 的热点和 运行 django 服务器以及笔记本电脑的 ipv4 地址(您可以使用 ipconfig 命令找到) 192.168..:8000 即你的 ipv4 address:port 号码,你也可以使用 phone 的浏览器访问 django 服务器,现在添加这个您允许的主机的 ipv4 地址。并且,将您的 url 从 localhost 或 127.0.0.1 更改为您的 ipv4 地址。我希望这能解决您的问题。
将您的 URL 替换为:
Uri.parse("YourURL").replace(host: "YourIPAddress");
解释:这个错误意味着你的host-address不正确(正如上面答案中已经指出的那样)。由于您可能有一个物理设备,它必须访问您连接到计算机的模拟器服务器(也就是您的 IP 地址)。
如何获取我的IP-Adress?
MAC: ifconfig | grep "inet " | grep -Fv 127.0.0.1 | awk '{print }'
WINDOWS: netsh interface ip show address | findstr "IP Address"
我知道这个问题太老了,但我会回答这个问题,因为这可能对遇到这个问题的人有帮助。
没有更多细节,我无法给出具体的解决方案。但具备以下条件可以给出具体的解决方案
- 我想 API 主机在本地计算机上
- 我想您将模拟器用于 运行 应用程序
你用邮递员试过的URL可能是这样的http://127.0.0.1:[port number]/register
或http://localhost:[port number]/register
。但这可能不适用于模拟器,因为它无法访问那个 URL。要访问您的本地主机,您需要找出主机地址。
按照以下步骤获取您的主机地址。
- 单击 android 模拟器中的三个点(更多)
- 设置 -> 代理
- 如果您愿意,可以通过选择“手动代理配置”来更改您的主机地址
然后你可以看到你的host name
和port number
。然后将 URL 替换为 http://10.0.0.2:5000/register
.
我正在使用 django rest-framework 构建一个 flutter 应用程序。注册 api 在 Postman 中运行良好,但在从 flutter 应用程序成功注册后,它显示上述错误。请求已发送到 https 地址。
删除了 csrf。没有任何反应。
要求:
var data = {'email':signupemailidcontroller.text,
'password1':passwordcontroller.text,
'password2':confirmpasswordcontroller.text,
};
//http request here
await http.post(websitesignupurl,
headers: headers,
body: json.encode(data))
.then((onResponse){
print(onResponse.body);
}).catchError((onerror){
print(onerror.toString());
});
控制台输出:
SocketException: OS 错误:连接被拒绝,errno = 111
我希望此请求的响应是包含用户和令牌的 Json 对象。
Harnish,需要更多详细信息才能调试此错误。
- 您是 运行 本地服务器还是与远程服务器通信?
- 你是 运行 Android 模拟器上的应用程序吗?
可能的解决方案:
如果您是 运行 本地服务器并使用 Android 模拟器,那么您的服务器端点应该是 10.0.2.2:8000
而不是 localhost:8000
,因为 AVD 使用 10.0.2.2
作为主机环回接口的别名(即)localhost
期货注意事项
我注意到上面的代码使用了 await 然后在同一行。这可能会造成混淆,需要明确的是,await
用于暂停执行直到未来完成,而 then
是在未来完成后执行的回调函数。同样可以写成下面
void myFunction() async {
var data = {};
var response = await http.post(URL, headers:headers, body:data);
if (response.statusCode == 200) {
print(reponse.body);
} else {
print('A network error occurred');
}
}
或非async/await方法
void myFunction() {
var data = {};
http.post(URL, headers:headers, body:data)
.then((response) => print(response.body))
.catchError((error) => print(error));
}
有关 Dart 中期货的更多详细信息,请阅读 https://www.dartlang.org/tutorials/language/futures
尝试将 <uses-permission android:name="android.permission.INTERNET"/>
添加到您的清单中。这为我解决了这个问题。
除了Rohan的回答你还可以阅读:
我试图 运行 使用本地服务器在物理设备上运行我的应用程序,因此,为此我不得不使用下面提到的命令,请记住根据您自己的端口号更改端口号
adb reverse tcp:3001 tcp:3001
As @maheshmnj pointed that this command is redirecting your phone's port 3001 to your computer's port 3001. In case if you want to redirect port 2000 of your phone to port 3000 of your computer then you could do this
adb reverse tcp:2000 tcp:3000
您还可以通过下面提到的页面阅读有关 adb 和网络的信息 https://developer.android.com/studio/command-line/adb.html
- 如果在 localhost 而不是 localhost 使用您网络的 IP
- 提示:在 cmd 运行
ipconfig
中查找 Ip 并获取 ipv4
1:示例:
await http.post("http://127.68.34.23/wp-json/...",
headers: headers,
body: json.encode(data))
如果在服务器中写 url 中的 main url
我用 $ adb tcpip <port-your-local-server>
这是我必须更改以修复我的案例中的相同错误:
- 使用
python manage.py runserver 0:8000
而不仅仅是python manage.py runserver
启动 django(0:8000 是 0.0.0.0:8000 的缩写并使您的 Web 应用程序在本地网络上可见 - 请参阅 https://docs.djangoproject.com/en/3.0/ref/django-admin/#runserver) - 在你的 flutter 应用程序中使用你的 pc 本地 ip 指定服务器地址,即而不是 http://localhost:8000 使用像 http://192.168.1.2:8000 这样的东西。如果在 Windows 上,则在终端中检索您当前的 IP 地址 运行
ipconfig
,否则ifconfig
. - 如有必要,在 django 设置中更新允许的主机。例如设置
ALLOWED_HOSTS = ['*', ]
(但仅限于您的开发环境!)
您需要做的是获取您的 public PC IP 并在您的 URL 中使用它,例如: http://192.168.0.67:3000,其中 3000 是端口号
我去了终端并使用了ifconfig
命令
我得到了我的 IP 地址:192.168.0.109
然后我在 flutter app http 请求中简单地将这个 IP 地址替换为我的“localhost”。而且效果非常好!
我曾经遇到过同样的问题, 在经历了一些过于复杂的 Whosebug 答案和 google 搜索之后,我终于弄明白了, 在这种情况下,您很可能使用的是 android 模拟器,因此只需在模拟器上打开移动数据,然后再试一次,我相信这次它会起作用。
我在真实设备上遇到了同样的问题 运行,仅在 API link 中更改 IP 不起作用。
我的解决方案是将服务器 运行 置于以下命令中:
manage.py runserver 0.0.0.0:8000
然后将您的机器 IP 传递给 API link 而不是 localhost,如下例所示:
http://192.168.0.67:8000
我在 flutter with spring 后端遇到了同样的问题。而不是使用 'http://localhost:8080/' 在我的请求中,我将其更改为 'http://143.235.7.641:8080/',其中“143.235.7.641”是我的 Wi-Fi IPv4 地址,并且有效。
(注意:我假设您正在物理 phone 中测试 flutter,并且 phone 和计算机连接相同 wi-fi。)
在 C#[.net core api] 中,转到属性选项卡下的 launchSettings.json 并将以下代码更改为此(在 applicationURl 上将 localhost 更改为 0.0.0.0)和应用程序( flutter),使用你的本地 IPV4 IP。例如:(192.169.0.10)
"coreWebAPI": {
"commandName": "Project",
"launchBrowser": false,
"launchUrl": "api/values",
"applicationUrl": "http://0.0.0.0:5000",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
}
如果您在 url 中使用 'localhost',请将其更改为您的 ipv4 地址,这样就可以了
final response = await client.post(
"http://192.xxx.xx.xx/api/signin",
headers: {"Authorization": apiKey},
body: jsonEncode({
"username": username,
"password": password,
}));
即使我遇到了 SocketException 错误,但我替换了我的 IP 地址并且没有提及任何端口号并且它对我有用,如果我提及端口号它会抛出错误。 希望这会成功。
我正在使用 Flask 作为后端,使用 blocs。
当端点不正确或您的主机和服务器后端(示例 Laravel API)不共享相同的 IP 地址时,会出现此问题。
解决方案是。
- 将您的工作区(计算机、模拟器、设备)连接到同一平台 网络。
- 通过在终端
ifconfig
上输入来查找您的 IP。 - 如果你用Laravel构建你的API,启动你电脑
IP ADDRESS
指定的服务器,sudophp artisan serve --host YOUR_COMPUTER_IP_ADDRESS --port NUMBER
示例:
`sudo php artisan serve --host 192.168.1.11 --port 80`
- 在 Postman 或类似应用程序中测试您的端点并输入
http://192.168.1.11:80/Your_Route
- 如果一切正常,您可以 运行 使用应用程序。去吧!!!
在我的机器上有效的解决方案
[√] Flutter (Channel stable, 2.5.3, on Microsoft Windows [Version 10.0.19043.1288], locale en-US)
[√] Android toolchain - develop for Android devices (Android SDK version 31.0.0)
[√] Chrome - develop for the web
[√] Android Studio (version 2020.3)
[√] VS Code (version 1.61.2)
[√] Connected device (3 available)
步骤:1(获取您的 ipv4)
在terminal/cmd中输入
ifconfig
对于 Mac/Linux/Unixipconfig
对于 Windows
你会得到一些结果 windows 看起来像
第 2 步(更改您的要求 url)
❌ 不做
http.get('localhost/my-local-rest-api/etc'
127.0.0.1//my-local-rest-api/etc
.
✅ 做
- for android
http.get('10.0.2.2/my-local-rest-api/etc
- for ios
http.get('192.168.8.155/my-local-rest-api/etc
重要
⚠ 总是传递 Uri 而不是原始字符串
示例:http.get(Uri.parse('my-url-in-raw-string'))
第 3 步:启用未加密的网络连接
对于Android
在android/app/src/debug/AndroidManifest.xml
<manifest ...other params...>
// STEP: 1 make sure you have INTERNET permission
<uses-permission android:name="android.permission.INTERNET" />
<application
android:label="YourAppName"
android:usesCleartextTraffic="true"> <-- STEP: 2 add this line
<activity
</application>
</manifest>
对于iOS
在ios/Runner/Info.plist
中添加这个
<key>NSAppTransportSecurity</key>
<dict>
<key>NSAllowsLocalNetworking</key>
<true/>
</dict>
mac 用户查看 ip 地址
在终端类型中:ipconfig getifaddr en0
并使用如下 IP 地址
import 'dart:io';
import 'package:http/http.dart' as http;
import 'package:fluttertoast/fluttertoast.dart';
class AuthAction {
static String BASE_URL = 'http://192.168.0.105:27017/';
static Future<Null> signUpAction(
firstName, lastName, email, password, userName) async {
try {
http.Response res = await http.post( Uri.parse(BASE_URL + 'signUp' ));
print(res);
if (res.statusCode == 200) {
}
} catch (err) {
print(err);
// Fluttertoast.showToast(msg: 'err');
}
}
}
Note: I did not touch the port on which my mongo-node server is running. Which is 27017
尝试这些 link 延迟功能,您将不会收到此错误
Future.delayed(const Duration(milliseconds: 500), () {
widget.callback();
});
如果您是 运行 真实 phone,那么即使您将本地主机地址添加到允许的主机,您也会收到此错误。 对我来说,通过 python manage.py runserver 将我的笔记本电脑连接到 phone 的热点和 运行 django 服务器以及笔记本电脑的 ipv4 地址(您可以使用 ipconfig 命令找到) 192.168..:8000 即你的 ipv4 address:port 号码,你也可以使用 phone 的浏览器访问 django 服务器,现在添加这个您允许的主机的 ipv4 地址。并且,将您的 url 从 localhost 或 127.0.0.1 更改为您的 ipv4 地址。我希望这能解决您的问题。
将您的 URL 替换为:
Uri.parse("YourURL").replace(host: "YourIPAddress");
解释:这个错误意味着你的host-address不正确(正如上面答案中已经指出的那样)。由于您可能有一个物理设备,它必须访问您连接到计算机的模拟器服务器(也就是您的 IP 地址)。
如何获取我的IP-Adress?
MAC: ifconfig | grep "inet " | grep -Fv 127.0.0.1 | awk '{print }'
WINDOWS: netsh interface ip show address | findstr "IP Address"
我知道这个问题太老了,但我会回答这个问题,因为这可能对遇到这个问题的人有帮助。
没有更多细节,我无法给出具体的解决方案。但具备以下条件可以给出具体的解决方案
- 我想 API 主机在本地计算机上
- 我想您将模拟器用于 运行 应用程序
你用邮递员试过的URL可能是这样的http://127.0.0.1:[port number]/register
或http://localhost:[port number]/register
。但这可能不适用于模拟器,因为它无法访问那个 URL。要访问您的本地主机,您需要找出主机地址。
按照以下步骤获取您的主机地址。
- 单击 android 模拟器中的三个点(更多)
- 设置 -> 代理
- 如果您愿意,可以通过选择“手动代理配置”来更改您的主机地址
然后你可以看到你的host name
和port number
。然后将 URL 替换为 http://10.0.0.2:5000/register
.