Flutter WebSocketException: Connection not upgraded to websocket - flutter

i'm trying to connect to websocket server with token in header.
Due to web_socket_channel package not support headers i'm using dart WebSocket class.
I'm struggling with error in console:
"WebSocketException: Connection to 'https://URL:0/messenger#' was not upgraded to websocket<…>"
My code sample:
_url = 'wss://URL/messenger'
channel = await WebSocket.connect(
_url,
protocols: _protocols,
headers: {
"Cookie": "realtalk_auth=$token",
},
);
Also, same url in Postman with header "Cookie: realtalk_auth=token" lead to success connection

Related

Google Cloud Run TLS exception on dart

I've deployed a node.js service (REST API) in GCP Run and I'm having issues with the TLS handshake in Flutter.
Unhandled Exception: HandshakeException: Handshake error in client (OS Error: WRONG_VERSION_NUMBER(tls_record.cc:242))
The code that I'm trying to use is:
static Future<http.Response> get(String url, {Map<String, String>? headers}) async {
final _headers = await _addAuthHeader(headers); //appends the authorization header (not used right now)
final out = await http.get(Uri.parse(url), headers: _headers); //this throws the exception
return out;
}
In GCP Run I've haven't set anything about the security other than the allow unauthenticated calls so I should be using the managed TLS service.
I've tested in the following conditions:
curl https://<url> works ✅
curl --tls-max 1.2 https://<url> works ✅
curl --tls-max 1.1 https://<url> doesn't work ❌ (maybe Flutter doesn't support TLS 1.2?)
In dart (HTTPS call) using dio package doesn't work ❌
In dart (HTTPS call) using http package doesn't work ❌
In dart (HTTP call) works ✅
In chrome works ✅
I've tried the solutions proposed in these other posts:
Why i'm getting WRONG_VERSION_NUMBER when making a POST request to a HTTPS Server?
How to solve Flutter CERTIFICATE_VERIFY_FAILED error while performing a POST request?
Flutter on Android 7 CERTIFICATE_VERIFY_FAILED with LetsEncrypt SSL cert after Sept 30, 2021
System information:
Flutter version: 3.3.8
Dart version: 2.18.4
Flutter doctor: everything fine

Dio is making a lot of request when debugging with debugger

I am debugging the API call to server for authentication. When I debug in Android Studio and step into the request, network shows multiple requests being made. While not debugging, the request are happening only once, but during debugging with breakpoints, there are mutltitude of requests being made.
These are the base options and interceptors being used. The interceptor also confirms that multiple requests are being made for some reason.
Dio get dio => Dio(BaseOptions(
baseUrl:
'${getIt<ConfigReader>().baseURL}${getIt<ConfigReader>().apiPath}',
contentType: Headers.jsonContentType,
responseType: ResponseType.json,
headers: {
'Accept': Headers.jsonContentType,
},
))
..interceptors.add(LogInterceptor());
How can I resolve the issue?

Request http in flutter web

I want to post data to the server with dio library but when I send data, I get XMLHttpRequest error. The server programmer checked twice his codes and made sure to enable CORS in the server. I think this error occurs because the server is not HTTPS and the browser blocks the request. I use this code to send my data to the server:
Response response = await client
.post(theUrl,
options: Options(
validateStatus: (status) {
return status! < 500;
},
followRedirects: false,
headers: {
HttpHeaders.contentTypeHeader: "application/json",
HttpHeaders.acceptHeader: "*/*",
HttpHeaders.accessControlAllowOriginHeader:
"Access-Control-Allow-Origin, Accept",
}),
data: convert.json.encode(data))
.timeout(const Duration(seconds: 10));
My log is just:
DioError [DioErrorType.RESPONSE]: XMLHttpRequest error.
So, How can I ignore the certificate of the server host and send data to an HTTP URL in flutter web?
I'm almost sure that the problem is because your server doesn't have an https certificate.
A workaround is run / debug your application with flutter desktop.
You can also take a look at: How to solve flutter web api cors error only with dart code?

flutter app can not connect to a webSocket

I am building a flutter app which needs to connect to the server and exchange data using websocket. The server is in JAVA and using SockJs and Stomp to implement this functionality.
I am using Stomp dart client and webSocket packages from pub.dev/packages.
This is the part of my code where I am trying to connect :
clientConnect() async {
String cookie = await storage.read(key: "cookie");
final stompClient = StompClient(
config: StompConfig(url: 'ws://192.168.0.13:8080/....', onConnect: onConnect,
webSocketConnectHeaders: {"cookie": cookie }));
stompClient.activate();
}
The problem I am facing is, my flutter app is not able to connect to the server and throws the this error.
WebSocketException: Connection to 'http://192.168.0.12:8080/....' was not upgraded to websocket
Late answer (maintainer of the stomp package):
Since you seem to be using StompJS on the java-side you need to use the special StompJS config in the client. Here is the example from the documentation:
https://pub.dev/packages/stomp_dart_client#use-stomp-with-sockjs
StompClient client = StompClient(
config: StompConfig.SockJS(
url: 'https://yourserver',
onConnect: onConnectCallback
)
);

LDAP authentication fails on ripple and actual device but not on browser

I'm trying to get an authorization token for an Ionic App from a LDAP service in a remote server.
I can get the auth token when I run the Ionic App in the browser with the command ionic serve and when I use Postman,
BUT it takes lot of time and eventually fails when I debug using ripple for the App or when I test on the phone or tablet.
The error says:
status: 503
statusText: Service Unavailable
data: html code from http://s3.amazonaws.com/heroku_pages/error.html
var deferred = $q.defer();
var req = {
method: 'GET',
url: 'http://host:port/adap?bind=token',
headers: {
Authorization: 'Basic <username>:<password>'
}
};
$http( req )
.then(function(data, status, headers, config) {
console.log(data);
deferred.resolve(data.data);
})
.catch(function(data) {
console.error(data.data);
deferred.reject(err);
});
return deferred.promise;
Does anybody have some hint about this issue?
Thanks in advance
For what you say the service is available, so the problem must be in the app side.
Check IP tables, and check ripple's proxy and set it to none.