是的,所以我一直致力于通过标头进行基本身份验证,并通过HTTP Post传递一些变量.这是一个终端应用程序.
这就是我的代码:
import 'package:http/http.dart' as http;
import 'dart:io';
void main() {
var url = "http://httpbin.org/post";
var client = new http.Client();
var request = new http.Request('POST', Uri.parse(url));
var body = {'content':'this is a test', 'email':'john@doe.com', 'number':'441276300056'};
request.headers[HttpHeaders.CONTENT_TYPE] = 'application/json; charset=utf-8';
request.headers[HttpHeaders.AUTHORIZATION] = 'Basic 021215421fbe4b0d27f:e74b71bbce';
request.body = body;
var future = client.send(request).then((response) => response.stream.bytesToString().then((value) => print(value.toString()))).catchError((error) => print(error.toString()));
}
Run Code Online (Sandbox Code Playgroud)
我正在使用httpbin作为回显服务器,所以它告诉我我传入的内容.如果我没有传递正文,或者我将字符串作为正文传递,我的代码可以正常工作.
显然这是因为http.Request中的body属性只接受字符串,而我正在尝试将映射传递给它.
我可以将它转换为字符串,它可能会工作,但我仍然认为我的代码可以改进.不是从语法的角度,或从它如何处理未来,但我不确定使用http.dart是正确的事情.
有人能指出我正确的方向吗?
提前致谢.
以下异常:
SocketIOException: Unexpected handshake error in client (OS Error: errno = -12268)
#0 _SecureFilterImpl.handshake (dart:io-patch:849:8)
#1 _SecureSocket._secureHandshake (dart:io:7382:28)
#2 _SecureSocket._secureConnectHandler._secureConnectHandler (dart:io:7294:21)
#3 _Socket._updateOutHandler.firstWriteHandler (dart:io-patch:773:64)
#4 _SocketBase._multiplex (dart:io-patch:408:26)
#5 _SocketBase._sendToEventHandler.<anonymous closure> (dart:io-patch:509:20)
#6 _ReceivePortImpl._handleMessage (dart:isolate-patch:37:92)
Run Code Online (Sandbox Code Playgroud)
以下代码的结果:
SocketIOException: Unexpected handshake error in client (OS Error: errno = -12268)
#0 _SecureFilterImpl.handshake (dart:io-patch:849:8)
#1 _SecureSocket._secureHandshake (dart:io:7382:28)
#2 _SecureSocket._secureConnectHandler._secureConnectHandler (dart:io:7294:21)
#3 _Socket._updateOutHandler.firstWriteHandler (dart:io-patch:773:64)
#4 _SocketBase._multiplex (dart:io-patch:408:26)
#5 _SocketBase._sendToEventHandler.<anonymous closure> (dart:io-patch:509:20)
#6 _ReceivePortImpl._handleMessage (dart:isolate-patch:37:92)
Run Code Online (Sandbox Code Playgroud)
请注意,如果我将 url 替换为“http”而不是“https”,它将按预期工作。
我最近部署了一个服务于HTTP请求的Dart服务器应用程序.我想添加对HTTPS的支持,所以我一直在尝试将SSL添加到Dart服务器应用程序中.
此答案清楚地说明了如何向Dart添加自签名SSL证书.但是,我想添加一个从SSL提供商处购买的SSL证书.
SSL提供商通过电子邮件发送了我的4个文件:
我一直在试图弄清楚如何certutil工作以及如何将这些证书添加到证书数据库中,但我无法理解这一切.
有能力在Dart中启用CA SSL证书的任何人吗?
已解决:感谢评论中的建议,我解决了这个问题.这是我完整设置的要点:https://gist.github.com/stevenroose/e6abde14258971eae982
如果您使用的是飞镖HttpClient(它提供了一个HttpClientRequest),以从服务器请求到另一台服务器,据我可以告诉唯一可用HTTP方法是GET和POST(分别对应于post/ postUrl和get/ getUrl功能).还有办法制作PUT和DELETE要求吗?