是否可以为使用dart:io:HttpClient类创建的http POST请求添加正文?

时间:2018-01-29 19:12:09

标签: post dart http-post dart-pub

我目前正在尝试使用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请求中添加正文?或者是否有一个不同的类支持摘要身份验证?

1 个答案:

答案 0 :(得分:0)

写是正确的用法。我拿了你的示例代码并将其发布到httpbin,它包含了我在回复中写回的数据:

Date d = Date.Today().addDays(15);

Example response