@@ -3,43 +3,46 @@ import 'package:flutter/services.dart';
33import 'package:flutter/widgets.dart' ;
44import 'package:http/http.dart' as http;
55
6+ Future <String > sendRequest (String urlString) async {
7+ if (urlString.isEmpty) {
8+ throw PlatformException (
9+ code: 'INVALID_ARGUMENT' ,
10+ message: 'URL argument is null or empty.' ,
11+ );
12+ }
13+
14+ developer.log ('Dart attempting to send request to: $urlString ' , name: 'Flutter SendRequest' );
15+
16+ try {
17+ final url = Uri .parse (urlString);
18+ final response = await http.get (url);
19+ developer.log ('Got 200 response: ${response .statusCode }' , name: 'Flutter SendRequest' );
20+ return 'success' ;
21+ } on http.ClientException catch (e) {
22+ developer.log ('Request completed with a non-2xx status code.' , name: 'Flutter SendRequest' , error: e);
23+ return 'success' ; // We don't care about status codes
24+ } catch (e, stackTrace) {
25+ developer.log (
26+ 'A network or parsing error occurred.' ,
27+ name: 'Flutter SendRequest' ,
28+ error: e,
29+ stackTrace: stackTrace,
30+ );
31+ throw PlatformException (
32+ code: 'NETWORK_ERROR' ,
33+ message: 'Failed to send request: ${e .toString ()}' ,
34+ );
35+ }
36+ }
37+
638void main () {
739 const MethodChannel channel = MethodChannel ('tech.httptoolkit.pinning_demo.flutter_channel' );
840 WidgetsFlutterBinding .ensureInitialized ();
941
1042 channel.setMethodCallHandler ((call) async {
1143 if (call.method == 'sendRequest' ) {
12- final urlString = call.arguments as String ? ;
13- if (urlString == null || urlString.isEmpty) {
14- throw PlatformException (
15- code: 'INVALID_ARGUMENT' ,
16- message: 'URL argument is null or empty.' ,
17- );
18- }
19-
20- developer.log ('Dart attempting to send request to: $urlString ' , name: 'Flutter SendRequest' );
21-
22- try {
23- final url = Uri .parse (urlString);
24- final response = await http.get (url);
25- developer.log ('Got 200 response: ${response .statusCode }' , name: 'Flutter SendRequest' );
26- return 'success' ;
27- } on http.ClientException catch (e) {
28- developer.log ('Request completed with a non-2xx status code.' , name: 'Flutter SendRequest' , error: e);
29- return 'success' ; // We don't care about status codes
30- } catch (e, stackTrace) {
31- // Any other error:
32- developer.log (
33- 'A network or parsing error occurred.' ,
34- name: 'Flutter SendRequest' ,
35- error: e,
36- stackTrace: stackTrace,
37- );
38- throw PlatformException (
39- code: 'NETWORK_ERROR' ,
40- message: 'Failed to send request: ${e .toString ()}' ,
41- );
42- }
44+ final urlString = call.arguments as String ? ?? '' ;
45+ return sendRequest (urlString);
4346 }
4447 developer.log ('Unknown method call ${call .method }' , name: 'Flutter' );
4548 });
0 commit comments