ex_03.bmx 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. SuperStrict
  2. ' Example showing FTP access using a pair of "performs", a custom Stream, and a debug callback.
  3. '
  4. ' The first perform connects to a valid directory, the second re-uses the connection and attempts to get
  5. ' a non-existent dir.
  6. ' FTP'ing to a directory rather than a specific file results in a directory listing.
  7. '
  8. Framework Net.libcurl
  9. Import BRL.StandardIO
  10. Local curl:TCurlEasy = TCurlEasy.Create()
  11. Local stream:TStringStream = New TStringStream
  12. ' enable verbosity
  13. curl.setOptInt(CURLOPT_VERBOSE, 1)
  14. ' redirect messages via callback
  15. curl.setDebugCallback(debugFunction)
  16. ' redirect data to stream
  17. curl.setWriteStream(stream)
  18. ' ftp connect and initial directory
  19. curl.setOptString(CURLOPT_URL, "ftp.sunsite.dk/pub/")
  20. ' go ! !
  21. Local res:Int = curl.perform()
  22. If res Then
  23. Print "***** " + CurlError(res) + " *****"
  24. End
  25. End If
  26. ' print the result :-)
  27. Print stream.text
  28. stream.text = "" ' reset our stream text...
  29. ' directory change should fail...
  30. curl.setOptString(CURLOPT_URL, "ftp.sunsite.dk/pub/gnu/")
  31. res = curl.perform()
  32. If res Then
  33. Print "***** " + CurlError(res) + " *****"
  34. End If
  35. curl.cleanup()
  36. Print stream.text
  37. End
  38. Function debugFunction:Int(data:Object, msgType:Int, message:String)
  39. ' we only care about server info
  40. If msgType = CURLINFO_HEADER_IN Then
  41. Print message
  42. End If
  43. End Function
  44. ' a simple string stream to collect the text content
  45. Type TStringStream Extends TStream
  46. Method Write:Int( buf:Byte Ptr, count:Int )
  47. text:+ String.FromBytes( buf, count )
  48. Return count
  49. End Method
  50. Field text:String = ""
  51. End Type