fix typo HAVE_DECL_CURLINFO_HTTP_CONNECTCODE
[ocurl.git] / examples / ocurl.ml
blobce22a89314ef8de2fab57aaba93ba72622bf9cba
1 (*
2 * ocurl.ml
4 * Copyright (c) 2003-2008, Lars Nilsson, <lars@quantumchamaleon.com>
5 *)
8 let writer accum data =
9 Buffer.add_string accum data;
10 String.length data
12 let showContent content =
13 Printf.printf "%s" (Buffer.contents content);
14 flush stdout
16 let showInfo connection =
17 Printf.printf "Time: %f\nURL: %s\n"
18 (Curl.get_totaltime connection)
19 (Curl.get_effectiveurl connection)
21 let getContent connection url =
22 Curl.set_url connection url;
23 Curl.perform connection
25 let _ =
26 Curl.global_init Curl.CURLINIT_GLOBALALL;
27 begin
28 let result = Buffer.create 16384
29 and errorBuffer = ref "" in
30 try
31 let connection = Curl.init () in
32 Curl.set_errorbuffer connection errorBuffer;
33 Curl.set_writefunction connection (writer result);
34 Curl.set_followlocation connection true;
35 Curl.set_url connection Sys.argv.(1);
36 Curl.perform connection;
37 showContent result;
38 showInfo connection;
39 Curl.cleanup connection
40 with
41 | Curl.CurlException (reason, code, str) ->
42 Printf.fprintf stderr "Error: %s\n" !errorBuffer
43 | Failure s ->
44 Printf.fprintf stderr "Caught exception: %s\n" s
45 end;
46 Curl.global_cleanup ()