gpt4 book ai didi

post - 是否可以将主体添加到使用dart:io:HttpClient类发出的HTTP POST请求中?

转载 作者:行者123 更新时间:2023-12-03 03:43:01 25 4
gpt4 key购买 nike

我目前正在尝试使用Dart编程语言通过摘要身份验证发出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请求?还是有另一个支持摘要身份验证的类?

最佳答案

写是正确的用法。我获取了您的示例代码并将其发布到httpbin,其中包含了我在响应中回写的数据:

postTest() async {
const body = "This is my body";
var httpclient = new HttpClient();
await httpclient
.postUrl(Uri.parse(
"http://httpbin.org/post"))
.then(
(HttpClientRequest request) {
request.headers.contentLength =
body.length;
request.write(body);
return request.close();
}).then((HttpClientResponse
response) async {
print(response.statusCode);
print(response.headers);
print(await response
.transform(UTF8.decoder)
.join());
});
}

关于post - 是否可以将主体添加到使用dart:io:HttpClient类发出的HTTP POST请求中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48508370/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com