解决 Flutter 连接本地服务协议失败的问题
在开发 Flutter 项目时,遇到了以下错误:
Error connecting to the service protocol: failed to connect to http://127.0.0.1:60928/N_QbyYL3NXA=/ HttpException: Connection closed before full header was received, uri = http://127.0.0.1:60928/N_QbyYL3NXA=/ws
该错误表明 Flutter 工具无法连接到本地服务协议,导致调试和运行失败。
通过日志和排查,发现问题的可能原因包括:
HTTP_PROXY
和 HTTPS_PROXY
,导致 Flutter 工具尝试通过代理连接本地服务。60928
可能被其他进程占用。输出显示powershellGet-ChildItem Env: | Where-Object { $_.Name -match "PROXY" }
HTTP_PROXY
和 HTTPS_PROXY
指向 http://127.0.0.1:7890
。powershell[System.Environment]::SetEnvironmentVariable("HTTP_PROXY", "", [System.EnvironmentVariableTarget]::User) [System.Environment]::SetEnvironmentVariable("HTTPS_PROXY", "", [System.EnvironmentVariableTarget]::User)
NO_PROXY
,确保本地请求不会被代理拦截:
powershell[System.Environment]::SetEnvironmentVariable("NO_PROXY", "127.0.0.1,localhost", [System.EnvironmentVariableTarget]::User)
powershellflutter clean flutter pub get flutter run
60928
是否被其他进程占用:
powershellnetstat -ano | findstr :60928
powershelltaskkill /PID <PID> /F
powershellInvoke-WebRequest -Uri "http://127.0.0.1:60928"
通过以上步骤,成功解决了 Flutter 连接本地服务协议失败的问题。项目可以正常启动和调试。
NO_PROXY
包含 127.0.0.1
和 localhost
,并清除不必要的代理配置。本文作者:yowayimono
本文链接:
版权声明:本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!