首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >SOAP /Dart中的SOAP请求

SOAP /Dart中的SOAP请求
EN

Stack Overflow用户
提问于 2018-11-27 18:05:34
回答 2查看 13.8K关注 0票数 2

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

这个webservice有一个基本的auth (用户,密码)和一些带有预定义信封的服务。

因此,我尝试创建一个SOAP信封:

代码语言:javascript
复制
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>";

这个信封是有效的。第二步是建立一个http连接:

代码语言:javascript
复制
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}");
 });

此时,我只收到411代码:

代码语言:javascript
复制
<hr><p>HTTP Error 411. The request must be chunked or have a content length.</p>

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

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

我是新来的达特/颤音

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2018-12-03 18:07:24

在测试中花费了大量时间之后,我通过使用以下标题获得了成功:

代码语言:javascript
复制
     "SOAPAction": "http://www.totvs.com/IwsConsultaSQL/RealizarConsultaSQL",
    "Content-Type": "text/xml;charset=UTF-8",
    "Authorization": "Basic bWVzdHJlOnRvdHZz",
    "cache-control": "no-cache"

它会自动发送内容长度,因此,工作的代码如下:

代码语言:javascript
复制
  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}");

  });

谢谢大家的帮助,我得到了邮递员代码的解决方案,正如前面所建议的,谢谢。

票数 5
EN

Stack Overflow用户

发布于 2018-11-28 02:59:30

您设置的标题太多,因为大多数标题将由客户端设置--特别是内容长度和编码。

基本的auth头看起来没问题。

不要把awaitthen混为一谈--用一种或另一种。

您可以将代码简化为:

代码语言:javascript
复制
import 'dart:convert';
import 'package:http/http.dart' as http;

main() async {
  String soap = '''<?xml version="1.0"?>
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"
                  xmlns:tot="http://www.totvs.com/">
  <soapenv:Header/>
  <soapenv:Body>
    <tot:RealizarConsultaSQL>      
      <tot:codSentenca>ETHOS.TESTE</tot:codSentenca>    
      <tot:codColigada>0</tot:codColigada>       
      <tot:codSistema>F</tot:codSistema>       
      <tot:parameters></tot:parameters>
    </tot:RealizarConsultaSQL>
  </soapenv:Body>
</soapenv:Envelope>''';

  http.Response response = await http.post(
    'http://totvs.brazilsouth.cloudapp.azure.com:8051/wherever',
    headers: {
      'content-type': 'text/xmlc',
      'authorization': 'bWVzdHJlOnRvdHZz',
      'SOAPAction': 'http://www.totvs.com/IwsConsultaSQL/RealizarConsultaSQL',
    },
    body: utf8.encode(soap),
  );
  print(response.statusCode);
}

请注意,Dart http降低了所有头名(如RFC所允许的),但这会混淆一些服务器。

用邮递员试试你的请求。如果你能让它在那里工作,编辑这个问题,显示一个好的邮递员的要求(或其他语言)。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/53505624

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档