How to fix HttpException: Connection closed before full header was received

57,582

Solution 1

Right now this issue is opened on github https://github.com/flutter/flutter/issues/32587

Temporarily you can solve this issue by switching to physical device instead of emulator How to test android apps in a real device with Android Studio?

Decision found on github (link upward)

I've got a temporary work around, which I can use for now. The exception above is thrown when I run the emulator as a Pixel 3 Android 9.+ Target (Q API). I made up a second emulated device, a Pixel 2 Android 9.0 Target (28 API), and this target works fine.

I'm also noticing that the Pixel 2 target takes up 8.5GB on Disk, while the Pixel 3 target only takes up 1.4GB. It makes me wonder if the Pixel 3 target is missing some important things.

Solution 2

The issue is mainly with Android Q.

You can downgrade your android version from Android Q to Android Pie.

which eventually leads to resolve the issue.

Solution 3

No solution unless the Flutter guys fix it.

I'm having it on one machine (Mac OS Mojave) even when connecting physical iOS devices. Of course it happens too with emulated Android devices (no matter which one).

I've also having this problem in a different computer (MacOS Mojave too) with a physical Android device (with a very old Android version: 5.1).

flutter clean does not fix it at all in my case(s).

Solution 4

First turning off your WiFi/Bluetooth.

And run below command:

flutter clean

And then quitting or restarting your emulator.

Solution 5

I figured out how to limit the connections on the underlying HttpClient of NetworkImage!

I globally overrode the HttpClient: (ref: Override HttpClient globally)

class MyHttpOverrides extends HttpOverrides {
  @override
  HttpClient createHttpClient(SecurityContext context) {
    return super.createHttpClient(context)
        ..maxConnectionsPerHost = 5;
  }
}


void main() {
  HttpOverrides.global = MyHttpOverrides();
  runApp(MyApp());
}

Ref : Connection closed...

Share:
57,582
Juthi Sarker Aka
Author by

Juthi Sarker Aka

Updated on July 08, 2022

Comments

  • Juthi Sarker Aka
    Juthi Sarker Aka almost 2 years

    I have recently upgraded my flutter version in my app. But when I want to debug the application, it shows me the following error.

    Error connecting to the service protocol: HttpException: Connection closed before full header was received, URI = http://127.0.0.1:50795/ws

    Is there anyone facing the same issue after upgrading the flutter version? If is there any workaround, please share.

  • i6x86
    i6x86 almost 5 years
    Switching to físicas device doesn’t work for me. I have same issue at iOS and Android physical devices and on iPhone emulator.
  • nbloqs
    nbloqs almost 5 years
    This did not work for me: I've got a physical Android device working, but not the simulator, nor a physical iOS device, and the Android device in debug mode seems to work with or without WiFi/Bluetooth off.
  • Payam Khaninejad
    Payam Khaninejad almost 5 years
    @nbloqs closing wifi just makes sure that your open connection will be closed.
  • nbloqs
    nbloqs almost 5 years
    Thanks, but what I mean is that flutter clean does not fix this at all, independently of the WiFi/Bluetooth status. It really looks like a permanent problem, not something related to an open connection.
  • Aniruddha Shevle
    Aniruddha Shevle almost 5 years
    What about iOS?
  • Vicky Salunkhe
    Vicky Salunkhe over 4 years
    Sorry @AniruddhaShevle I don't have an iOS so have no clear idea about it.
  • ArifMustafa
    ArifMustafa over 4 years
    I had this issue in Visual Studio Code, So I turned off my Wifi, went to Terminal and flutter clean and then change Emulator device and again turned on the Wifi, issue resolved.
  • Sana.91
    Sana.91 over 4 years
    facing same issue on physical device, any resolution?
  • awaik
    awaik over 4 years
    try to use another emulator - for example create Pixel 3 Android 9 and try to execute app there. Also upgrade Flutter and if it is won't help try to run and check is everything is ok flutter doctor -v
  • Sana.91
    Sana.91 over 4 years
    but why facing issue on a physical android device: samsung ?
  • Sana.91
    Sana.91 over 4 years
    @awaik asked this as a seperate question stackoverflow.com/questions/58215730/…
  • kishea
    kishea about 4 years
    where did you set this?
  • Bosko Popovic
    Bosko Popovic about 4 years
    I extended the IOClient and done: class PersistentIoClient extends http.IOClient { static final http.Client _client = PersistentIoClient( HttpClient()..connectionTimeout = const Duration(seconds: 15), ); static http.Client get client => _client; static sync.Lock lock = sync.Lock(reentrant: true); PersistentIoClient([HttpClient inner]) : super(inner); @override Future<http.IOStreamedResponse> send(http.BaseRequest request) async { request.persistentConnection = false; return await super.send(request); } }
  • Dika
    Dika almost 4 years
    hi, @BoskoPopovic please write your code in a gist or github repo. I believe many people will need it, including me. Thank you
  • Muhammad Faizan
    Muhammad Faizan almost 4 years
    Not a solution, what if iOS?
  • Urvashi kharecha
    Urvashi kharecha almost 4 years
    can anyone pls tell me more about this? @BoskoPopovic
  • hio
    hio over 3 years
    did you find any solution ?
  • Chris
    Chris over 3 years
    Hi, I think this is an issue only while debugging .. I don't actually think there is an issue when running in release mode
  • Purvik Rana
    Purvik Rana over 3 years
    This did not help me out.
  • Purvik Rana
    Purvik Rana over 3 years
    Release build will also have this issue on actual devices.
  • Ajeet Singh
    Ajeet Singh over 2 years
    This works pretty well for me! Please not do not try to go higher, doesn't add any value. Try with smaller values only. In my case 2 connection per host was amazing.
  • Eric Aya
    Eric Aya over 2 years
    This is not an answer, because you could post the same thing to any question that includes using internet. This does not help anyone.
  • Hieu Vo
    Hieu Vo over 2 years
    It might be a generic answer, but it does help in this case :) the original ISP cannot connect to a specific host to download the package, and VPN helps
  • Eric Aya
    Eric Aya over 2 years
    So because your ISP is incompetent you're going to post hundreds of answers like "turn on your vpn" to all questions that mention trying to access internet? Come on...