16

My app does a lot of HTTP requests to a server. After installing a SSL certificate on it, the app broke.

My server is running Ubuntu with Nginx hosting PHP code (using the certificate and working) and "proxy-ing" the app server code written in NodeJS. It was working until I changed to HTTPS.

My POST Request typical usage:

var jsonRequest = //Some JSON.
Map<String, String> headers = {'Content-type': 'application/json'};
var response = await http.post(urls['auth'], body: jsonRequest,headers: headers);

The error I get:

E/flutter (25875): [ERROR:flutter/lib/ui/ui_dart_state.cc(148)] Unhandled Exception: HandshakeException: Handshake error in client (OS Error: 
E/flutter (25875):      WRONG_VERSION_NUMBER(tls_record.cc:242))

E/flutter (25875): #0      IOClient.send (package:http/src/io_client.dart:33:23)
E/flutter (25875): <asynchronous suspension>
E/flutter (25875): #1      BaseClient._sendUnstreamed (package:http/src/base_client.dart:169:38)
E/flutter (25875): <asynchronous suspension>
E/flutter (25875): #2      BaseClient.post (package:http/src/base_client.dart:54:7)
E/flutter (25875): #3      post.<anonymous closure> (package:http/http.dart:70:16)

...
George
  • 6,886
  • 3
  • 44
  • 56
Lucas Pires
  • 161
  • 1
  • 1
  • 5
  • 9
    Typically, this happens when an HTTPS client reaches an HTTP endpoint, hence not getting proper TLS at all. Make sure there is a proper TLS (HTTPS) server at the URL you use. Easy to test with curl, wget, openssl s_client or any online TLS webserver checkers. – Patrick Mevzek Aug 15 '19 at 22:59
  • Thanks. I tried with curl and got the same error. Looks like I was connecting directly to the node (HTTP) server instead of NGINX, changed the port and it went away. – Lucas Pires Aug 15 '19 at 23:17
  • In flutter 2.0 fixed same issue by passing Url(...) instead of Url.parse(...) to http.post method. – Igor Gor Apr 27 '21 at 14:15

3 Answers3

17

As @Patrick mentioned in the comments, this is the result of TLS mismatch.

General thumb rule: If the server (API) is based on https (TLS) then the client should connect using https. If the server uses http (non-TLS) then the client should use http to connect to it.

In your case, seems like the API you are trying to hit is an http type hence from your flutter app you should use:

Uri.http(baseUrl, endPointUrl)
Sisir
  • 4,584
  • 4
  • 26
  • 37
2

I think you are using https:// but havent installed SSL certificate. So try to use http:// instead of https://. This work in my case. hope also work on your side.

Ali Coder
  • 163
  • 3
  • 14
1

Add this to your android/app/src/main/AndroidManifest.xml

android:usesCleartextTraffic="true"

the result is this:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.contact_wsp">
   <application
        android:label="contact_wsp"
        android:usesCleartextTraffic="true"
        android:icon="@mipmap/ic_launcher">
        <activity
            android:name=".MainActivity"