Flutter: (ASP.NET) Web API : Invalid Header Field Name

試著忘記壹切 提交于 2020-06-29 15:16:56

问题


I am new to Flutter and I am trying to call my ASP.NET server web API.

From the logs on my server, everything goes fine but Android Studio throws an exception: "invalid header field name".

Here is the code in dart:

import 'package:http/http.dart' as http;
...

_getService()  async {
  String result;
  try {
    var url = 'http://192.168.1.14:34263/api/Mobile/test/1';
    Future<http.Response> response = http.get( url );
    result = response.toString();
  } catch(exception){
    result = exception.toString();
    debugPrint(result);
  }
...
}

Here is the response header (obtained via Chrome):

Access-Control-Allow-Headers:accept, authorization, Content-Type
Access-Control-Allow-Methods: GET, POST, OPTIONS, PUT, PATCH, DELETE
Access-Control-Allow-Origin: *
Access-Control-Expose-Headers: WWW-Authenticate
Cache-Control: no-cache
Content-Encoding: deflate
Content-Length:79
Content-Type: application/xml; charset=utf-8
Date: Thu, 08 Mar 2018 01:01:25 GMT
Expires:-1
Pragma:no-cache

Server:MyTestServer
X-Content-Type-Options:NOSNIFF
X-Permitted-Cross-Domain-Policies:master-only
X-SourceFiles:=?UTF-8?BDpcTXlJbmNyZWRpYmxlRHJlc3NpbmdcTXlJbmNyZWRpYmxlRHJlc3NpbmdcTXlJbmNyZWRpYmxlRHJlc3NpbmdcYXBpXE1vYmlsZVxjb3Vjb3VcMQ==?=
X-XSS-Protection:1;mode=block

Here is the answer which is returned:

<string xmlns="http://schemas.microsoft.com/2003/10/Serialization/">test</string>

Can anyone tell me what am I doing wrong?

Many thanks


回答1:


Ok, I finally found out, by debugging the code.

In fact, my server added a series of field names in the response's header (via the Web.config) and the last character of one of these field names was a space. As a result, the http_parser.dart threw an exception since spaces are no authorized characters in header field name.

Nothing was detected by Chrome (or any browser) nor by Postman.




回答2:


I had similar problem and after some heavy debugging

I removed these headers from nginx:

#add_header X−Content−Type−Options nosniff;

#add_header X−Frame−Options SAMEORIGIN;

#add_header X−XSS−Protection 1;

and it works fine. So most likely it's backend - header related issue



来源:https://stackoverflow.com/questions/49163993/flutter-asp-net-web-api-invalid-header-field-name

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!