【发布时间】:2018-01-29 19:12:09
【问题描述】:
我目前正在尝试使用 Dart 编程语言通过 Digest Authentication 发出 http POST 请求。唯一支持 http 摘要认证的 Dart http 类是 dart:io:HttpClient 类。
这是我目前的代码:
setup() async {
var httpclient = new HttpClient();
httpclient.addCredentials(Uri.parse("https://example.com/pearson-rest/services/PublicPortalServiceJSON"), "Protected", new HttpClientDigestCredentials("pearson", "m0bApP5"));
await httpclient.postUrl(Uri.parse("https://example.com/pearson-rest/services/PublicPortalServiceJSON"))
.then((HttpClientRequest request) {
request.headers.set("Content-Type", "text/xml; charset=utf-8");
request.headers.set("SOAPAction", "https://example.com:443/pearson-rest/services/PublicPortalServiceJSON#loginToPublicPortal");
request.headers.set("Host", "example.com:443");
request.headers.set("User-Agent", "Apache-HttpClient/UNAVAILABLE (java 1.5)");
request.write("""<?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/" xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd" xmlns="http://publicportal.rest.powerschool.pearson.com/xsd"><soap:Body><login xmlns="http://publicportal.rest.powerschool.pearson.com/xsd"><username><![CDATA["""+ studentUsername +"""]]></username><password><![CDATA["""+ studentPassword +"""]]></password><userType>2</userType></login></soap:Body></soap:Envelope>""");
return request.close();
})
.then((HttpClientResponse response) async {
print(response.statusCode);
print(response.headers);
print(await response.transform(UTF8.decoder).join());
});
}
请求对象似乎没有正文字段。文档也很糟糕,所以到目前为止我还没有找到解决方案。我认为 write() 方法相当于一个主体,但事实并非如此。如何将正文添加到 HttpClient POST 请求?或者是否有不同的类也支持摘要身份验证?
【问题讨论】:
-
为什么你认为
write不起作用?也许您想使用add? api.dartlang.org/stable/1.24.3/dart-io/… 中列出了所有其他可用方法 -
@GünterZöchbauer 身份验证工作正常,SOAP 信封 100% 正确,但请求导致内部服务器错误,表明信封未正确传输或未按预期方式传输。我之前用其他语言测试过相同的信封,它工作得非常好。
-
我不知道服务器需要什么信封。您能否发布您从服务器获得的确切错误消息?
-
@GünterZöchbauer The error message
-
Dart 不会创建 SOAP 请求,我怀疑任何其他语言都没有明确这样做。我不知道可以为 Dart 提供的软件包。也许你可以运行一个服务器端服务,在支持 SOAP 的语言中转换 JSON 和 SOAP
标签: post dart http-post dart-pub