【问题标题】:Is it possible to add a body to a http POST request made with the dart:io:HttpClient class?是否可以将正文添加到使用 dart:io:HttpClient 类发出的 http POST 请求中?
【发布时间】: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 不起作用?也许您想使用addapi.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


【解决方案1】:

Write 是正确的使用方法。我获取了您的示例代码并将其发布到 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());
  });
}

【讨论】:

  • 感谢您的评论。我现在看到 write() 相当于一个主体。然而,我现在意识到我真正的问题是什么。当我发送请求时,我收到“序言中的意外 EOF”错误。你有没有机会知道这个错误的来源?我设置了一个开发服务器,它似乎正在交付整个主体,但是当我将它发送到实际服务器时,我得到了那个错误。
  • 恐怕不行 - 这听起来像是服务器端的另一个问题,需要一些调试。我建议尝试从 Fiddler 之类的东西发送请求,看看你是否可以在那里重现,如果可以调试网络应用程序(或者如果它不是你的代码,请发布关于该问题的更具体的问题)。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-05-29
  • 2014-08-16
  • 2017-12-09
  • 2010-11-16
  • 1970-01-01
相关资源
最近更新 更多