Flutter/Dart 中的 SOAP 请求

Dan*_*lho 3 xml soap wsdl dart flutter

我需要使用 Flutter 向 .NET Webservice (WSDL) 发出 SOAP 请求。

该网络服务有一个基本的身份验证(用户、密码)和一些带有预定义信封的服务。

所以我尝试创建一个 SOAP 信封:

String requestBody = "<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:tot=\"http://www.totvs.com/\">   <soapenv:Header/>   <soapenv:Body>      <tot:RealizarConsultaSQL>         <!--Optional:-->         <tot:codSentenca>ETHOS.TESTE</tot:codSentenca>         <!--Optional:-->         <tot:codColigada>0</tot:codColigada>         <!--Optional:-->         <tot:codSistema>F</tot:codSistema>         <!--Optional:-->         <tot:parameters></tot:parameters>      </tot:RealizarConsultaSQL>   </soapenv:Body></soapenv:Envelope>";
Run Code Online (Sandbox Code Playgroud)

此信封有效。第二步是建立 http 连接:

http.Response response = await http.post(
  request,
  headers: {
    "Accept-Encoding": "gzip,deflate",
    "Content-Length": utf8.encode(requestBody).length.toString(),
    "Content-Type": "text/xmlc",
    "SOAPAction": "http://www.totvs.com/IwsConsultaSQL/RealizarConsultaSQL",
    "Authorization": "Basic bWVzdHJlOnRvdHZz",
    "Host": "totvs.brazilsouth.cloudapp.azure.com:8051",
    "Connection": "Keep-Alive",
    "User-Agent": "Apache-HttpClient/4.1.1 (java 1.5)"
  },
  body: utf8.encode(requestBody),
  encoding: Encoding.getByName("UTF-8")).then((onValue)
{
  print("Response status: ${onValue.statusCode}");
  print("Response body: ${onValue.body}");
 });
Run Code Online (Sandbox Code Playgroud)

此时我刚刚收到 411 代码:

<hr><p>HTTP Error 411. The request must be chunked or have a content length.</p>
Run Code Online (Sandbox Code Playgroud)

所以,我有两个很大的疑问:

  1. 如何通过身份验证(用户/密码);
  2. 为什么,即使设置“Content-Length”硬编码它总是返回 411。

我是 Dart/Flutter 新手

Dan*_*lho 6

经过大量时间的测试,我通过使用此标头获得了成功:

     "SOAPAction": "http://www.totvs.com/IwsConsultaSQL/RealizarConsultaSQL",
    "Content-Type": "text/xml;charset=UTF-8",
    "Authorization": "Basic bWVzdHJlOnRvdHZz",
    "cache-control": "no-cache"
Run Code Online (Sandbox Code Playgroud)

看来 Content-Length 是自动发送的,所以,有效的代码是这样的:

  http.Response response = await http.post(
      request,
      headers: {
        "SOAPAction": "http://www.totvs.com/IwsConsultaSQL/RealizarConsultaSQL",
        "Content-Type": "text/xml;charset=UTF-8",
        "Authorization": "Basic bWVzdHJlOnRvdHZz",
        "cache-control": "no-cache"
      },
      body: utf8.encode(requestBody),
      encoding: Encoding.getByName("UTF-8")
  ).then((onValue)
  {
    print("Response status: ${onValue.statusCode}");
    print("Response body: ${onValue.body}");

  });
Run Code Online (Sandbox Code Playgroud)

感谢大家的帮助,我按照之前的建议通过邮递员代码得到了解决方案,谢谢。