OCaml中HttpRequest的最佳模块是什么

OCaml中HttpRequest的最佳模块是什么,ocaml,ocaml-batteries,Ocaml,Ocaml Batteries,我希望使用OCaml访问Yahoo Finance API。从本质上讲,从雅虎财经获取报价只需要一堆HTTP请求 我应该使用哪个模块? 我希望有异步HTTP请求 有可能使用: 有一个相当完整且有点复杂的实现 有点简单,但缺少一些有用的部分 使用以安装: $ opam install ocsigenserver cohttp 例如,在顶层: try Topdirs.dir_directory (Sys.getenv "OCAML_TOPLEVEL_PATH") with _ -> ()

我希望使用OCaml访问Yahoo Finance API。从本质上讲,从雅虎财经获取报价只需要一堆HTTP请求

我应该使用哪个模块?


我希望有异步HTTP请求

有可能使用:

  • 有一个相当完整且有点复杂的实现
  • 有点简单,但缺少一些有用的部分
使用以安装:

$ opam install ocsigenserver cohttp
例如,在顶层:

try Topdirs.dir_directory (Sys.getenv "OCAML_TOPLEVEL_PATH") with _ -> ();;
#use "topfind";;
#thread;;
#require "ocsigenserver";;
open Lwt

(* a simple function to access the content of the response *)
let content = function
  | { Ocsigen_http_frame.frame_content = Some v } ->
      Ocsigen_stream.string_of_stream 100000 (Ocsigen_stream.get v)
  | _ -> return ""

(* launch both requests in parallel *)
let t = Lwt_list.map_p Ocsigen_http_client.get_url
  [ "http://ocsigen.org/";
    "http://stackoverflow.com/" ]

(* maps the result through the content function *)
let t2 = t >>= Lwt_list.map_p content

(* launch the event loop *)
let result = Lwt_main.run t2
以及使用cohttp:

try Topdirs.dir_directory (Sys.getenv "OCAML_TOPLEVEL_PATH") with _ -> ();;
#use "topfind";;
#require "cohttp.lwt";;
open Lwt

(* a simple function to access the content of the response *)
let content = function
  | Some (_, body) -> Cohttp_lwt_unix.Body.string_of_body body
  | _ -> return ""

(* launch both requests in parallel *)
let t = Lwt_list.map_p Cohttp_lwt_unix.Client.get
  (List.map Uri.of_string
     [ "http://example.org/";
       "http://example2.org/" ])

(* maps the result through the content function *)
let t2 = t >>= Lwt_list.map_p content

(* launch the event loop *)
let v = Lwt_main.run t2

<>注意,对于Jane街道异步库的COTHTP的实现也是可用的:

< P>。对于记录,也有。

你认为“代码> Ocamlnet < /代码> @ BaselestalyKevyt不是真的,我是一个全新的学习者,一无所知。Ocamlnet是最好的吗?我不知道它是否是最好的,但它非常好。@BasileStrynkevitch它是否提供了
异步功能
?我最近一直使用Ocamlnet来实现这一目的,但它没有异步功能。“+../toplevel”这里有点误导:这对于OPAM安装的编译器是不必要的,对于系统编译器也不起作用(你应该使用
试试Topdirs.dir\u目录(Sys.getenv“OCAML\u toplier\u PATH”)和
。@Thomas我认为你的建议很好。我无法运行这些代码,但可以使用
尝试Topdirs.dir\u目录(Sys.getenv“OCAML\u TOPLEVEL\u PATH”)和()
。你能修改一下他的其他密码吗?@Thomas我是个新手,你能告诉我什么是
Some(u,body)
?这是一个与可选配对的模式匹配。在这种情况下,该对的第一个元素被丢弃,第二个元素绑定到名称“body”。如果您想了解更多关于“模式匹配”的信息,请阅读。@JacksonTale这是Lwt.bind的一部分。看见