web-services - 如何从 Flutter/Dart Soap Web 服务 asmx 调用中获取值(value)?

标签 web-services soap dart flutter asmx

我试图了解 Flutter 和 Dart 如何使用 SOAP 网络服务 asmx。为此,我创建了非常基本的项目并使用在线soap WebService asmx 进行测试。

使用 http://www.dneonline.com/calculator.asmx?op=Add我在 Flutter 中成功构建了我的信封。

在测试中我使用 SOAP 1.1 部分。

首先我无法运行应用程序,但幸运的是我在“ Content-Length: length ”上发现了错误。所以我删除了它,一切都很好。

它是一个基本的计算器(添加)功能。首先我不知道如何在信封内添加整数,所以我使用静态硬编码值。

作为回应,我找到了 9 有我的 SOAP 电话回答的行。

第二件事是(这是我的问题)我得到响应正文,但我不知道如何从正文中获取值(value)。

如何从 Flutter/Dart Soap Web 服务 asmx 调用中获取值(value)?

这是我完整的 Flutter 代码。

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

void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: MyHomePage(title: 'Flutter Demo Home Page'),
    );
  }
}

class MyHomePage extends StatefulWidget {
  MyHomePage({Key key, this.title}) : super(key: key);

  final String title;

  @override
  _MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  int _firstInteger = 5;
  int _secondInteger = 4;

  var envelope =
      "<?xml version=\"1.0\" encoding=\"utf-8\"?><soap:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\"> <soap:Body> <Add xmlns=\"http://tempuri.org/\"> <intA>5</intA> <intB>4</intB></Add></soap:Body></soap:Envelope>";
  var _testValue = "";
  bool _add = true;

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    _add = true;
  }

  Future _getCalculator() async {
    http.Response response =
        await http.post('http://www.dneonline.com/calculator.asmx',
            headers: {
              "Content-Type": "text/xml; charset=utf-8",
              "SOAPAction": "http://tempuri.org/Add",
              "Host": "www.dneonline.com"
            },
            body: envelope);

    setState(() {
      _testValue = response.body;
      _add = true;
    });
    print(response.body);
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(widget.title),
      ),
      body: Center(
          child: new Column(
        mainAxisAlignment: MainAxisAlignment.center,
        children: <Widget>[
          _add == true
              ? new Text(
                  "Answer: $_testValue",
                  style: new TextStyle(
                      fontSize: 18.0,
                      fontWeight: FontWeight.bold,
                      color: Colors.red[800]),
                )
              : new CircularProgressIndicator(),
          new RaisedButton(
            onPressed: () {
              setState(() {
                _add = false;
              });
              _getCalculator();
            },
            child: new Text("Calculate"),
          )
        ],
      )),
    );
  }
}

这是 response.body 输出:
I/flutter ( 6414): <?xml version="1.0" encoding="utf-8"?><soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"><soap:Body><AddResponse xmlns="http://tempuri.org/"><AddResult>9</AddResult></AddResponse></soap:Body></soap:Envelope>

最佳答案

完整的工作代码在这里:

从 Flutter 调用 soap web services asmx 并使用 dart xml 解析它。 :)

希望我们能得到一些 Dart xml 信封转换器,这样我们就不必手动创建每个信封。

import 'package:flutter/material.dart';
import 'package:http/http.dart' as http;
import 'package:xml/xml.dart' as xml;


void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: MyHomePage(title: 'Flutter Demo Home Page'),
    );
  }
}

class MyHomePage extends StatefulWidget {
  MyHomePage({Key key, this.title}) : super(key: key);

  final String title;

  @override
  _MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  int _firstInteger = 5;
  int _secondInteger = 4;

  var envelope =
      "<?xml version=\"1.0\" encoding=\"utf-8\"?><soap:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\"> <soap:Body> <Add xmlns=\"http://tempuri.org/\"> <intA>5</intA> <intB>4</intB></Add></soap:Body></soap:Envelope>";
  var _testValue = "";
  bool _add = true;

  List<dynamic> itemsList = List();

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    _add = true;
  }

  Future _getCalculator() async {
    http.Response response =
        await http.post('http://www.dneonline.com/calculator.asmx',
            headers: {
              "Content-Type": "text/xml; charset=utf-8",
              "SOAPAction": "http://tempuri.org/Add",
              "Host": "www.dneonline.com"
            },
            body: envelope);
    var _response = response.body;
    await _parsing(_response);
  }


  Future _parsing(var _response) async {
    var _document = xml.parse(_response);
    Iterable<xml.XmlElement> items = _document.findAllElements('AddResponse');
    items.map((xml.XmlElement item) {
      var _addResult = _getValue(item.findElements("AddResult"));
      itemsList.add(_addResult);
    }).toList();

    print("itemsList: $itemsList");

    setState(() {
      _testValue = itemsList[0].toString();
      _add = true;
    });

  }


  _getValue(Iterable<xml.XmlElement>  items) {
    var textValue;
    items.map((xml.XmlElement node) {
      textValue = node.text;
    }).toList();
    return textValue;
  }


  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(widget.title),
      ),
      body: Center(
          child: new Column(
        mainAxisAlignment: MainAxisAlignment.center,
        children: <Widget>[
          _add == true
              ? new Text(
                  "Answer: $_testValue",
                  style: new TextStyle(
                      fontSize: 18.0,
                      fontWeight: FontWeight.bold,
                      color: Colors.red[800]),
                )
              : new CircularProgressIndicator(),
          new RaisedButton(
            onPressed: () {
              setState(() {
                _add = false;
              });
              _getCalculator();
            },
            child: new Text("Calculate"),
          )
        ],
      )),
    );
  }
}

关于web-services - 如何从 Flutter/Dart Soap Web 服务 asmx 调用中获取值(value)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55605458/

相关文章:

magento - SOAP 错误 : Parsing Schema: can't import schema from …

android - 由于QuerySnapshot为null,即使在initState()中初始化后,也对null调用了getter 'docs'

android - 如何为 ASP.NET MVC3 创建 Web 服务

javascript - 使用文件 ://在本地加载的来自移动 WebView 的 CORS cookie 凭据

android - 在android中将图像和视频发送到服务器

java - 从 Spring-WS 中的 getWebServiceTemplate().marshalSendAndReceive() 获取 byte[] 响应

java - SoapFault 子节点中的 xmlns =""(空)命名空间

Dart:在浏览器中使用 dart 编写的 webworker,并在 dart2js 中使用

android - 滑动列表项以获取更多选项(Flutter)

web-services - 从 jsp 调用 Jasper Server 报告