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

19

124

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.

Wizardry answered 27/4, 2019 at 10:24 Comment(3)
flutter clean worked in my case.Houseyhousey
i ran "flutter clean" in terminal of VS code without turning off the WIFI first unfortunately. Now I have got so many errors like runApp isn't defined, all the build, .dart tool, .packages, has been deleted, . what should I do now?Schistosome
facing the same error when starting widget tests on a real devicePeracid
C
26

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.

Cenacle answered 13/5, 2019 at 14:23 Comment(7)
Switching to físicas device doesn’t work for me. I have same issue at iOS and Android physical devices and on iPhone emulator.Sponger
facing same issue on physical device, any resolution?Cyna
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 -vCenacle
but why facing issue on a physical android device: samsung ?Cyna
@Cenacle asked this as a seperate question #58216230Cyna
same on windowsPlugugly
I still get this error with my physical android mobileMabellemable
G
18

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.

Gotland answered 23/6, 2019 at 20:54 Comment(5)
What about iOS?Overhaul
Sorry @AniruddhaShevle I don't have an iOS so have no clear idea about it.Gotland
Not a solution, what if iOS?Vestigial
happens on windowsPlugugly
I have this problem with both Android and web (web on windows and iOS is same)Bohon
A
17

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).

Arlana answered 9/7, 2019 at 10:58 Comment(0)
M
14

First turning off your WiFi/Bluetooth.

And run below command:

flutter clean

And then quitting or restarting your emulator.

Matildematin answered 8/7, 2019 at 21:14 Comment(5)
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.Arlana
@Arlana closing wifi just makes sure that your open connection will be closed.Matildematin
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.Arlana
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.Lotta
Thanks, im used: flutter clean them flutter pub get and finally flutter runSpectral
L
8

This was my Problem And this worked For me:

PROBLEM
App would crash abruptly out of no where

CAUSE
I was using cached_network_image, When users uploaded heavy photos > 2MB, cache ran out of memory then app crashes. No warning.

SOLUTION
Use FadeInImage.memoryNetwork() or something else like NetworkImage() but not cache_network_image, it only works for less than 2 MB photos
thanks :)

Laywoman answered 23/7, 2021 at 6:45 Comment(3)
We can use placeholder: (context, url) => const CircularProgressIndicator(), so the problem will solve with CachedNetworkImagePosology
@AdieRT I was using it still this issue happened. I recommend everyone to use this simple package which does not depend on many other dependencies: pub.dev/packages/cached_network_image_builder The only thing which you have to implement is when snapshot has an error and to delete the cached files after a certain period of time like 10 days or so to not clutter the device space...Dariodariole
@RayZion Thank you for pointing this issue out! I wouldn't have known this... What I learned is to not depend on packages that much ...Dariodariole
R
1

I fixed the issue by setting request.persistentConnection = false; (default is true)

Writing this in case someone has the same problem...

Rafaelarafaelia answered 13/3, 2020 at 3:11 Comment(5)
where did you set this?Bithynia
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); } }Rafaelarafaelia
hi, @BoskoPopovic please write your code in a gist or github repo. I believe many people will need it, including me. Thank youGamete
can anyone pls tell me more about this? @BoskoPopovicLondalondon
Thank you so much! This solved it for me, 3 years later running Flutter 3.13.2. I'm using Dio, but the setting is the same: api.flutter.dev/flutter/dart-io/HttpClientRequest/… Code using Dio: dio.get(path, options: Options(persistentConnection: false))Bioecology
C
1

In my case (error "HttpException: Connection closed before full header was received") the call was to an https address using Microsoft Internet Information Services as backend, in the SSL settings of the website in IIS i had mistakenly set "Client certificates: Accept" instead of "Client certificates: Ignore", setting "Ignore" solved the problem.

Cardinal answered 21/3, 2021 at 19:43 Comment(0)
A
1

The issue was on the flutter code.the library socket_io_client it seems not to accept builder

OptionBuilder()
        .setTransports(['websocket'])
        .setPath('/messanging')
        // for Flutter or Dart VM

        //.setPath("/messanging")
       // .setExtraHeaders({"Authorization":"Bearer 
        ${datasource.user?.payload?.access_token}"}) // optional
        .build()

worked when i did use the below map method

socket = IO.io("http://10.18.5.212:3000/messanging", <String, dynamic>{
      "transports": ["websocket"],
      "Authorization":
          "Bearer eyJhbGciOiJIUzI1N......"
    }
Amberjack answered 19/8, 2022 at 10:3 Comment(0)
M
1

For those who run flutter clean and errors pops up on all files. Just run flutter pub get, then it will return back to normal states.

Microbicide answered 4/4, 2024 at 3:8 Comment(0)
C
0

If you faced this issue on IOS build, follow th below steps

Goto Runner -> editSchema -> Run -> change build configurations to Debug.

if you are in Release mode you will face this issue, while running on android studio.

Creuse answered 25/7, 2019 at 11:11 Comment(0)
G
0

I've faced the same problem. Actually the problem was in the native code.

You can track the problem for android if you open an your_app/android project from Android Studio and run it.

The same for iOS -> open iOS project in Xcode So after I've fixed it the app run as intended

I hope this help

Grapnel answered 7/2, 2020 at 17:37 Comment(0)
A
0

I found the solution/work around. For me, it worked on physical device but not on emulator. The simple fix is to use your an emulator of x86 environment rather than x86_64 emulator.

Accustom answered 25/2, 2020 at 12:21 Comment(1)
Release build will also have this issue on actual devices.Mariken
C
0

Like Vitaly says ..

I am also struggling to force this error at all when building through Xcode for iOS and I imagine the same goes for Android with Android studio..

For iOS do flutter build ios --release and then open/run the project in Xcode

For Android do flutter build apk --release and then open/run the project in Android Studio

It seems to be an issue when debugging through flutter ... I will update this answer if I find otherwise but it seems stable with this method

Curium answered 23/6, 2020 at 17:0 Comment(2)
did you find any solution ?Ligroin
Hi, I think this is an issue only while debugging .. I don't actually think there is an issue when running in release modeCurium
B
0

I got the same error when I using Flutter with http package.

import 'package:http/http.dart' as http;
...
http.Response response =  http.get('url')

This was fine. But when I went to get the response.statusCode, it Error connecting error.

I found that I'm using SSR/v2Ray on my pc. I fix it by setting the proxy on Android Emulator extended controls panel.

Brecciate answered 23/10, 2020 at 6:57 Comment(0)
S
0

I was getting the same issue in POST request while sending the Images as multipart Resolved by using

contentType: MediaType('image', 'jpeg') in the http.MultipartFile 

function

e.g

        request.files.add(
              http.MultipartFile(
                'image',
                stream,
                length,
                filename: basename(_image1.path),
                contentType: MediaType('image', 'jpeg'),
              ),
            );

where 'image' is the paramter name.

Don't forget to import 'package:http_parser/http_parser.dart'; for MediaType. Other Mediatype which you can use are

"image/png"
"image/jpg" 
"image/jpeg"

And I am sending this too in Header "Content-Type": "multipart/form-data"

Stander answered 26/5, 2021 at 17:52 Comment(0)
E
0

Stopping the running application and clicking on run again worked for me.

Erne answered 14/9, 2021 at 9:27 Comment(0)
M
0

You also need to mention your listening port which you define in your file like this final WebSocketChannel channel = IOWebSocketChannel.connect('wss://check.com:6043/');

It has solved my issue

Mesdemoiselles answered 26/11, 2021 at 4:34 Comment(0)
B
0

Try using Uri.parse(url); Make sure the 'url' you put here is the full url with endpoint.

Bodice answered 5/1, 2023 at 12:45 Comment(0)
E
0

Replace https in the image url with http

Image.network(
  imageUrl.replaceAll("https", "http"),
)
Eldredge answered 1/4, 2024 at 5:56 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.