2

我正在尝试Http_client.Convenience.http_post使用http post request.


API 相当简单:

val http_post:字符串->(字符串*字符串)列表->字符串

使用给定的 URL 执行“POST”请求并返回响应正文。该列表包含随 POST 请求发送的参数。


我想做的是构造一个http post请求以通过 获取航班信息google flights,解释如下part 1http ://www.nohup.in/blog/using-json-google-flights

为了保持 的格式Post request,我截取了如下截图:

在此处输入图像描述


所以最后,我Http_client.Convenience.http_post为它构造了一个:

open Http_client.Convenience;;

let post_para = [("(Request-Line)", "POST /flights/rpc HTTP/1.1");
         ("Host", "www.google.com");
         ("Content-Type", "application/json; charset=utf-8");
         ("X-GWT-Permutation", "0BB89375061712D90759336B50687E78");
         ("X-GWT-Module-Base", "http://www.google.com/flights/static/");
         ("Referer", "http://www.google.com/flights/");
         ("Content-Length", "275");
         ("Cookie", "PREF=ID=2dc218fc830df28d:U=29aaf343dd519bca:FF=0:TM=1307225398:LM=1308065727:GM=1:S=RWC3dYzVvVSpkrlz; NID=52=VTp1QILW1ntPlrkLx7yLUtOYhchNk35G4Lk35KBd7A3lCznVV5glz7lwDoDP2RkjtTJVNZSomv3iffPqiJz4oXfpoph3ljb2eInGOe-FwosvrmSXPpnLkEWxMHIbuaid; S=travel-flights=YFCjkd9M9h3Z_uEqBmgynA");
         ("Pragma", "no-cache");
         ("Cache-Control", "no-cache");
         ("data", "[,[[,\"fs\",\"[,[,[\"SJC\"]\n,\"2012-04-05\",[\"EWR\",\"JFK\",\"LGA\"]\n,\"2012-04-12\"]\n]\n\"]],[,[[,\"b_ca\",\"54\"],[,\"f_ut\",\"search;f=SJC;t=EWR,JFK,LGA;d=2012-04-05;r=2012-04-12\"],[,\"b_lr\",\"11:36\"],[,\"b_lr\",\"1:1528\"],[,\"b_lr\",\"2:1827\"],[,\"b_qu\",\"3\"],[,\"b_qc\",\"1\"]]]]")];;

let search () = try (http_post "http://www.google.com/flights/rpc" post_para) with
    Http_client.Http_error (id, msg) -> msg;;

let _ = print_endline (search());;

当我运行它时,它只会给我以下信息error html page

<HTML>
   <HEAD>
     <TITLE>Internal Server Error</TITLE>
   </HEAD>
   <BODY BGCOLOR="#FFFFFF" TEXT="#000000">
     <H1>Internal Server Error</H1>
     <H2>Error 500</H2>
   </BODY>
</HTML>

谁能告诉我为什么?我的怎么了http_post

4

1 回答 1

3

从 post_para 中删除它,它不是 HTTP 标头,OCamlnet 会自动为您发送:"(Request-Line)", "POST /flights/rpc HTTP/1.1".

要分别发送标头和 POST 数据,您需要在 http_call 对象上使用set_request_header设置标头。

此外,OCamlnet 中的便利模块会将数据作为 application/x-www-form-urlencoded 发送,但我认为您需要按原样发送数据。您可以使用Http_client.post_raw来做到这一点。

于 2013-06-11T15:29:09.103 回答