You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Flutter中SOAP登录接口报500错误,求协助排查代码问题

Flutter SOAP登录请求500错误修复方案

以下是你的代码中存在的核心问题及修复步骤:

1. SOAP请求体格式错误且缺失登录参数

你的_body存在两个致命问题:

  • 末尾多余的'link';直接破坏了XML结构
  • <ser:authentifierUtilisateur>标签为空,未传入登录必需的用户名和密码参数

修复后的动态请求体示例:

final body = '''
<?xml version="1.0" encoding="utf-8"?>
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" 
xmlns:ser="http:/link.fr/">
<soapenv:Header/>
<soapenv:Body>
<ser:authentifierUtilisateur>
  <ser:identifiant>${identifiantController.text}</ser:identifiant>
  <ser:motDePasse>${motDePasseController.text}</ser:motDePasse>
</ser:authentifierUtilisateur>
</soapenv:Body>
</soapenv:Envelope>
''';

2. 请求URL未配置

Uri.parse('')为空,需替换为真实的SOAP接口地址:

var response = await http.post(Uri.parse('https://your-real-api-domain.fr/soap-endpoint'),

3. SOAPAction头未正确设置

SOAPAction需对应接口的完整方法路径,示例:

'SOAPAction': 'http:/link.fr/authentifierUtilisateur',

4. SOAP命名空间无效

将xmlns:soapenv="link"替换为标准SOAP命名空间:

xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"

额外调试建议

  • 发送请求前打印utf8.encode(body)内容,确认XML格式完全符合接口要求
  • 捕获错误时打印响应完整body,服务端500错误通常会返回具体错误描述,帮助定位问题:
} else {  
  print('Response status: ${response.statusCode}');
  print('Response body: ${response.body}');
  throw Exception('Failed load data ${response.statusCode}');
}

修复后的完整UserProvider类示例:

class UserProvider{ 
  TextEditingController motDePasseController = TextEditingController();    
  TextEditingController identifiantController = TextEditingController(); 

  UserProvider(){} 

  Future<List<UsersModel>> loginUser( ) async{ 
    final body = '''
    <?xml version="1.0" encoding="utf-8"?>
    <soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" 
    xmlns:ser="http:/link.fr/">
    <soapenv:Header/>
    <soapenv:Body>
    <ser:authentifierUtilisateur>
      <ser:identifiant>${identifiantController.text}</ser:identifiant>
      <ser:motDePasse>${motDePasseController.text}</ser:motDePasse>
    </ser:authentifierUtilisateur>
    </soapenv:Body>
    </soapenv:Envelope>
    ''';
    try{ 
      var response = await http.post(Uri.parse('https://your-real-api-domain.fr/soap-endpoint'),
        headers: { 
          'content-type': 'text/xml; charset=utf-8',
          'SOAPAction': 'http:/link.fr/authentifierUtilisateur',     
          HttpHeaders.authorizationHeader: '4e1bae794a5a905db6620ab0be1c593c'
        },
        body: utf8.encode(body),
      );
      if(response.statusCode == 200){  
        identifiantController.clear();
        motDePasseController.clear();
        final responseBody = response.body;
        final parseXml = xml.XmlDocument.parse(responseBody).innerText;
        final decodeJson = jsonDecode(parseXml) as List;
        List<UsersModel> data =
          decodeJson.map((e) => UsersModel.fromJson(e)).toList();
        return data;
      } else{  
        print('Response status: ${response.statusCode}');
        print('Response body: ${response.body}');
        throw Exception('Failed load data ${response.statusCode}');
      }
    }catch(error){
      print(error); 
      throw error;
    }
  }
}

内容的提问来源于stack exchange,提问作者Mouadh Hadj Ammar

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.07 10:55:15