我正在为我的公司制作一个应用程序,我需要它在专用网络(VPN)上工作,但是当我在VPN上无法正常工作时,尝试打印结果witch应该给我一个json代替给我这个“
401-未经授权:由于无效而拒绝访问 凭据。
”,但是如果将URL更改为“ [https://randomuser.me/api/?results=15][1]”,则可以毫无问题地获取数据。 在使用VPN时,我尝试检查连接性,但没有结果。
...
class _MyHomePageState extends State<MyHomePage> {
var _connection = 'Unknown';
Connectivity connectivity;
StreamSubscription<ConnectivityResult> subscription;
String url = 'myUrl';
List data;
Future<String> makeRequest() async {
var response = await http
.post(url, headers: {'Accept': 'application/json'});
print('________________________________________________________________________^^^^ '+ response.body );
setState(() {
var extractdata = json.decode(response.body);
//data = extractdata;
//print( data[0]["name"]["first"]);
});
}
@override
void initState() {
super.initState();
connectivity = new Connectivity();
subscription = connectivity.onConnectivityChanged.listen( (ConnectivityResult result){print('RESULT');print(result);});
this.makeRequest();
}
@override
void dispose(){
subscription.cancel();
super.dispose();
}
...
这是控制台结果
Performing hot restart...
Syncing files to device SM J500FN...
Restarted application in 5 173ms.
I/flutter (16502): RESULT
I/flutter (16502): ConnectivityResult.none
I/flutter (16502): ________________________________________________________________________^^^^ <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
I/flutter (16502): <html xmlns="http://www.w3.org/1999/xhtml">
I/flutter (16502): <head>
I/flutter (16502): <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"/>
I/flutter (16502): <title>401 - Unauthorized: Access is denied due to invalid credentials.</title>
I/flutter (16502): <style type="text/css">
I/flutter (16502): <!--
I/flutter (16502): body{margin:0;font-size:.7em;font-family:Verdana, Arial, Helvetica, sans-serif;background:#EEEEEE;}
I/flutter (16502): fieldset{padding:0 15px 10px 15px;}
I/flutter (16502): h1{font-size:2.4em;margin:0;color:#FFF;}
I/flutter (16502): h2{font-size:1.7em;margin:0;color:#CC0000;}
I/flutter (16502): h3{font-size:1.2em;margin:10px 0 0 0;color:#000000;}
I/flutter (16502): #header{width:96%;margin:0 0 0 0;padding:6px 2% 6px 2%;font-family:"trebuchet MS", Verdana, sans-serif;color:#FFF;
I/flutter (16502): background-color:#555555;}
I/flutter (16502): #content{margin:0 0 0 2%;position:relative;}
I/flutter (16502): .content-container{background:#FFF;width:96%;margin-top:8px;padding:10px;position:relative;}
I/flutter (16502): -->
I/flutter (16502): </style>
I/flutter (16502): </head>
I/flutter (16502): <body
E/flutter (16502): [ERROR:flutter/lib/ui/ui_dart_state.cc(148)] Unhandled Exception: FormatException: Unexpected character (at character 1)
E/flutter (16502): <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org...
E/flutter (16502): ^
E/flutter (16502):
E/flutter (16502): #0 _ChunkedJsonParser.fail (dart:convert/runtime/libconvert_patch.dart:1357:5)
E/flutter (16502): #1 _ChunkedJsonParser.parseNumber (dart:convert/runtime/libconvert_patch.dart:1253:9)
E/flutter (16502): #2 _ChunkedJsonParser.parse (dart:convert/runtime/libconvert_patch.dart:921:22)
E/flutter (16502): #3 _parseJson (dart:convert/runtime/libconvert_patch.dart:29:10)
E/flutter (16502): #4 JsonDecoder.convert (dart:convert/json.dart:491:36)
E/flutter (16502): #5 JsonCodec.decode (dart:convert/json.dart:149:41)
E/flutter (16502): #6 _MyHomePageState.makeRequest.<anonymous closure> (package:doi_report/homepage.dart:30:30)
E/flutter (16502): #7 State.setState (package:flutter/src/widgets/framework.dart:1122:30)
E/flutter (16502): #8 _MyHomePageState.makeRequest (package:doi_report/homepage.dart:29:5)
E/flutter (16502): <asynchronous suspension>
E/flutter (16502): #9 _MyHomePageState.initState (package:doi_report/homepage.dart:42:10)
E/flutter (16502): #10 StatefulElement._firstBuild (package:flutter/src/widgets/framework.dart:3846:58)
E/flutter (16502): #11 ComponentElement.mount (package:flutter/src/widgets/framework.dart:3711:5)
E/flutter (16502): #12 Element.inflateWidget (package:flutter/src/widgets/framework.dart:2956:14)
E/flutter (16502): #13 Element.updateChild (package:flutter/src/widgets/framework.dart:2759:12)
E/flutter (16502): #14 SingleChildRenderObjectElement.mount (package:flutter/src/widgets/framework.dart:4876:14)
E/flutter (16502): #15 Element.inflateWidget (package:flutter/src/widgets/framework.dart:2956:14)
E/flutter (16502): #16 Element.updateChild (package:flutter/src/widgets/framework.dart:2759:12)
E/flutter (16502): #17 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:3747:16)
E/flutter (16502): #18 Element.rebuild (package:flutter/src/widgets/framework.dart:3559:5)
E/flutter (16502): #19 ComponentElement._firstBuild (package:flutter/src/widgets/framework.dart:3716:5)
E/flutter (16502): #20 ComponentElement.mount (package:flutter/src/widgets/framework.dart:3711:5)
E/flutter (16502): #21 Element.inflateWidget (package:flutter/src/widgets/framework.dart:2956:14)
E/flutter (16502): #22 Element.updateChild (package:flutter/src/widgets/framework.dart:2759:12)
E/flutter (16502): #23 SingleChildRenderObjectElement.mount (package:flutter/src/widgets/framework.dart:4876:14)
E/flutter (16502): #24 Element.inflateWidget (package:flutter/src/widgets/framework.dart:2956:14)
E/flutter (16502): #25 Element.updateChild (package:flutter/src/widgets/framework.dart:2759:12)
E/flutter (16502): #26 SingleChildRenderObjectElement.mount (package:flutter/src/widgets/framework.dart:4876:14)
E/flutter (16502): #27 Element.inflateWidget (package:flutter/src/widgets/framework.dart:2956:14)
E/flutter (16502): #28 Element.updateChild (package:flutter/src/widgets/framework.dart:2759:12)
E/flutter (16502): #29 SingleChildRenderObjectElement.mount (package:flutter/src/widgets/framework.dart:4876:14)
E/flutter (16502): #30 Element.inflateWidget (package:flutter/src/widgets/framework.dart:2956:14)
E/flutter (16502): #31 Element.updateChild (package:flutter/src/widgets/framework.dart:2759:12)
E/flutter (16502): #32 SingleChildRenderObjectElement.mount (package:flutter/src/widgets/framework.dart:4876:14)
E/flutter (16502): #33 Element.inflateWidget (package:flutter/src/widgets/framework.dart:2956:14)
E/flutter (16502): #34 Element.updateChild (package:flutter/src/widgets/framework.dart:2759:12)
E/flutter (16502): #35 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:3747:16)
E/flutter (16502): #36 Element.rebuild (package:flutter/src/widgets/framework.dart:3559:5)
E/flutter (16502): #37 ComponentElement._firstBuild (package:flutter/src/widgets/framework.dart:3716:5)
E/flutter (16502): #38 StatefulElement._firstBuild (package:flutter/src/widgets/framework.dart:3864:11)
E/flutter (16502): #39 ComponentElement.mount (package:flutter/src/widgets/framework.dart:3711:5)
E/flutter (16502): #40 Element.inflateWidget (package:flutter/src/widgets/framework.dart:2956:14)
E/flutter (16502): #41 Element.updateChild (package:flutter/src/widgets/framework.dart:2759:12)
E/flutter (16502): #42 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:3747:16)
E/flutter (16502): #43 Element.rebuild (package:flutter/src/widgets/framework.dart:3559:5)
E/flutter (16502): #44 ComponentElement._firstBuild (package:flutter/src/widgets/framework.dart:3716:5)
E/flutter (16502): #45 ComponentElement.mount (package:flutter/src/widgets/```
答案 0 :(得分:0)
问题出在身份验证的服务器类型上,我启用了Windows身份验证,这就是不允许我的应用访问我想从php文件中检索的数据的问题。尽管我必须说检查连接性以及当我使用vpn时没有结果,但我还是认为问题出在应用程序中而不是服务器中。
它可以在禁用Windows身份验证的情况下工作,但是checkconnection的结果仍然没有。