我编写了以下 OCaml 代码,以便向 https 服务器发出 POST 请求。
open Lwt
open Cohttp
open Cohttp_lwt_unix
try
let headers = Header.init ()
|> fun h -> Header.add h "content-type" "application/json" in
let body = Client.post
~headers
~body:(body_of_credentials nickname secret)
(uri_of_project project)
>>= fun (response, body) ->
let code = response |> Response.status |> Code.code_of_status in
Printf.printf "Response code: %d\n" code;
Printf.printf "Headers: %s\n" (response |> Response.headers |> Header.to_string);
body |> Cohttp_lwt.Body.to_string >|= fun body ->
Printf.printf "Body of length: %d\n" (String.length body);
body
in
let body = Lwt_main.run body in
print_endline ("Received body\n" ^ body)
with
| Tls_lwt.Tls_alert e ->
print_endline (Tls.Packet.alert_type_to_string e);
exit 1
但是当使用 CONDUIT_TLS=native CONDUIT_DEBUG=true COHTTP_DEBUG=true
执行它时,我得到以下响应:
Selected TLS library: Native
Resolver system: https://my_server/auth/tokens/ ((name https) (port 443) (tls true)) -> (TCP ((V4 xx.xxx.xx.xxx) 443))
HANDSHAKE_FAILURE
我已经阅读了所有谷歌搜索结果(文档、ocaml/cohttp/ocaml-tls 列表和堆栈溢出问题)我能找到它,但没有任何帮助,所以我想从头开始。
如何获得有关此故障的更多详细信息?
如果有帮助,我使用以下 opam 配置:
"lwt" {>= "4.1.0"}
"cohttp" {>= "1.1.1"}
"cohttp-lwt"
"cohttp-lwt-unix" {>= "1.1.1"}
"tls" {>= "0.9.2"}
编辑:
正如@ivg 所建议的,我尝试使用 CONDUIT_TLS=openssl
但随后出现以下错误:
Selected TLS library: OpenSSL
Resolver system: https://my_server/auth/tokens/ ((name https) (port 443) (tls true)) -> (TCP ((V4 xx.xxx.xx.xxx) 443))
...: internal error, uncaught exception:
SSL: Method error
编辑²:
正如以下讨论中所建议的那样:github.com/savonet/ocaml-ssl/issues/40我向 ssl-0.5.5
添加了一个 opam pin:opam pin add ssl 0.5.5
以修复此错误。现在我可以将请求发布到我的 https 服务器,但不能使用 tls 的纯 ocaml 实现。
最佳答案
您收到此警报是因为握手过程(身份验证)失败。此警报意味着对等方(服务器或客户端)未通过身份验证,因此无法建立安全连接。
要调试问题,我建议首先确保使用传统工具一切正常,例如 openssl
、wget
或 curl
。
如果您确定您的配置没有问题,并且这是 ocaml-tls
部分的问题,我建议使用 ocaml-tls< 的低级接口(interface)
库。显然,conduit
不公开或使用 ocaml-tls
的任何跟踪功能,因此别无选择。
警报类型是从两个更丰富的 fatal
和 error
类型预测的,它们包含有关问题性质的更多信息,请参见。创建警报的 following code 并考虑导致 HANDSHAKE_FAILURE
警报的可能输入值。
要访问特定错误或警报,我建议您使用 ocaml-tls
的跟踪功能。源存储库中有 are examples 已经启用跟踪并且应该提供足够的信息。我希望这些示例适合您的用例。
关于unix - 使用 ocaml-cohttp Client.post 方法请求 HTTPS 服务器时如何调试 HANDSHAKE_FAILURE? (为什么我会收到这个错误?),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52830891/