我需要向.NET Webservice (WSDL)发出SOAP请求。
这个webservice有一个基本的auth (用户,密码)和一些带有预定义信封的服务。
因此,我尝试创建一个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>";这个信封是有效的。第二步是建立一个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}");
});此时,我只收到411代码:
<hr><p>HTTP Error 411. The request must be chunked or have a content length.</p>所以,我有两个很大的疑问:
我是新来的达特/颤音
发布于 2018-12-03 18:07:24
在测试中花费了大量时间之后,我通过使用以下标题获得了成功:
"SOAPAction": "http://www.totvs.com/IwsConsultaSQL/RealizarConsultaSQL",
"Content-Type": "text/xml;charset=UTF-8",
"Authorization": "Basic bWVzdHJlOnRvdHZz",
"cache-control": "no-cache"它会自动发送内容长度,因此,工作的代码如下:
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}");
});谢谢大家的帮助,我得到了邮递员代码的解决方案,正如前面所建议的,谢谢。
发布于 2018-11-28 02:59:30
您设置的标题太多,因为大多数标题将由客户端设置--特别是内容长度和编码。
基本的auth头看起来没问题。
不要把await和then混为一谈--用一种或另一种。
您可以将代码简化为:
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所允许的),但这会混淆一些服务器。
用邮递员试试你的请求。如果你能让它在那里工作,编辑这个问题,显示一个好的邮递员的要求(或其他语言)。
https://stackoverflow.com/questions/53505624
复制相似问题