1

I'm having an issue while making an HTTP post request in order send the data to My SQL via php as an API. http and dio both throws same error.

I have a php file register_user.php file which connects db and execute insert query. I tested this file using url parsing it works fine and the url is

http://pro-connect.infinityfreeapp.com/register_user.php

Function userRegistration() needs to do a post request to the above link with some data.

Future userRegistration() async {
    String name = nameController.text;
    String email = emailController.text;
    String password = passwordController.text;

    var url = Uri.parse('http://pro-connect.infinityfreeapp.com/register_user.php');
    var data = {'name': name, 'email': email, 'password': password};

    var response = await http.post(url, body: data);
    var message = '${response.body}';
}

updated AndroidManifest.xml as

<application
  android:label="reg_login_demo"
  android:icon="@mipmap/ic_launcher"
  android:usesCleartextTraffic="true"
>

Updated : Without 3rd party package

Future userRegistration() async {
    String name = nameController.text;
    String email = emailController.text;
    String password = passwordController.text;

    var url = Uri.parse('http://pro-connect.infinityfreeapp.com/register_user.php');
    var data = {'name': name, 'email': email, 'password': password};

    final client = HttpClient();
    final request = await client.postUrl(url);
    request.headers.set(HttpHeaders.contentTypeHeader, "application/json;charset=UTF-8");
    request.write(data);

    final response = await request.close();

    response.transform(utf8.decoder).listen((contents) {
      print(contents);
    });
}

Error

E/flutter (14533): [ERROR:flutter/lib/ui/ui_dart_state.cc(186)] Unhandled Exception: HttpException: Unexpected response (unsolicited response without request).
E/flutter (14533): #0      new _HttpClientConnection.<anonymous closure> (dart:_http/http_impl.dart:1824:9)
E/flutter (14533): #1      _rootRunUnary (dart:async/zone.dart:1362:47)
E/flutter (14533): #2      _CustomZone.runUnary (dart:async/zone.dart:1265:19)
E/flutter (14533): #3      _CustomZone.runUnaryGuarded (dart:async/zone.dart:1170:7)
E/flutter (14533): #4      _BufferingStreamSubscription._sendData (dart:async/stream_impl.dart:341:11)
E/flutter (14533): #5      _DelayedData.perform (dart:async/stream_impl.dart:591:14)
E/flutter (14533): #6      _StreamImplEvents.handleNext (dart:async/stream_impl.dart:706:11)
E/flutter (14533): #7      _PendingEvents.schedule.<anonymous closure> (dart:async/stream_impl.dart:663:7)
E/flutter (14533): #8      _rootRun (dart:async/zone.dart:1346:47)
E/flutter (14533): #9      _CustomZone.run (dart:async/zone.dart:1258:19)
E/flutter (14533): #10     _CustomZone.runGuarded (dart:async/zone.dart:1162:7)
E/flutter (14533): #11     _CustomZone.bindCallbackGuarded.<anonymous closure> (dart:async/zone.dart:1202:23)
E/flutter (14533): #12     _rootRun (dart:async/zone.dart:1354:13)
E/flutter (14533): #13     _CustomZone.run (dart:async/zone.dart:1258:19)
E/flutter (14533): #14     _CustomZone.runGuarded (dart:async/zone.dart:1162:7)
E/flutter (14533): #15     _CustomZone.bindCallbackGuarded.<anonymous closure> (dart:async/zone.dart:1202:23)
E/flutter (14533): #16     _microtaskLoop (dart:async/schedule_microtask.dart:40:21)
E/flutter (14533): #17     _startMicrotaskLoop (dart:async/schedule_microtask.dart:49:5)
If I try to print var message
I/flutter (14533): <html>
<body>
<script type="text/javascript" src="/aes.js" ></script>
<script>function toNumbers(d){
var e=[];d.replace(/(..)/g,function(d){e.push(parseInt(d,16))});
return e
}
function toHex(){
for(var d=[],d=1==arguments.length&&arguments[0].constructor==Array?arguments[0]:arguments,e="",f=0;f<d.length;f++)e+=(16>d[f]?"0":"")+d[f].toString(16);
return e.toLowerCase()
}
var a=toNumbers("f655ba9d09a112d4968c63579db590b4"),
b=toNumbers("98344c2eee86c3994890592585b49f80"),
c=toNumbers("6a1d752b2e48122d17f6fadb07bd279c");
document.cookie="__test="+toHex(slowAES.decrypt(c,2,a,b))+"; 
expires=Thu, 31-Dec-37 23:55:55 GMT; path=/"; 
location.href="http://pro-connect.infinityfreeapp.com/register_user.php?i=1";
</script>
<noscript>This site requires Javascript to work, please enable Javascript in your browser or use a browser with Javascript support
</noscript>
</body>
</html>

At last, I am running this application in my own mobile device.

Shashank Gb
  • 902
  • 1
  • 6
  • 14

1 Answers1

0

I don't know whether it'll work, but it worked for me!

Actually, I used a free web hosting site to host the SQL where I was not able to fetch a response from the PHP file hosted there accessed with 'http package' of Flutter

Then I switched to https://www.000webhost.com/ and tried the same procedure and that worked! It may be due to the reason that they may have restricted the permission for scraping and stuff, which can be observed from the response I received repeatedly which is "Unhandled Exception: HttpException: Unexpected response (unsolicited response without request)"