2

我正在开发一个小工具来将我的学校作业上传到我学校提供的专用服务器上,我决定使用ocamland ocurlocaml一个libcurl. 我可以从命令行检索我想做的一切(使用 curl),但无法使用 ocurl 上传文件。

我认为这是因为服务器页面期望multipart/form-data作为内容类型。在阅读了libcurl我使用的文档之后curl_formadd()(这就是我从命令行所做的),但 ocurl 无法做到这一点(我在 中没有找到类似的东西curl.ml

工作卷曲命令行:

curl -v "myhost/upload2.php" --form "fichier1= @file.tar.gz" --form "MAX_FILE_SIZE=1000000" -b cookie

回复

> POST /upload2.php HTTP/1.1
> User-Agent: curl/7.32.0
> Host: myhost
> Accept: */*
> Cookie: PHPSESSID=4qbihrkhode23902q1v988a114; cookie_test=1
> Content-Length: 10563
> Expect: 100-continue
> Content-Type: multipart/form-data; boundary=------------------------53aa2e4a08d1f7c0
> 
< HTTP/1.1 100 Continue
< HTTP/1.1 200 OK
< Date: Wed, 23 Oct 2013 17:34:21 GMT
* Server Apache/2.2.16 (Debian) is not blacklisted
< Server: Apache/2.2.16 (Debian)
< X-Powered-By: PHP/5.3.3-7+squeeze17
< Expires: Thu, 19 Nov 1981 08:52:00 GMT
< Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0
< Pragma: no-cache
< Vary: Accept-Encoding
< Content-Length: 2265
< Content-Type: text/html
<

我最接近的非工作ocaml代码

let fetch connection url =
Printf.printf "Retriving %s ...\n" (url);
flush stdout;
Curl.set_url connection url;
Curl.perform connection;
Curl.set_post connection false
in
Curl.global_init Curl.CURLINIT_GLOBALALL;
let connection = Curl.init() in
Curl.set_verbose connection true;  
let curl_file_option = Curl.CURLFORM_FILECONTENT("fichier1","file.tar.gz",Curl.CONTENTTYPE "application/x-tar") in                                     
Curl.set_maxfilesize connection (Int32.of_int 1000000);                                                                                                      
Curl.set_httppost connection [curl_file_option];
fetch connection (baseURL^"upload2.php");
;;

除了使用 100% 的 CPU 之外,它什么都不做

最后,问题是:你能帮我建立自己的 formdata 函数吗?

4

1 回答 1

0

请仔细阅读 curl 手册

   CURLFORM_FILECONTENT
          followed by a filename, causes that file to be read and its contents used as data in this part. This part does not automatically become a file upload part
          simply because its data was read from a file.

即只使用CURLFORM_FILE。虽然我仍然不明白您的原始代码如何导致 100% CPU。

此外,ocurl 不提供 curl_formadd 只是因为不需要它, set_httppost 已经获取了一个列表。

于 2013-11-07T05:48:58.903 回答