Page 1 of 1

Alternative for ReceiveHTTPFile on Linux?

Posted: Sat Apr 27, 2013 12:04 am
by installgentoo
ReceiveHTTPFile is pretty much bugged right now for all platforms. There were alternative solutions I found on the forums for Windows and Mac, but I can't find the alternative for Linux. Is there a way?

I'll just paste the windows and mac versions here incase anybody needs them:

MAC OSX

Code: Select all

Procedure DownloadFromURL(URL.s, Filename.s)
  Protected u = CocoaMessage(0, 0, "NSURL URLWithString:$", @URL)
  Protected d = CocoaMessage(0, CocoaMessage(0, 0, "NSData alloc"), "initWithContentsOfURL:", u)
  If d
    CocoaMessage(0, d, "writeToFile:$", @Filename, "atomically:", #NO)
    CocoaMessage(0, d, "release")
  EndIf
  ProcedureReturn d
EndProcedure
Windows

Code: Select all

URL.S = "http://www.google.com"
DownloadFilename.S = GetTemporaryDirectory() + "Test.htm"

If URLDownloadToFile_(0, @URL, @DownloadFilename, 0, 0) = #S_OK
  MessageRequester("Info", URL + " has been downloaded sucessfully into " + DownloadFilename + "!", #MB_ICONINFORMATION)
Else
  MessageRequester("Error", "The download of " + URL + " failed!", #MB_ICONERROR)
EndIf

Re: Alternative for ReceiveHTTPFile on Linux?

Posted: Sat Apr 27, 2013 2:34 pm
by DarkPlayer
Hi,

you can check my HTTP library: http://www.purebasic.fr/english/viewtop ... 12&t=54297.

It is fully written in PureBasic, but uses some API tricks to workaround the issues/bugs of the PB Network stack, and should work on Windows, Linux and MacOS X. You should take a look at the SimpleHTTP_GET_File() command in the example code, which does pretty much the same as ReceiveHTTPFile(). If you want to download bigger files, you can also use the DownloadDialog_* procedures, which display a progress dialog (screenshot) for the download.

DarkPlayer

Re: Alternative for ReceiveHTTPFile on Linux?

Posted: Sat Apr 27, 2013 2:46 pm
by fiver
libcurl can also be a good way to go, not least because it will do https and all kinds of other handy url related stuff, you almost certainly have it installed already.

Code: Select all

#CURLOPT_URL                    = 10002
#CURLOPT_WRITEFUNCTION          = 20011

CompilerSelect #PB_Compiler_OS
CompilerCase #PB_OS_Linux
;-- More than likely distro dependant try find /usr/lib -name *curl* from a shell
;-- if you can't find it
ImportC "/usr/lib/i386-linux-gnu/libcurl.so.4.2.0"
  curl_easy_init()
  curl_easy_setopt(handle.i, option.i, parameter.i)
  curl_easy_perform(handle.i)
  curl_easy_cleanup(handle.i)
EndImport
CompilerCase #PB_OS_Windows
ImportC "libcurl.lib"
  curl_easy_init()
  curl_easy_setopt(handle.i, option.i, parameter.i)
  curl_easy_perform(handle.i)
  curl_easy_cleanup(handle.i)
EndImport
CompilerEndSelect

;size_t function( char *ptr, size_t size, size_t nmemb, void *userdata)
ProcedureC  LibCurlWriteFunction(*ptr, Size.i, NMemB, *Stream)
  ;-- Retreives utf-8/ascii encoded data
  ;-- & 255 is legacy and not required just use .i??
  Protected SizeProper.i  = Size & 255
  Protected NMemBProper.i = NMemB
  Protected MyDataS.s
  Shared ReceivedData.s

  MyDataS = PeekS(*ptr, -1, #PB_UTF8)
  ReceivedData + MyDataS
  ProcedureReturn  SizeProper * NMemBProper
EndProcedure


Procedure.s LibCurlGetData()
  Shared ReceivedData.s
  Protected ReturnData.s
  ReturnData.s = ReceivedData.s
  ReceivedData.s = ""
  ProcedureReturn ReturnData.s
EndProcedure


Procedure CurlGetUrl(URL$, OutputFile$)
  fp = CreateFile(#PB_Any, OutputFile$)
  len = StringByteLength(URL$, #PB_UTF8) + SizeOf(Character)
  *mem = AllocateMemory(len)
  PokeS(*mem, URL$, len, #PB_UTF8)

  curlsession.i = curl_easy_init()
  If curlsession
    Debug PeekS(*mem)
    curl_easy_setopt(curlsession, #CURLOPT_URL, *mem)
    curl_easy_setopt(curlsession, #CURLOPT_WRITEFUNCTION, @LibCurlWriteFunction())
    curl_easy_perform(curlsession)
    If WriteString(fp, LibCurlGetData(), #PB_UTF8)
      RetVal = 1
    EndIf
    CloseFile(fp)
    curl_easy_cleanup(curlsession)
  EndIf
  ProcedureReturn RetVal
EndProcedure

;--eg
CurlGetUrl("http://www.jurainfo.com/", "MyFile.txt")

Re: Alternative for ReceiveHTTPFile on Linux?

Posted: Sun Apr 28, 2013 8:00 pm
by installgentoo
thanks for these examples.

I ended up using DarkPlayers library, it works well cross platform.