Flutter/Dart中的SOAP请求 [英] SOAP Request in Flutter/Dart
问题描述
我需要使用Flutter向.NET Web服务(WSDL)发出SOAP请求.
I need to make a SOAP request to a .NET Webservice (WSDL) with Flutter.
此网络服务具有基本的身份验证(用户,密码)和一些带有预定义信封的服务.
This webservice has an basic auth (user, password) and some services with pre-defined envelopes.
所以我试图创建一个SOAP信封:
So I tried to create a SOAP envelope:
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连接:
This envelope is valid. The second step was to make a http connection:
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代码:
At this point I just receive 411 code:
<hr><p>HTTP Error 411. The request must be chunked or have a content length.</p>
所以,我有两个大疑问:
So, I have 2 big doubts :
- 如何通过身份验证(用户名/密码);
- 为什么即使硬编码设置"Content-Length"也总是返回411.
我是Dart/Flutter的新手
I'm new in Dart/Flutter
推荐答案
花费大量时间进行测试后,使用此标头获得了成功:
After a lot of hours spended in tests, I got success by using this header:
"SOAPAction": "http://www.totvs.com/IwsConsultaSQL/RealizarConsultaSQL",
"Content-Type": "text/xml;charset=UTF-8",
"Authorization": "Basic bWVzdHJlOnRvdHZz",
"cache-control": "no-cache"
它表明Content-Length是自动发送的,因此,起作用的代码是这样的:
It seams that Content-Length is send automatically, so, the code that worked is this:
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}");
});
谢谢大家的帮助,我通过邮递员的代码获得了解决方案,就像以前建议的那样,谢谢.
Thanks all for the help, I got the solution by a Postman code, as suggested before, thanks.
这篇关于Flutter/Dart中的SOAP请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!