Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why i'm getting WRONG_VERSION_NUMBER when making a POST request to a HTTPS Server?

Tags:

ssl

flutter

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)

...
like image 708
Lucas Pires Avatar asked Aug 15 '19 22:08

Lucas Pires


3 Answers

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)
like image 54
Sisir Avatar answered Nov 10 '22 05:11

Sisir


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"
like image 32
Brando Palacios Gutierrez Avatar answered Nov 10 '22 07:11

Brando Palacios Gutierrez


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.

like image 35
Ali Coder Avatar answered Nov 10 '22 07:11

Ali Coder