2
0

teststream.pp 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. {
  2. $Id: header,v 1.1 2000/07/13 06:33:45 michael Exp $
  3. This file is part of the Free Component Library (FCL)
  4. Copyright (c) 1999-2000 by the Free Pascal development team
  5. See the file COPYING.FPC, included in this distribution,
  6. for details about the copyright.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  10. **********************************************************************}
  11. {
  12. This example shows how to use curl and write the result to a TStream.
  13. }
  14. {$mode objfpc}
  15. {$H+}
  16. program teststream;
  17. uses classes,libcurl,unixtype;
  18. Function DoWrite(Ptr : Pointer; Size : size_t; nmemb: size_t; Data : Pointer) : size_t;cdecl;
  19. begin
  20. Result:=TStream(Data).Write(Ptr^,Size*nmemb);
  21. end;
  22. Var
  23. f : TFileStream;
  24. URL : Pchar = 'http://www.freepascal.org';
  25. hCurl : pCurl;
  26. begin
  27. F:=TFileStream.Create('fpc.html',fmCreate);
  28. Try
  29. hCurl:= curl_easy_init;
  30. if Assigned(hCurl) then
  31. begin
  32. curl_easy_setopt(hCurl,CURLOPT_VERBOSE, [True]);
  33. curl_easy_setopt(hCurl,CURLOPT_URL,[URL]);
  34. curl_easy_setopt(hCurl,CURLOPT_WRITEFUNCTION,[@DoWrite]);
  35. curl_easy_setopt(hCurl,CURLOPT_WRITEDATA,[Pointer(F)]);
  36. curl_easy_perform(hCurl);
  37. curl_easy_cleanup(hCurl);
  38. end;
  39. Finally
  40. F.Free;
  41. end;
  42. end.