[Solved] ReceiveHTTPFile fails for me

Just starting out? Need help? Post your questions and find answers here.
User avatar
Fangbeast
PureBasic Protozoa
PureBasic Protozoa
Posts: 4799
Joined: Fri Apr 25, 2003 3:08 pm
Location: Not Sydney!!! (Bad water, no goats)

[Solved] ReceiveHTTPFile fails for me

Post by Fangbeast »

Trying to download book information to a file on disk for processing but it fails for me. if I post "http://www.omdbapi.com/?t=star wars" into the browser manually, it returns information.

Can anyone shed some light as to where I am going wrong?

Code: Select all

InitNetwork()

UsePNGImageDecoder()

Enumeration 1
  #Window_OMDBAPI
EndEnumeration

#WindowIndex = #PB_Compiler_EnumerationValue

Enumeration 1
  ; Window_OMDBAPI
  #Gadget_OMDBAPI_cDetails
  #Gadget_OMDBAPI_LabelTitle
  #Gadget_OMDBAPI_Title
  #Gadget_OMDBAPI_LabelYear
  #Gadget_OMDBAPI_Year
  #Gadget_OMDBAPI_LabelRated
  #Gadget_OMDBAPI_Rated
  #Gadget_OMDBAPI_LabelReleased
  #Gadget_OMDBAPI_Released
  #Gadget_OMDBAPI_LabelRunTime
  #Gadget_OMDBAPI_RunTime
  #Gadget_OMDBAPI_LabelGenre
  #Gadget_OMDBAPI_Genre
  #Gadget_OMDBAPI_LabelWriter
  #Gadget_OMDBAPI_Writer
  #Gadget_OMDBAPI_LabelDirector
  #Gadget_OMDBAPI_Director
  #Gadget_OMDBAPI_LabelActors
  #Gadget_OMDBAPI_Actors
  #Gadget_OMDBAPI_LabelPlot
  #Gadget_OMDBAPI_Plot
  #Gadget_OMDBAPI_LabelPicture
  #Gadget_OMDBAPI_Picture
  #Gadget_OMDBAPI_LabelimdbRating
  #Gadget_OMDBAPI_ImdbRating
  #Gadget_OMDBAPI_LabelImdbVotes
  #Gadget_OMDBAPI_ImdbVotes
  #Gadget_OMDBAPI_LabelimdbID
  #Gadget_OMDBAPI_ImdbID
  #Gadget_OMDBAPI_LabelImdbObject
  #Gadget_OMDBAPI_ImdbObject
  #Gadget_OMDBAPI_cControl
  #Gadget_OMDBAPI_Save
  #Gadget_OMDBAPI_Print
  #Gadget_OMDBAPI_Exit
  #Gadget_OMDBAPI_Search
  #Gadget_OMDBAPI_Run
EndEnumeration

#GadgetIndex = #PB_Compiler_EnumerationValue

Enumeration 1
  #Image_OMDBAPI_Picture
  #Image_OMDBAPI_Save
  #Image_OMDBAPI_Print
  #Image_OMDBAPI_Exit
  #Image_OMDBAPI_Run
EndEnumeration

#ImageIndex = #PB_Compiler_EnumerationValue

CatchImage(#Image_OMDBAPI_Picture,?_OPT_OMDBAPI_Picture)
CatchImage(#Image_OMDBAPI_Save,?_OPT_OMDBAPI_Save)
CatchImage(#Image_OMDBAPI_Print,?_OPT_OMDBAPI_Print)
CatchImage(#Image_OMDBAPI_Exit,?_OPT_OMDBAPI_Exit)
CatchImage(#Image_OMDBAPI_Run,?_OPT_OMDBAPI_Run)

DataSection
  _OPT_OMDBAPI_Picture:IncludeBinary "Images\About320x80.png"
  _OPT_OMDBAPI_Save:IncludeBinary "Images\save32x32.ico"
  _OPT_OMDBAPI_Print:IncludeBinary "Images\printer32x32.ico"
  _OPT_OMDBAPI_Exit:IncludeBinary "Images\Exit32x32-n.ico"
  _OPT_OMDBAPI_Run:IncludeBinary "Images\run32x32.ico"
EndDataSection

Procedure.l Window_OMDBAPI()
  If OpenWindow(#Window_OMDBAPI,193,85,750,363,"OMDB API testing",#PB_Window_SystemMenu|#PB_Window_MinimizeGadget|#PB_Window_ScreenCentered|#PB_Window_Invisible)
      SetWindowColor(#Window_OMDBAPI,$808080)
      ContainerGadget(#Gadget_OMDBAPI_cDetails,5,5,740,295,#PB_Container_Flat|#PB_Container_BorderLess)
        SetGadgetColor(#Gadget_OMDBAPI_cDetails,#PB_Gadget_BackColor,$8080FF)
      TextGadget(#Gadget_OMDBAPI_LabelTitle,10,10,70,20,"Title",#PB_Text_Center)
        SetGadgetColor(#Gadget_OMDBAPI_LabelTitle,#PB_Gadget_BackColor,$8080FF)
        SetGadgetFont(#Gadget_OMDBAPI_LabelTitle,LoadFont(#Gadget_OMDBAPI_LabelTitle,"Comic Sans MS",10,0))
      StringGadget(#Gadget_OMDBAPI_Title,80,5,650,25,"",#PB_String_BorderLess)
        SetGadgetFont(#Gadget_OMDBAPI_Title,LoadFont(#Gadget_OMDBAPI_Title,"Comic Sans MS",10,0))
      TextGadget(#Gadget_OMDBAPI_LabelYear,10,40,70,20,"Year",#PB_Text_Center)
        SetGadgetColor(#Gadget_OMDBAPI_LabelYear,#PB_Gadget_BackColor,$8080FF)
        SetGadgetFont(#Gadget_OMDBAPI_LabelYear,LoadFont(#Gadget_OMDBAPI_LabelYear,"Comic Sans MS",10,0))
      StringGadget(#Gadget_OMDBAPI_Year,80,35,105,25,"",#PB_String_BorderLess)
        SetGadgetFont(#Gadget_OMDBAPI_Year,LoadFont(#Gadget_OMDBAPI_Year,"Comic Sans MS",10,0))
      TextGadget(#Gadget_OMDBAPI_LabelRated,190,40,70,20,"Rated",#PB_Text_Center)
        SetGadgetColor(#Gadget_OMDBAPI_LabelRated,#PB_Gadget_BackColor,$8080FF)
        SetGadgetFont(#Gadget_OMDBAPI_LabelRated,LoadFont(#Gadget_OMDBAPI_LabelRated,"Comic Sans MS",10,0))
      StringGadget(#Gadget_OMDBAPI_Rated,260,35,105,25,"",#PB_String_BorderLess)
        SetGadgetFont(#Gadget_OMDBAPI_Rated,LoadFont(#Gadget_OMDBAPI_Rated,"Comic Sans MS",10,0))
      TextGadget(#Gadget_OMDBAPI_LabelReleased,370,40,70,20,"Released",#PB_Text_Center)
        SetGadgetColor(#Gadget_OMDBAPI_LabelReleased,#PB_Gadget_BackColor,$8080FF)
        SetGadgetFont(#Gadget_OMDBAPI_LabelReleased,LoadFont(#Gadget_OMDBAPI_LabelReleased,"Comic Sans MS",10,0))
      StringGadget(#Gadget_OMDBAPI_Released,440,35,105,25,"",#PB_String_BorderLess)
        SetGadgetFont(#Gadget_OMDBAPI_Released,LoadFont(#Gadget_OMDBAPI_Released,"Comic Sans MS",10,0))
      TextGadget(#Gadget_OMDBAPI_LabelRunTime,550,40,70,20,"Run time",#PB_Text_Center)
        SetGadgetColor(#Gadget_OMDBAPI_LabelRunTime,#PB_Gadget_BackColor,$8080FF)
        SetGadgetFont(#Gadget_OMDBAPI_LabelRunTime,LoadFont(#Gadget_OMDBAPI_LabelRunTime,"Comic Sans MS",10,0))
      StringGadget(#Gadget_OMDBAPI_RunTime,620,35,110,25,"",#PB_String_BorderLess)
        SetGadgetFont(#Gadget_OMDBAPI_RunTime,LoadFont(#Gadget_OMDBAPI_RunTime,"Comic Sans MS",10,0))
      TextGadget(#Gadget_OMDBAPI_LabelGenre,10,70,70,20,"Genre",#PB_Text_Center)
        SetGadgetColor(#Gadget_OMDBAPI_LabelGenre,#PB_Gadget_BackColor,$8080FF)
        SetGadgetFont(#Gadget_OMDBAPI_LabelGenre,LoadFont(#Gadget_OMDBAPI_LabelGenre,"Comic Sans MS",10,0))
      StringGadget(#Gadget_OMDBAPI_Genre,80,65,650,25,"",#PB_String_BorderLess)
        SetGadgetFont(#Gadget_OMDBAPI_Genre,LoadFont(#Gadget_OMDBAPI_Genre,"Comic Sans MS",10,0))
      TextGadget(#Gadget_OMDBAPI_LabelWriter,10,100,70,20,"Writers",#PB_Text_Center)
        SetGadgetColor(#Gadget_OMDBAPI_LabelWriter,#PB_Gadget_BackColor,$8080FF)
        SetGadgetFont(#Gadget_OMDBAPI_LabelWriter,LoadFont(#Gadget_OMDBAPI_LabelWriter,"Comic Sans MS",10,0))
      StringGadget(#Gadget_OMDBAPI_Writer,80,95,285,25,"",#PB_String_BorderLess)
        SetGadgetFont(#Gadget_OMDBAPI_Writer,LoadFont(#Gadget_OMDBAPI_Writer,"Comic Sans MS",10,0))
      TextGadget(#Gadget_OMDBAPI_LabelDirector,370,100,70,20,"Director",#PB_Text_Center)
        SetGadgetColor(#Gadget_OMDBAPI_LabelDirector,#PB_Gadget_BackColor,$8080FF)
        SetGadgetFont(#Gadget_OMDBAPI_LabelDirector,LoadFont(#Gadget_OMDBAPI_LabelDirector,"Comic Sans MS",10,0))
      StringGadget(#Gadget_OMDBAPI_Director,440,95,290,25,"",#PB_String_BorderLess)
        SetGadgetFont(#Gadget_OMDBAPI_Director,LoadFont(#Gadget_OMDBAPI_Director,"Comic Sans MS",10,0))
      TextGadget(#Gadget_OMDBAPI_LabelActors,10,130,70,20,"Actors",#PB_Text_Center)
        SetGadgetColor(#Gadget_OMDBAPI_LabelActors,#PB_Gadget_BackColor,$8080FF)
        SetGadgetFont(#Gadget_OMDBAPI_LabelActors,LoadFont(#Gadget_OMDBAPI_LabelActors,"Comic Sans MS",10,0))
      StringGadget(#Gadget_OMDBAPI_Actors,80,125,650,25,"",#PB_String_BorderLess)
        SetGadgetFont(#Gadget_OMDBAPI_Actors,LoadFont(#Gadget_OMDBAPI_Actors,"Comic Sans MS",10,0))
      TextGadget(#Gadget_OMDBAPI_LabelPlot,10,155,70,20,"Plot",#PB_Text_Center)
        SetGadgetColor(#Gadget_OMDBAPI_LabelPlot,#PB_Gadget_BackColor,$8080FF)
        SetGadgetFont(#Gadget_OMDBAPI_LabelPlot,LoadFont(#Gadget_OMDBAPI_LabelPlot,"Comic Sans MS",10,0))
      EditorGadget(#Gadget_OMDBAPI_Plot,80,155,285,100,#PB_Editor_WordWrap)
        SetGadgetFont(#Gadget_OMDBAPI_Plot,LoadFont(#Gadget_OMDBAPI_Plot,"Comic Sans MS",10,0))
      TextGadget(#Gadget_OMDBAPI_LabelPicture,370,155,70,20,"Picture",#PB_Text_Center)
        SetGadgetColor(#Gadget_OMDBAPI_LabelPicture,#PB_Gadget_BackColor,$8080FF)
        SetGadgetFont(#Gadget_OMDBAPI_LabelPicture,LoadFont(#Gadget_OMDBAPI_LabelPicture,"Comic Sans MS",10,0))
      ImageGadget(#Gadget_OMDBAPI_Picture,440,155,290,100,ImageID(#Image_OMDBAPI_Picture))
      ResizeGadget(#Gadget_OMDBAPI_Picture,440,155,290,100)
      ResizeImage(#Image_OMDBAPI_Picture,290,100)
      SetGadgetState(#Gadget_OMDBAPI_Picture,ImageID(#Image_OMDBAPI_Picture))
      TextGadget(#Gadget_OMDBAPI_LabelimdbRating,10,265,70,20,"Rating",#PB_Text_Center)
        SetGadgetColor(#Gadget_OMDBAPI_LabelimdbRating,#PB_Gadget_BackColor,$8080FF)
        SetGadgetFont(#Gadget_OMDBAPI_LabelimdbRating,LoadFont(#Gadget_OMDBAPI_LabelimdbRating,"Comic Sans MS",10,0))
      StringGadget(#Gadget_OMDBAPI_ImdbRating,80,260,105,25,"",#PB_String_BorderLess)
        SetGadgetFont(#Gadget_OMDBAPI_ImdbRating,LoadFont(#Gadget_OMDBAPI_ImdbRating,"Comic Sans MS",10,0))
      TextGadget(#Gadget_OMDBAPI_LabelImdbVotes,190,265,70,20,"Votes",#PB_Text_Center)
        SetGadgetColor(#Gadget_OMDBAPI_LabelImdbVotes,#PB_Gadget_BackColor,$8080FF)
        SetGadgetFont(#Gadget_OMDBAPI_LabelImdbVotes,LoadFont(#Gadget_OMDBAPI_LabelImdbVotes,"Comic Sans MS",10,0))
      StringGadget(#Gadget_OMDBAPI_ImdbVotes,260,260,105,25,"",#PB_String_BorderLess)
        SetGadgetFont(#Gadget_OMDBAPI_ImdbVotes,LoadFont(#Gadget_OMDBAPI_ImdbVotes,"Comic Sans MS",10,0))
      TextGadget(#Gadget_OMDBAPI_LabelimdbID,370,265,70,20,"Imdb ID",#PB_Text_Center)
        SetGadgetColor(#Gadget_OMDBAPI_LabelimdbID,#PB_Gadget_BackColor,$8080FF)
        SetGadgetFont(#Gadget_OMDBAPI_LabelimdbID,LoadFont(#Gadget_OMDBAPI_LabelimdbID,"Comic Sans MS",10,0))
      StringGadget(#Gadget_OMDBAPI_ImdbID,440,260,105,25,"",#PB_String_BorderLess)
        SetGadgetFont(#Gadget_OMDBAPI_ImdbID,LoadFont(#Gadget_OMDBAPI_ImdbID,"Comic Sans MS",10,0))
      TextGadget(#Gadget_OMDBAPI_LabelImdbObject,550,265,70,20,"Object",#PB_Text_Center)
        SetGadgetColor(#Gadget_OMDBAPI_LabelImdbObject,#PB_Gadget_BackColor,$8080FF)
        SetGadgetFont(#Gadget_OMDBAPI_LabelImdbObject,LoadFont(#Gadget_OMDBAPI_LabelImdbObject,"Comic Sans MS",10,0))
      StringGadget(#Gadget_OMDBAPI_ImdbObject,620,260,110,25,"",#PB_String_BorderLess)
        SetGadgetFont(#Gadget_OMDBAPI_ImdbObject,LoadFont(#Gadget_OMDBAPI_ImdbObject,"Comic Sans MS",10,0))
      CloseGadgetList()
      ContainerGadget(#Gadget_OMDBAPI_cControl,5,305,740,55,#PB_Container_Flat)
        SetGadgetColor(#Gadget_OMDBAPI_cControl,#PB_Gadget_BackColor,$8080FF)
      ButtonImageGadget(#Gadget_OMDBAPI_Save,5,5,40,40,ImageID(#Image_OMDBAPI_Save))
      ButtonImageGadget(#Gadget_OMDBAPI_Print,40,5,40,40,ImageID(#Image_OMDBAPI_Print))
      ButtonImageGadget(#Gadget_OMDBAPI_Exit,80,5,40,40,ImageID(#Image_OMDBAPI_Exit))
      StringGadget(#Gadget_OMDBAPI_Search,130,20,555,25,"",#PB_String_BorderLess)
        SetGadgetFont(#Gadget_OMDBAPI_Search,LoadFont(#Gadget_OMDBAPI_Search,"Comic Sans MS",10,0))
      ButtonImageGadget(#Gadget_OMDBAPI_Run,695,5,40,40,ImageID(#Image_OMDBAPI_Run))
      CloseGadgetList()
      HideWindow(#Window_OMDBAPI,0)
    ProcedureReturn WindowID(#Window_OMDBAPI)
  EndIf
EndProcedure

Declare SearchForMovie()

Global HTTPHeader.s     = "http://www.omdbapi.com/?t="
Global CurrentDir.s     = GetCurrentDirectory()
Global MovieFilename.s  = CurrentDir.s + "OMDBAPI.txt"

Procedure SearchForMovie()
  SearchText.s = GetGadgetText(#Gadget_OMDBAPI_Search): Debug HTTPHeader.s + SearchText.s
  If SearchText.s <> ""
    If FileSize(MovieFilename.s) = -1
      If ReceiveHTTPFile(HTTPHeader.s + SearchText.s, MovieFilename.s) <> 0
        MovieFile.i = ReadFile(#PB_Any, MovieFilename.s)
        If MovieFile.i
          While Eof(MovieFile.i) = 0
            Debug ReadString(MovieFile.i)
          Wend
          CloseFile(MovieFile.i)
        EndIf
      Else
        Debug "Failed to retreive movie info file"
      EndIf
    Else
      DeleteFile(MovieFilename.s)
    EndIf
  Else
    Debug "Search text is empty"
  EndIf
EndProcedure
  
If Window_OMDBAPI()
  quitOMDBAPI = 0
  Repeat
    EventID  = WaitWindowEvent()
    MenuID   = EventMenu()
    GadgetID = EventGadget()
    WindowID = EventWindow()
    Select EventID
      Case #PB_Event_CloseWindow
        Select WindowID
          Case #Window_OMDBAPI            : quitOMDBAPI = 1
        EndSelect
      Case #PB_Event_Gadget
        Select GadgetID
          Case #Gadget_OMDBAPI_Exit       : quitOMDBAPI = 1
          Case #Gadget_OMDBAPI_Run        : SearchForMovie()
        EndSelect
    EndSelect
  Until quitOMDBAPI
  CloseWindow(#Window_OMDBAPI)
EndIf
End
Last edited by Fangbeast on Tue Apr 26, 2016 7:50 am, edited 1 time in total.
Amateur Radio/VK3HAF, (D-STAR/DMR and more), Arduino, ESP32, Coding, Crochet
normeus
Enthusiast
Enthusiast
Posts: 486
Joined: Fri Apr 20, 2012 8:09 pm
Contact:

Re: ReceiveHTTPFile fails for me

Post by normeus »

Code: Select all

"http://www.omdbapi.com/?t=star_wars"
no spaces
google Translate;Makes my jokes fall flat- Fait mes blagues tombent à plat- Machte meine Witze verpuffen- Eh cumpari ci vo sunari
User avatar
Fangbeast
PureBasic Protozoa
PureBasic Protozoa
Posts: 4799
Joined: Fri Apr 25, 2003 3:08 pm
Location: Not Sydney!!! (Bad water, no goats)

Re: ReceiveHTTPFile fails for me

Post by Fangbeast »

normeus wrote:

Code: Select all

"http://www.omdbapi.com/?t=star_wars"
no spaces
Thanks for that.

Hmm, why does it work with spaces when I put it into the browser? Does it encode the url behind the scenes then?

This should solve my problem for my wife as I seem to be doing all the work manually for her movie program (ROFLMAO).
Amateur Radio/VK3HAF, (D-STAR/DMR and more), Arduino, ESP32, Coding, Crochet
User avatar
Fangbeast
PureBasic Protozoa
PureBasic Protozoa
Posts: 4799
Joined: Fri Apr 25, 2003 3:08 pm
Location: Not Sydney!!! (Bad water, no goats)

Re: ReceiveHTTPFile fails for me

Post by Fangbeast »

Anyone know how to receive this directly into a string buffer? I could then PEEKS and work with it directly.

Here is the almost working code (probably buggy as hell!!)

Code: Select all

InitNetwork()

UseJPEGImageDecoder()
UsePNGImageDecoder()

Enumeration 1
  #Window_OMDBAPI
EndEnumeration

#WindowIndex = #PB_Compiler_EnumerationValue

Enumeration 1
  ;Window_OMDBAPI
  #Gadget_OMDBAPI_cDetails
  #Gadget_OMDBAPI_LabelTitle
  #Gadget_OMDBAPI_Title
  #Gadget_OMDBAPI_LabelYear
  #Gadget_OMDBAPI_Year
  #Gadget_OMDBAPI_LabelRated
  #Gadget_OMDBAPI_Rated
  #Gadget_OMDBAPI_LabelReleased
  #Gadget_OMDBAPI_Released
  #Gadget_OMDBAPI_LabelRunTime
  #Gadget_OMDBAPI_RunTime
  #Gadget_OMDBAPI_LabelGenre
  #Gadget_OMDBAPI_Genre
  #Gadget_OMDBAPI_LabelWriter
  #Gadget_OMDBAPI_Writer
  #Gadget_OMDBAPI_LabelDirector
  #Gadget_OMDBAPI_Director
  #Gadget_OMDBAPI_LabelActors
  #Gadget_OMDBAPI_Actors
  #Gadget_OMDBAPI_LabelPlot
  #Gadget_OMDBAPI_Plot
  #Gadget_OMDBAPI_LabelPicture
  #Gadget_OMDBAPI_Picture
  #Gadget_OMDBAPI_LabelimdbRating
  #Gadget_OMDBAPI_ImdbRating
  #Gadget_OMDBAPI_LabelImdbVotes
  #Gadget_OMDBAPI_ImdbVotes
  #Gadget_OMDBAPI_LabelimdbID
  #Gadget_OMDBAPI_ImdbID
  #Gadget_OMDBAPI_LabelImdbObject
  #Gadget_OMDBAPI_ImdbObject
  #Gadget_OMDBAPI_cControl
  #Gadget_OMDBAPI_Save
  #Gadget_OMDBAPI_Print
  #Gadget_OMDBAPI_Exit
  #Gadget_OMDBAPI_Search
  #Gadget_OMDBAPI_Run
EndEnumeration

#GadgetIndex = #PB_Compiler_EnumerationValue

; Image Constants

Enumeration 1
  #Image_OMDBAPI_Picture
  #Image_OMDBAPI_Save
  #Image_OMDBAPI_Print
  #Image_OMDBAPI_Exit
  #Image_OMDBAPI_Run
EndEnumeration

#ImageIndex = #PB_Compiler_EnumerationValue

; Load Images

CatchImage(#Image_OMDBAPI_Picture,  ?_OPT_OMDBAPI_Picture)
CatchImage(#Image_OMDBAPI_Save,     ?_OPT_OMDBAPI_Save)
CatchImage(#Image_OMDBAPI_Print,    ?_OPT_OMDBAPI_Print)
CatchImage(#Image_OMDBAPI_Exit,     ?_OPT_OMDBAPI_Exit)
CatchImage(#Image_OMDBAPI_Run,      ?_OPT_OMDBAPI_Run)

DataSection
  _OPT_OMDBAPI_Picture  : IncludeBinary "Images\About320x80.png"
  _OPT_OMDBAPI_Save     : IncludeBinary "Images\save32x32.ico"
  _OPT_OMDBAPI_Print    : IncludeBinary "Images\printer32x32.ico"
  _OPT_OMDBAPI_Exit     : IncludeBinary "Images\Exit32x32-n.ico"
  _OPT_OMDBAPI_Run      : IncludeBinary "Images\run32x32.ico"
EndDataSection

Procedure.l Window_OMDBAPI()
  If OpenWindow(#Window_OMDBAPI,193,85,750,623,"OMDB API testing",#PB_Window_SystemMenu|#PB_Window_MinimizeGadget|#PB_Window_ScreenCentered|#PB_Window_Invisible)
      SetWindowColor(#Window_OMDBAPI,$808080)
      ContainerGadget(#Gadget_OMDBAPI_cDetails,5,5,740,555,#PB_Container_Flat|#PB_Container_BorderLess)
        SetGadgetColor(#Gadget_OMDBAPI_cDetails,#PB_Gadget_BackColor,$8080FF)
      TextGadget(#Gadget_OMDBAPI_LabelTitle,10,10,70,20,"Title",#PB_Text_Center)
        SetGadgetColor(#Gadget_OMDBAPI_LabelTitle,#PB_Gadget_BackColor,$8080FF)
        SetGadgetFont(#Gadget_OMDBAPI_LabelTitle,LoadFont(#Gadget_OMDBAPI_LabelTitle,"Comic Sans MS",10,0))
      StringGadget(#Gadget_OMDBAPI_Title,80,5,650,25,"",#PB_String_BorderLess)
        SetGadgetFont(#Gadget_OMDBAPI_Title,LoadFont(#Gadget_OMDBAPI_Title,"Comic Sans MS",10,0))
      TextGadget(#Gadget_OMDBAPI_LabelYear,10,40,70,20,"Year",#PB_Text_Center)
        SetGadgetColor(#Gadget_OMDBAPI_LabelYear,#PB_Gadget_BackColor,$8080FF)
        SetGadgetFont(#Gadget_OMDBAPI_LabelYear,LoadFont(#Gadget_OMDBAPI_LabelYear,"Comic Sans MS",10,0))
      StringGadget(#Gadget_OMDBAPI_Year,80,35,105,25,"",#PB_String_BorderLess)
        SetGadgetFont(#Gadget_OMDBAPI_Year,LoadFont(#Gadget_OMDBAPI_Year,"Comic Sans MS",10,0))
      TextGadget(#Gadget_OMDBAPI_LabelRated,190,40,70,20,"Rated",#PB_Text_Center)
        SetGadgetColor(#Gadget_OMDBAPI_LabelRated,#PB_Gadget_BackColor,$8080FF)
        SetGadgetFont(#Gadget_OMDBAPI_LabelRated,LoadFont(#Gadget_OMDBAPI_LabelRated,"Comic Sans MS",10,0))
      StringGadget(#Gadget_OMDBAPI_Rated,260,35,105,25,"",#PB_String_BorderLess)
        SetGadgetFont(#Gadget_OMDBAPI_Rated,LoadFont(#Gadget_OMDBAPI_Rated,"Comic Sans MS",10,0))
      TextGadget(#Gadget_OMDBAPI_LabelReleased,370,40,70,20,"Released",#PB_Text_Center)
        SetGadgetColor(#Gadget_OMDBAPI_LabelReleased,#PB_Gadget_BackColor,$8080FF)
        SetGadgetFont(#Gadget_OMDBAPI_LabelReleased,LoadFont(#Gadget_OMDBAPI_LabelReleased,"Comic Sans MS",10,0))
      StringGadget(#Gadget_OMDBAPI_Released,440,35,105,25,"",#PB_String_BorderLess)
        SetGadgetFont(#Gadget_OMDBAPI_Released,LoadFont(#Gadget_OMDBAPI_Released,"Comic Sans MS",10,0))
      TextGadget(#Gadget_OMDBAPI_LabelRunTime,550,40,70,20,"Run time",#PB_Text_Center)
        SetGadgetColor(#Gadget_OMDBAPI_LabelRunTime,#PB_Gadget_BackColor,$8080FF)
        SetGadgetFont(#Gadget_OMDBAPI_LabelRunTime,LoadFont(#Gadget_OMDBAPI_LabelRunTime,"Comic Sans MS",10,0))
      StringGadget(#Gadget_OMDBAPI_RunTime,620,35,110,25,"",#PB_String_BorderLess)
        SetGadgetFont(#Gadget_OMDBAPI_RunTime,LoadFont(#Gadget_OMDBAPI_RunTime,"Comic Sans MS",10,0))
      TextGadget(#Gadget_OMDBAPI_LabelGenre,10,70,70,20,"Genre",#PB_Text_Center)
        SetGadgetColor(#Gadget_OMDBAPI_LabelGenre,#PB_Gadget_BackColor,$8080FF)
        SetGadgetFont(#Gadget_OMDBAPI_LabelGenre,LoadFont(#Gadget_OMDBAPI_LabelGenre,"Comic Sans MS",10,0))
      StringGadget(#Gadget_OMDBAPI_Genre,80,65,650,25,"",#PB_String_BorderLess)
        SetGadgetFont(#Gadget_OMDBAPI_Genre,LoadFont(#Gadget_OMDBAPI_Genre,"Comic Sans MS",10,0))
      TextGadget(#Gadget_OMDBAPI_LabelWriter,10,100,70,20,"Writers",#PB_Text_Center)
        SetGadgetColor(#Gadget_OMDBAPI_LabelWriter,#PB_Gadget_BackColor,$8080FF)
        SetGadgetFont(#Gadget_OMDBAPI_LabelWriter,LoadFont(#Gadget_OMDBAPI_LabelWriter,"Comic Sans MS",10,0))
      StringGadget(#Gadget_OMDBAPI_Writer,80,95,285,25,"",#PB_String_BorderLess)
        SetGadgetFont(#Gadget_OMDBAPI_Writer,LoadFont(#Gadget_OMDBAPI_Writer,"Comic Sans MS",10,0))
      TextGadget(#Gadget_OMDBAPI_LabelDirector,370,100,70,20,"Director",#PB_Text_Center)
        SetGadgetColor(#Gadget_OMDBAPI_LabelDirector,#PB_Gadget_BackColor,$8080FF)
        SetGadgetFont(#Gadget_OMDBAPI_LabelDirector,LoadFont(#Gadget_OMDBAPI_LabelDirector,"Comic Sans MS",10,0))
      StringGadget(#Gadget_OMDBAPI_Director,440,95,290,25,"",#PB_String_BorderLess)
        SetGadgetFont(#Gadget_OMDBAPI_Director,LoadFont(#Gadget_OMDBAPI_Director,"Comic Sans MS",10,0))
      TextGadget(#Gadget_OMDBAPI_LabelActors,10,130,70,20,"Actors",#PB_Text_Center)
        SetGadgetColor(#Gadget_OMDBAPI_LabelActors,#PB_Gadget_BackColor,$8080FF)
        SetGadgetFont(#Gadget_OMDBAPI_LabelActors,LoadFont(#Gadget_OMDBAPI_LabelActors,"Comic Sans MS",10,0))
      StringGadget(#Gadget_OMDBAPI_Actors,80,125,650,25,"",#PB_String_BorderLess)
        SetGadgetFont(#Gadget_OMDBAPI_Actors,LoadFont(#Gadget_OMDBAPI_Actors,"Comic Sans MS",10,0))
      TextGadget(#Gadget_OMDBAPI_LabelPlot,10,155,70,20,"Plot",#PB_Text_Center)
        SetGadgetColor(#Gadget_OMDBAPI_LabelPlot,#PB_Gadget_BackColor,$8080FF)
        SetGadgetFont(#Gadget_OMDBAPI_LabelPlot,LoadFont(#Gadget_OMDBAPI_LabelPlot,"Comic Sans MS",10,0))
      EditorGadget(#Gadget_OMDBAPI_Plot,80,155,285,355,#PB_Editor_WordWrap)
        SetGadgetFont(#Gadget_OMDBAPI_Plot,LoadFont(#Gadget_OMDBAPI_Plot,"Comic Sans MS",10,0))
      TextGadget(#Gadget_OMDBAPI_LabelPicture,370,155,70,20,"Picture",#PB_Text_Center)
        SetGadgetColor(#Gadget_OMDBAPI_LabelPicture,#PB_Gadget_BackColor,$8080FF)
        SetGadgetFont(#Gadget_OMDBAPI_LabelPicture,LoadFont(#Gadget_OMDBAPI_LabelPicture,"Comic Sans MS",10,0))
      ImageGadget(#Gadget_OMDBAPI_Picture,440,155,290,355,ImageID(#Image_OMDBAPI_Picture))
      ResizeGadget(#Gadget_OMDBAPI_Picture,440,155,290,355)
      ResizeImage(#Image_OMDBAPI_Picture,290,355)
      SetGadgetState(#Gadget_OMDBAPI_Picture,ImageID(#Image_OMDBAPI_Picture))
      TextGadget(#Gadget_OMDBAPI_LabelimdbRating,10,525,70,20,"Rating",#PB_Text_Center)
        SetGadgetColor(#Gadget_OMDBAPI_LabelimdbRating,#PB_Gadget_BackColor,$8080FF)
        SetGadgetFont(#Gadget_OMDBAPI_LabelimdbRating,LoadFont(#Gadget_OMDBAPI_LabelimdbRating,"Comic Sans MS",10,0))
      StringGadget(#Gadget_OMDBAPI_ImdbRating,80,520,105,25,"",#PB_String_BorderLess)
        SetGadgetFont(#Gadget_OMDBAPI_ImdbRating,LoadFont(#Gadget_OMDBAPI_ImdbRating,"Comic Sans MS",10,0))
      TextGadget(#Gadget_OMDBAPI_LabelImdbVotes,190,525,70,20,"Votes",#PB_Text_Center)
        SetGadgetColor(#Gadget_OMDBAPI_LabelImdbVotes,#PB_Gadget_BackColor,$8080FF)
        SetGadgetFont(#Gadget_OMDBAPI_LabelImdbVotes,LoadFont(#Gadget_OMDBAPI_LabelImdbVotes,"Comic Sans MS",10,0))
      StringGadget(#Gadget_OMDBAPI_ImdbVotes,260,520,105,25,"",#PB_String_BorderLess)
        SetGadgetFont(#Gadget_OMDBAPI_ImdbVotes,LoadFont(#Gadget_OMDBAPI_ImdbVotes,"Comic Sans MS",10,0))
      TextGadget(#Gadget_OMDBAPI_LabelimdbID,370,525,70,20,"Imdb ID",#PB_Text_Center)
        SetGadgetColor(#Gadget_OMDBAPI_LabelimdbID,#PB_Gadget_BackColor,$8080FF)
        SetGadgetFont(#Gadget_OMDBAPI_LabelimdbID,LoadFont(#Gadget_OMDBAPI_LabelimdbID,"Comic Sans MS",10,0))
      StringGadget(#Gadget_OMDBAPI_ImdbID,440,520,105,25,"",#PB_String_BorderLess)
        SetGadgetFont(#Gadget_OMDBAPI_ImdbID,LoadFont(#Gadget_OMDBAPI_ImdbID,"Comic Sans MS",10,0))
      TextGadget(#Gadget_OMDBAPI_LabelImdbObject,550,525,70,20,"Object",#PB_Text_Center)
        SetGadgetColor(#Gadget_OMDBAPI_LabelImdbObject,#PB_Gadget_BackColor,$8080FF)
        SetGadgetFont(#Gadget_OMDBAPI_LabelImdbObject,LoadFont(#Gadget_OMDBAPI_LabelImdbObject,"Comic Sans MS",10,0))
      StringGadget(#Gadget_OMDBAPI_ImdbObject,620,520,110,25,"",#PB_String_BorderLess)
        SetGadgetFont(#Gadget_OMDBAPI_ImdbObject,LoadFont(#Gadget_OMDBAPI_ImdbObject,"Comic Sans MS",10,0))
      CloseGadgetList()
      ContainerGadget(#Gadget_OMDBAPI_cControl,5,565,740,55,#PB_Container_Flat)
        SetGadgetColor(#Gadget_OMDBAPI_cControl,#PB_Gadget_BackColor,$8080FF)
      ButtonImageGadget(#Gadget_OMDBAPI_Save,5,5,40,40,ImageID(#Image_OMDBAPI_Save))
      ButtonImageGadget(#Gadget_OMDBAPI_Print,40,5,40,40,ImageID(#Image_OMDBAPI_Print))
      ButtonImageGadget(#Gadget_OMDBAPI_Exit,80,5,40,40,ImageID(#Image_OMDBAPI_Exit))
      StringGadget(#Gadget_OMDBAPI_Search,130,20,555,25,"",#PB_String_BorderLess)
        SetGadgetFont(#Gadget_OMDBAPI_Search,LoadFont(#Gadget_OMDBAPI_Search,"Comic Sans MS",10,0))
      ButtonImageGadget(#Gadget_OMDBAPI_Run,695,5,40,40,ImageID(#Image_OMDBAPI_Run))
      CloseGadgetList()
      HideWindow(#Window_OMDBAPI,0)
    ProcedureReturn WindowID(#Window_OMDBAPI)
  EndIf
EndProcedure

; My Declarations

Declare SearchForMovie()

; My constants

Enumeration #ImageIndex
  #OMDBAPI_TemporaryImage
EndEnumeration

Global HTTPHeader.s     = "http://www.omdbapi.com/?t="
Global CurrentDir.s     = GetCurrentDirectory()
Global MovieFilename.s  = CurrentDir.s + "OMDBAPI.txt"
Global MoviePicture.s   = CurrentDir.s + "OMDBAPI.jpg"

; Constants

Procedure SearchForMovie()
  
  ; Get the search text from the user
  
  SearchText.s = GetGadgetText(#Gadget_OMDBAPI_Search)
  
  ; Check if there is search text to be used
  
  If SearchText.s <> ""
    
    ; Go ahead with the search if the info file doesn't exist (WIndows 8 security problem, can't 
    ; seem to overwrite an existing file.

    If FileSize(MovieFilename.s) = -1
      
      ; Properly encode the request URL and search text to prevent errors
      
      HTTPRequestString.s = URLEncoder(HTTPHeader.s + SearchText.s)
      
      ; Try to receive the requested information to a file on disk
      
      If ReceiveHTTPFile(HTTPRequestString.s, MovieFilename.s) <> 0
        
        ; Proceed if there was a received movie information file

        If FileSize(MovieFilename.s) <> -1
          
          ; Open the movie information file to read from

          MovieFile.i = ReadFile(#PB_Any, MovieFilename.s)
          
          ; Proceed if we were able to open the movie file
          
          If MovieFile.i
            
            ; Start reading lines of text from the opened movie information file
            
            While Eof(MovieFile.i) = 0
              
              ; Read a single line of text from he file
  
              Stringy.s     = ReadString(MovieFile.i)
              
              ; Replace double quotes with single quotes or string work will fail
              
              Stringy.s     = ReplaceString(Stringy.s, Chr(34), Chr(39), #PB_String_NoCase, 1)
              
              ; Count the number of separators for fields that we are looking for
              
              Delimiters.i  = CountString(Stringy.s, "','")
              
              ; Start processing the individual fields in the string read from the file using the chosen delimiters
  
              For StringLoop.i = 1 To Delimiters.i
                
                ; Get the current field to be processed

                CurrentString.s = StringField(Stringy.s, StringLoop.i, "','")
                
                ; Remove unwanted characters from the field string
                
                CurrentString.s = RemoveString(CurrentString.s, "{", #PB_String_NoCase, 1)
                
                CurrentString.s = RemoveString(CurrentString.s, "}", #PB_String_NoCase, 1)
                
                CurrentString.s = RemoveString(CurrentString.s, "'", #PB_String_NoCase, 1)
                
                ; Process only the fields not referencing the cover picture 'Poster' field

                If FindString(CurrentString.s, "Poster", 1, #PB_String_NoCase) = 0
                  
                  ; Retrieve the label and the data for the current field separately

                  StringLabel.s   = Trim(StringField(CurrentString.s, 1, ":"))
                  
                  StringData.s    = Trim(StringField(CurrentString.s, 2, ":"))
                  
                  ; Check which gadget the current data has to get set to

                  Select StringLabel.s
                      
                    Case "Title"      : SetGadgetText(#Gadget_OMDBAPI_Title,       StringData.s)
                      
                    Case "Year"       : SetGadgetText(#Gadget_OMDBAPI_Year,        StringData.s)
                      
                    Case "Rated"      : SetGadgetText(#Gadget_OMDBAPI_Rated,       StringData.s)
                      
                    Case "Released"   : SetGadgetText(#Gadget_OMDBAPI_Released,    StringData.s)
                      
                    Case "Runtime"    : SetGadgetText(#Gadget_OMDBAPI_RunTime,     StringData.s)
                      
                    Case "Genre"      : SetGadgetText(#Gadget_OMDBAPI_Genre,       StringData.s)
                      
                    Case "Director"   : SetGadgetText(#Gadget_OMDBAPI_Director,    StringData.s)
                      
                    Case "Writer"     : SetGadgetText(#Gadget_OMDBAPI_Writer,      StringData.s)
                      
                    Case "Actors"     : SetGadgetText(#Gadget_OMDBAPI_Actors,      StringData.s)
                      
                    Case "Plot"       : SetGadgetText(#Gadget_OMDBAPI_Plot,        StringData.s)
                      
;                   Case "Poster"     : ImageGadget(#Gadget_OMDBAPI_Picture,       StringData.s)
                      
                    Case "imdbRating" : SetGadgetText(#Gadget_OMDBAPI_ImdbRating,  StringData.s)
                      
                    Case "imdbVotes"  : SetGadgetText(#Gadget_OMDBAPI_ImdbVotes,   StringData.s)
                      
                    Case "imdbID"     : SetGadgetText(#Gadget_OMDBAPI_ImdbID,      StringData.s)
                      
                    Case "Type"       : SetGadgetText(#Gadget_OMDBAPI_ImdbObject,  StringData.s)
                      
                    Case "Response"   : 
                      
                  EndSelect
                  
                  ; Process the picture feild separately and retrieve it

                Else
                  
                  StringLabel.s   = Trim(StringField(CurrentString.s, 1, ":"))
                  
                  StringData.s    = Trim(StringField(CurrentString.s, 2, ":")) + ":"
                  
                  StringData.s    + Trim(StringField(CurrentString.s, 3, ":"))
                  
                  ; Make sure the picture filename to retrieve is properly encoded
                  
                  StringData.s = URLEncoder(StringData.s)
                  
                  ; Get just the filename part to use as a local filename
                  
                  PictureFilename.s = GetFilePart(StringData.s)
                  
                  ; Now try to retrieve the picture from the imdb site
                  
                  If ReceiveHTTPFile(StringData.s, CurrentDir.s + PictureFilename.s) <> 0

                    ; Check if the selected picture already lives in the local picture directory
                    
                    If FileSize(CurrentDir.s +  PictureFilename.s) <> -1
                      
                      ; Now try to load the image from the local picture storage area
                      
                      If LoadImage(#OMDBAPI_TemporaryImage, CurrentDir.s +  PictureFilename.s) <> 0
                        
                        ; 
                        
                        If ResizeImage(#OMDBAPI_TemporaryImage, 290, 355, #PB_Image_Smooth) <> 0
                          
                          SetGadgetState(#Gadget_OMDBAPI_Picture, ImageID(#OMDBAPI_TemporaryImage))
                          
                        Else
                          
                          Debug "Selected picture couuld not be resized to the required gadget"
                          
                        EndIf
                        
                        ; 
                        
                      Else
                        
                        Debug "Selected picture was missing or damaged and unloadable"
                        
                      EndIf
                      
                      ; 
                      
                    Else
                      
                      Debug "Picture was downloaded but has gone missing"
                      
                    EndIf
                    
                    ; 
                    
                  Else
                    
                    Debug "The picture was not downloaded successfully"
                    
                  EndIf
                  
                  ; 
                  
                EndIf
                
                ; Process the next field in the string read from the movie information file

              Next StringLoop
              
              ; Read another line of data from the movie information file

            Wend
            
            ; Close the movie information file, we are finished reading from it
            
            CloseFile(MovieFile.i)
            
            ; Let the user know that the movie information file could not be opened
            
          Else
            
            Debug "Cannot open the movie file to be read"
            
          EndIf
          
          ; Let the user know that the movie information file could not be downloaded

        Else
          
          Debug "Failed to retrieve movie info file to local directory"
          
        EndIf
        
        ; The download might have worked but we can't seem to locate the movie information file

      Else
        
        Debug "The movie file does not seem to exist on disk?"
        
      EndIf
      
      ; Delete an existing movie information file (Problem with Windows 8 security here)

    Else
      
      DeleteFile(MovieFilename.s)
      
    EndIf
    
    ; User didn't supply any text to search with

  Else
    
    Debug "Search text is empty, nothing to find"
    
  EndIf
  
  ; 

EndProcedure
  
; 

If Window_OMDBAPI()
  quitOMDBAPI = 0
  Repeat
    EventID  = WaitWindowEvent()
    MenuID   = EventMenu()
    GadgetID = EventGadget()
    WindowID = EventWindow()
    Select EventID
      Case #PB_Event_CloseWindow
        Select WindowID
          Case #Window_OMDBAPI            : quitOMDBAPI = 1
        EndSelect
      Case #PB_Event_Gadget
        Select GadgetID
          Case #Gadget_OMDBAPI_Save       : ;SaveMovieData()
          Case #Gadget_OMDBAPI_Print      : ;PrintMovieData()
          Case #Gadget_OMDBAPI_Exit       : quitOMDBAPI = 1
          Case #Gadget_OMDBAPI_Run        : SearchForMovie()
        EndSelect
    EndSelect
  Until quitOMDBAPI
  CloseWindow(#Window_OMDBAPI)
EndIf
End

Amateur Radio/VK3HAF, (D-STAR/DMR and more), Arduino, ESP32, Coding, Crochet
User avatar
Fangbeast
PureBasic Protozoa
PureBasic Protozoa
Posts: 4799
Joined: Fri Apr 25, 2003 3:08 pm
Location: Not Sydney!!! (Bad water, no goats)

Re: ReceiveHTTPFile fails for me

Post by Fangbeast »

Note that using the API URLDownloadToFile_ gets the picture, ReceiveHTTPFile does not and I don't know why. I attempted to get the header with GetHTTPHeader didn't return anything.

Code: Select all

InitNetwork()

CurrentDir.s        = GetCurrentDirectory()

RemotePictureURL.s  = "http://ia.media-imdb.com/images/M/MV5BMjIxNjAzODQ0N15BMl5BanBnXkFtZTcwODY2MjMyNA@@._V1_SX300.jpg"

RemotePictureURL.s  = URLEncoder(RemotePictureURL.s)

LocalPictureName.s  = CurrentDir.s + "Sample.jpg"

If URLDownloadToFile_(0, RemotePictureURL.s, LocalPictureName.s, 0, 0)

;If Not ReceiveHTTPFile(RemotePictureURL.s, LocalPictureName.s)
  
  Debug GetHTTPHeader(RemotePictureURL.s)
  
  Debug "Remote URL:   "  + RemotePictureURL.s
  
  Debug "Local URL:    "  + LocalPictureName.s
  
  Debug "Cannot get file for some reason"
  
EndIf
Amateur Radio/VK3HAF, (D-STAR/DMR and more), Arduino, ESP32, Coding, Crochet
MachineCode
Addict
Addict
Posts: 1482
Joined: Tue Feb 22, 2011 1:16 pm

Re: ReceiveHTTPFile fails for me

Post by MachineCode »

Fangbeast wrote:why does it work with spaces when I put it into the browser? Does it encode the url behind the scenes then?
Yep, sure does. Paste that URL into Firefox and even though the Address Bar shows "star wars", the tab title shows "star%20wars".
Microsoft Visual Basic only lasted 7 short years: 1991 to 1998.
PureBasic: Born in 1998 and still going strong to this very day!
normeus
Enthusiast
Enthusiast
Posts: 486
Joined: Fri Apr 20, 2012 8:09 pm
Contact:

Re: ReceiveHTTPFile fails for me

Post by normeus »

More than likely the server is setup to send images to browsers only. Or you might need to send your api key ( a key like your known secret, NOT your secret api key). so the call might be "http://ia.media-imdb.com/images/M/igage ... aredsecret"
read about getting server pictures form their api
User avatar
Fangbeast
PureBasic Protozoa
PureBasic Protozoa
Posts: 4799
Joined: Fri Apr 25, 2003 3:08 pm
Location: Not Sydney!!! (Bad water, no goats)

Re: ReceiveHTTPFile fails for me

Post by Fangbeast »

normeus wrote:More than likely the server is setup to send images to browsers only. Or you might need to send your api key ( a key like your known secret, NOT your secret api key). so the call might be "http://ia.media-imdb.com/images/M/igage ... aredsecret"
read about getting server pictures form their api
Not sure that is correct as the PB "ReceiveHTTPFile" command is based on the API version "URLDownloadToFile_" as far as I know and I am now using that one successfully to retrieve the picture.

Worked it out last night but thanks for the suggestion.

I used to use a secret key years ago with another service and I analysed some of the calls going to imdb and there didn't seem to be a secret key involved.

I'll post my fixed code today (yawn)
Amateur Radio/VK3HAF, (D-STAR/DMR and more), Arduino, ESP32, Coding, Crochet
User avatar
Fangbeast
PureBasic Protozoa
PureBasic Protozoa
Posts: 4799
Joined: Fri Apr 25, 2003 3:08 pm
Location: Not Sydney!!! (Bad water, no goats)

Re: ReceiveHTTPFile fails for me

Post by Fangbeast »

MachineCode wrote:
Fangbeast wrote:why does it work with spaces when I put it into the browser? Does it encode the url behind the scenes then?
Yep, sure does. Paste that URL into Firefox and even though the Address Bar shows "star wars", the tab title shows "star%20wars".
Thanks
Amateur Radio/VK3HAF, (D-STAR/DMR and more), Arduino, ESP32, Coding, Crochet
normeus
Enthusiast
Enthusiast
Posts: 486
Joined: Fri Apr 20, 2012 8:09 pm
Contact:

Re: ReceiveHTTPFile fails for me

Post by normeus »

Sorry, I just tried your code to get the picture and it worked fine on my end ( should have tried it before I commented )
however;

Code: Select all

 Debug GetHTTPHeader("http://ia.media-imdb.com/images/M/MV5BMjIxNjAzODQ0N15BMl5BanBnXkFtZTcwODY2MjMyNA@@._V1_SX300.jpg")
returns nothing.
If you are using GetHTTPHeader() to test if there is anything to retrieve, then you'll get nothing.
infratec
Always Here
Always Here
Posts: 7796
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

Re: ReceiveHTTPFile fails for me

Post by infratec »

Hi Fangbeast,

try this:

Code: Select all

EnableExplicit


Procedure.i HTTPGet2Mem(URL$)
  
  Protected.i ConnectionID, TimeOutCounter, Laenge, Pos, Received, HelpLength, Length, HeaderEndPos, HeaderFinished, Size
  Protected Server$, Header$, String$, Result$, Page$, Parameter$, Host$, *Buffer, *HelpBuffer
  
  
  If LCase(Left(URL$, 7)) <> "http://"
    URL$ = "http://" + URL$
  EndIf
  
  ;Host$ = GetURLPart(URL$, #PB_URL_Site)
  
  Host$ = Mid(URL$, 8, FindString(URL$, "/", 8) - 8)
  
  Page$ = "/" + GetURLPart(URL$, #PB_URL_Path)
  Parameter$ = GetURLPart(URL$, #PB_URL_Parameters)
  If Parameter$
    Page$ + "&" + Parameter$
  EndIf
  
  ;Debug Host$
  
  ConnectionID = OpenNetworkConnection(Host$, 80)
  
  If ConnectionID
    
    ;Build header
        
    Header$ = "GET " + Page$ + " HTTP/1.1" + #CRLF$
    Header$ + "Host: " + Host$ + #CRLF$
    ;Header$ + "Accept: text/html" + #CRLF$
    Header$ + "Content-Type: application/x-www-form-urlencoded" + #CRLF$    
    ;String$ + "Connection: close" + #CRLF$
    Header$ + #CRLF$
    ; header is finished
    
    ;Debug Header$
    

    SendNetworkString(ConnectionID, Header$)
    TimeOutCounter = 1000
    Repeat
      If NetworkClientEvent(ConnectionID) = #PB_NetworkEvent_Data
        Break
      EndIf
      Delay(10)
      TimeOutCounter -1
    Until TimeOutCounter = 0
    
    If TimeOutCounter <> 0
      #Size = 10000
      *HelpBuffer = AllocateMemory(#Size)
      
      If *HelpBuffer
        String$ = ""      
        Repeat
          HelpLength = ReceiveNetworkData(ConnectionID, *HelpBuffer, #Size)
          If HelpLength > 0
            *Buffer = ReAllocateMemory(*Buffer, Length + HelpLength)
            CopyMemory(*HelpBuffer, *Buffer + Length, HelpLength)
            Length + HelpLength
            
            ;Debug Length
            
            If Not HeaderFinished
              HeaderEndPos = FindString(PeekS(*Buffer), #CRLF$ + #CRLF$)
              If HeaderEndPos
                HeaderFinished = #True
                Pos = FindString(PeekS(*Buffer), "Content-Length:")
                If Pos
                  Size = Val(PeekS(*Buffer + Pos + 15)) + HeaderEndPos + 3
                  ;Debug Size
                EndIf
              EndIf
            EndIf
            
            ;String$ = PeekS(*HelpBuffer + HelpLength - 10, 10, #PB_UTF8)
            ;String$ + PeekS(*HelpBuffer, -1, #PB_UTF8)
            ;Debug String$
          EndIf
          If Size > 0
            If Length >= Size
              Received = #True
            EndIf
          Else
            If FindString(String$, "</html") > 0
              Received = #True
            EndIf
          EndIf
        Until Received
        FreeMemory(*HelpBuffer)
      EndIf
    EndIf
      
    CloseNetworkConnection(ConnectionID)
  EndIf
  
  ProcedureReturn *Buffer
  
EndProcedure




Define *Buffer, Size.i, HeaderPos.i

UseJPEGImageDecoder()
InitNetwork()

*Buffer = HTTPGet2Mem("http://ia.media-imdb.com/images/M/MV5BMjIxNjAzODQ0N15BMl5BanBnXkFtZTcwODY2MjMyNA@@._V1_SX300.jpg")
If *Buffer <> #Null
  Size = MemorySize(*Buffer)
  
  HeaderPos = FindString(PeekS(*Buffer, 1024), #CRLF$ + #CRLF$)
  
  OpenWindow(0, 0, 0, 800, 640, "Test")
  CatchImage(0, *Buffer + HeaderPos + 3)
  ImageGadget(0, 0, 0, 0, 0, ImageID(0))  
  FreeMemory(*Buffer)
  
  Repeat : Until WaitWindowEvent() = #PB_Event_CloseWindow
  
Else
  MessageRequester("Error", "Not downloaded")
EndIf
But it is only for picture addresses in the web :!:
Since only than the picture data starts immediately after the http header.

Bernd
User avatar
Fangbeast
PureBasic Protozoa
PureBasic Protozoa
Posts: 4799
Joined: Fri Apr 25, 2003 3:08 pm
Location: Not Sydney!!! (Bad water, no goats)

Re: ReceiveHTTPFile fails for me

Post by Fangbeast »

Thanks for the example Bernd. I already fixed my problems file wise with a combination of AI and native PB, works well.

Then Idle gave me an example for buffer as well. I like both your buffer solutions, gets around temporary files cluttering up my space.

So much to do, so little time!!

P.S. RecipeMuncher has 177,00 recipes in it...EEK!!!
Amateur Radio/VK3HAF, (D-STAR/DMR and more), Arduino, ESP32, Coding, Crochet
User avatar
Fangbeast
PureBasic Protozoa
PureBasic Protozoa
Posts: 4799
Joined: Fri Apr 25, 2003 3:08 pm
Location: Not Sydney!!! (Bad water, no goats)

Re: ReceiveHTTPFile fails for me

Post by Fangbeast »

But it is only for picture addresses in the web :!:
Since only than the picture data starts immediately after the http header.
Very fast for pictures. Now I need to do the same for the information somehow.

Using the pb function to query the OMDBAPI database is nice, but there seems no way to have progress information that I can find as calls to that database (OMDBAPI.com) have proven very slow and I need to see what's going on.

OMDB acts like an intermediary to IMDB, maybe that's the slowdown problem.

At any rate, I want to return the movie info to a buffer as well and have some sort of progress bar to tell me what is happening.
Amateur Radio/VK3HAF, (D-STAR/DMR and more), Arduino, ESP32, Coding, Crochet
User avatar
Thunder93
Addict
Addict
Posts: 1788
Joined: Tue Mar 21, 2006 12:31 am
Location: Canada

Re: ReceiveHTTPFile fails for me

Post by Thunder93 »

Earlier today when I was doing up a simple utility to maintain IE10 TPLs, I observed incomplete TPLs files via PB ReceiveHTTPFile(). I had to resort to the windows API for reliable downloads.
Fangbeast wrote:
normeus wrote:More than likely the server is setup to send images to browsers only. Or you might need to send your api key ( a key like your known secret, NOT your secret api key). so the call might be "http://ia.media-imdb.com/images/M/igage ... aredsecret"
read about getting server pictures form their api
Not sure that is correct as the PB "ReceiveHTTPFile" command is based on the API version "URLDownloadToFile_" as far as I know and I am now using that one successfully to retrieve the picture.

Worked it out last night but thanks for the suggestion.

I used to use a secret key years ago with another service and I analysed some of the calls going to imdb and there didn't seem to be a secret key involved.

I'll post my fixed code today (yawn)
ʽʽSuccess is almost totally dependent upon drive and persistence. The extra energy required to make another effort or try another approach is the secret of winning.ʾʾ --Dennis Waitley
User avatar
Fangbeast
PureBasic Protozoa
PureBasic Protozoa
Posts: 4799
Joined: Fri Apr 25, 2003 3:08 pm
Location: Not Sydney!!! (Bad water, no goats)

Re: ReceiveHTTPFile fails for me

Post by Fangbeast »

[quote="Thunder93"]Earlier today when I was doing up a simple utility to maintain IE10 TPLs, I observed incomplete TPLs files via PB ReceiveHTTPFile(). I had to resort to the windows API for reliable downloads.

Yes, noticed that last year but I can't say it's a bug because I don't know how the pb version is implemented and I have no access to the more verbose error messages that are available for the API version and I presume for the PB version as well if one knew how to query them.
Amateur Radio/VK3HAF, (D-STAR/DMR and more), Arduino, ESP32, Coding, Crochet
Post Reply