Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/dart/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
是否可以向使用dart:io:HttpClient类发出的http POST请求添加正文?_Post_Dart_Http Post_Dart Pub - Fatal编程技术网

是否可以向使用dart:io:HttpClient类发出的http POST请求添加正文?

是否可以向使用dart:io:HttpClient类发出的http POST请求添加正文?,post,dart,http-post,dart-pub,Post,Dart,Http Post,Dart Pub,我目前正在尝试使用Dart编程语言通过摘要身份验证发出http POST请求。唯一支持http摘要身份验证的Dart http类是该类 这是我目前的代码: setup()异步{ var httpclient=新的httpclient(); httpclient.addCredentials(Uri.parse(“https://example.com/pearson-rest/services/PublicPortalServiceJSON新的HttpClientDigestCredential

我目前正在尝试使用Dart编程语言通过摘要身份验证发出http POST请求。唯一支持http摘要身份验证的Dart http类是该类

这是我目前的代码:
setup()异步{
var httpclient=新的httpclient();
httpclient.addCredentials(Uri.parse(“https://example.com/pearson-rest/services/PublicPortalServiceJSON新的HttpClientDigestCredentials(“pearson”、“m0bApP5”);
等待httpclient.postrl(Uri.parse(“https://example.com/pearson-rest/services/PublicPortalServiceJSON"))
.然后((HttpClientRequest请求){
request.headers.set(“内容类型”,“text/xml;charset=utf-8”);
request.headers.set(“SOAPAction”https://example.com:443/pearson-rest/services/PublicPortalServiceJSON(登录公共门户);
set(“Host”,“example.com:443”);
set(“用户代理”,“ApacheHttpClient/UNAVAILABLE(Java1.5)”;
请求。填写(““2”);
返回请求。close();
})
.then((HttpClientResponse响应)异步{
打印(响应状态码);
打印(响应.标题);
打印(等待响应.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());
  });
}

为什么你认为
write
不起作用?也许您想使用
添加
?@GünterZöchbauer中列出了所有其他可用方法身份验证正在工作,SOAP信封100%正确,但请求会导致内部服务器错误,表明信封未正确传输或未按预期方式传输。我以前用其他语言测试过同一个信封,效果非常好。我不知道服务器需要什么信封。你能发布你从服务器上得到的准确的错误消息吗?@GünterZöchbauer Dart不会创建SOAP请求,我怀疑任何其他语言没有明确地创建SOAP请求。我不知道有没有一个包裹能为Dart提供这样的服务。也许您可以运行一个服务器端服务,该服务使用支持SOAP的语言在JSON和SOAP之间进行转换谢谢您的评论。我现在看到write()相当于一个实体。然而,我现在意识到我真正的问题是什么。当我发送请求时,我得到一个“prolog中意外的EOF”错误。你知道这个错误的来源吗?我安装了一个dev服务器,它似乎提供了整个主体,但当我将其发送到实际服务器时,我收到了该错误。恐怕不会-这听起来像是服务器端的另一个问题,需要一些调试。我建议尝试从Fiddler之类的人那里发送请求,看看你是否可以在那里重新编程,如果可以的话,调试web应用程序(或者如果不是你的代码,就发布一个关于这个问题的更具体的问题)。
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());
  });
}