New topic: File in use - Dealing with non-existent HTTPSocket downloads
<http://forums.realsoftware.com/viewtopic.php?t=46842> Page 1 of 1 [ 4 posts ] Previous topic | Next topic Author Message mbrogdon Post subject: File in use - Dealing with non-existent HTTPSocket downloadsPosted: Wed Feb 06, 2013 12:15 pm Joined: Thu Jan 24, 2013 5:10 pm Posts: 9 Automating downloads using HTTPSocket, asynchronous. This seems to be working well, however there are times when the file the code is looking for doesn't exist on the Web. The code creates the FolderItem, tries to download to it and instead downloads a 404 missing page error. All of this is expected (though I'm not sure that there isn't a better way). The code also detects that this has happened and attempts to delete the file before moving on to the next file. Frequently, the file is still in use and it cannot be deleted. My thought was to loop until the FolderItem error (104) goes away. If the error 104 goes away, attempt to delete again. In the loop, include an integer that increments up. Once it reaches a certain number, exit the loop, therefore giving up on the delete. Just tested this, and it doesn't work because my application is what has hold of the FolderItem. Looking at the content of the downloaded file, it seems to have completely downloaded the HTML error page, so I'm not sure why this is happening. What is the proper way to deal with this? Thanks. Top wpurvis Post subject: Re: File in use - Dealing with non-existent HTTPSocket downlPosted: Wed Feb 06, 2013 12:59 pm Joined: Tue Oct 04, 2005 10:55 am Posts: 40 Location: Fort Myers, FL How are you detecting that you downloaded a 404 error message into the file? Via the HTTPStatus parameter in the DownloadComplete event? This may or may not be relevant. It would be relevant if, for example, you were opening the file and reading its contents, rather than relying on the HTTP status code. If the HTTPSocket is holding the file open after the download is complete, that sounds like a bug (or a bad design), but I'd be surprised if that's the case. Have you tried nil'ing the HTTPSocket? (Maybe that will make it release its hold on the file, if in fact it's keeping one.) Top charonn0 Post subject: Re: File in use - Dealing with non-existent HTTPSocket downlPosted: Wed Feb 06, 2013 1:11 pm Joined: Mon Apr 02, 2007 2:08 am Posts: 1108 Location: San Francisco, CA, USA If you are handling the HTTP request asynchronously, then look at the the HTTPStatus parameter to the PageReceived event. The HTTPStatus parameter will be the HTTP response code for the page (be it an error, a document, or a file) now being received. You can detect and handle errors (e.g. 404) there before saving the Content parameter to file. Note: The HTTPSocket.Get method is overloaded with two synchronous and two asynchronous versions. For the following demonstration, only HTTPSocket.Get(url) will work. e.g. Dim sock As New MyHTTPSocket sock.Get("www.example.org/dl/somefile.zip") 'Asynchronous request. then in the MyHTTPSocket.PageReceived event: Sub PageReceived(url as string, httpStatus as integer, headers as internetHeaders, content as string) If HTTPStatus = 200 Then 'OK 'example code; not tested Dim saveTo As FolderItem = SpecialFolder.Documents Dim fileName As String = NthField(url, "/", CountFields(url, "/")) saveTo = saveTo.Child(fileName) Dim bs As BinaryStream = BinaryStream.Create(saveTo, True) 'Overwrite if exists = True bs.Write(content) 'the content parameter is the raw file data bs.Close ElseIf httpStatus = 404 'Not found 'Don't create the file in the first place. End If End Sub Note that if you are passing a FolderItem to the HTTPSocket.Get method [i.e.: MyHttpSocket.Get("www.example.com", SaveTo)] then the PageReceived event will not fire. Refer to the docs for the overloaded HTTPSocket.Get method. _________________ Boredom Software Top mbrogdon Post subject: Re: File in use - Dealing with non-existent HTTPSocket downlPosted: Wed Feb 06, 2013 2:04 pm Joined: Thu Jan 24, 2013 5:10 pm Posts: 9 I made a mistake in my previous post in that I'm using synchronous. However, I think "bad design" was indeed the cause of the problem. The application is supposed to open, wait ten seconds and then automatically begin downloading the files. I was looping and counting ticks in a thread to create the 10 second delay. Used a thread so that the window wouldn't freeze upon opening. Seems to work fine as far as creating the delay, but when I use this code, I get the occasional download problem. I probably was not using the thread correctly. I moved my code to a method, inserted a timer with a period of 10000 to generate the 10 seconds delay and called the method from the timer's action event. No window freeze and the download issues seem to be gone. Thanks! Top Display posts from previous: All posts1 day7 days2 weeks1 month3 months6 months1 year Sort by AuthorPost timeSubject AscendingDescending Page 1 of 1 [ 4 posts ] -- Over 1500 classes with 29000 functions in one REALbasic plug-in collection. The Monkeybread Software Realbasic Plugin v9.3. http://www.monkeybreadsoftware.de/realbasic/plugins.shtml [email protected]
