FoxPro Programming
Reusing and Closing wwHTTP Object
Gravatar is a globally recognized avatar based on your email address. Reusing and Closing wwHTTP Object
  Richard Caruso
  All
  Oct 29, 2020 @ 09:23am

I read in a post that you should not reuse the same object for subsequent Get/Posts. Should we close/release loHttp and createobject("wwHTTP") again before re-posting? This code works, but just wondering what best practice would be going forward.

Also, when leaving the routine, is it necessary to CLOSE PROCEDURE wwHTTP.frx? Tried issuing loHttp.httpClose() at end of routine. We are getting an error message "...wwhttp.fxp is not closed" when compiling after running the program.

Many Thanks, Ric

DO wwHTTP
loHttp = CREATEOBJECT("wwHTTP")
loHttp.AddPostKey(FileToStr(jcFile2Post))

WAIT WINDOW NOWAIT "Running HTTP request to post a file..."

*** Preset the buffer and result size
lcText=""
lnText=0

loHttp.AddPostKey(FileToStr(jcFile2Post))
lcText = loHttp.Post(pcPostEndPoint)

IF loHttp.nError # 0	
	IF loHttp.nError = 400 AND ATC("file already exists",jcErrorMsg) > 0
		jnOverwrite = MESSAGEBOX("File Already Exists! Overwrite File on Server?",36,"Overwrite File")
		IF jnOverwrite = G_YES_PB
			*!* OK to Overwrite, post again with overwrite = YES
			loHttp.AddHeader("Overwrite","Y")
			loHttp.AddPostKey(FileToStr(jcFile2Post))
			lcText = loHttp.Post(pcPostEndPoint)
		ELSE
			=MESSAGEBOX("PCT File Failed to Upload",48,"Upload Failure")
		ENDIF	&& jnOverwrite = G_YES_PB
	ENDIF && loHttp.nError = 400 AND ATC(..)
ELSE
		=MESSAGEBOX(lcText,64,"Uploading File Successful")
ENDIF	&& loHttp.nError # 0

loHttp.httpClose()

Gravatar is a globally recognized avatar based on your email address. re: Reusing and Closing wwHTTP Object
  Rick Strahl
  Richard Caruso
  Oct 29, 2020 @ 03:04pm

wwHttp is a lightweight object and closing down the object insures that you get a clean copy to run your next request. HTTP is a stateless protocol so it connects and disconnects on each request, so there's no benefit from keeping the connection (which in theory should shut down anyway after each request).

HTTP can be funky at times and finish a request and return results while a connection is still open in the background which if connecting again can cause weird behavior (connection failures usually). This is the main reason that I recommend using a new instance. When you use a new instance a new handle and completely new connection is used. Windows internally manages HTTP connection handles and internally optimizes connection lifetimes for things like Keep-Alive so even re-connects in rapid succession are fast.

So - use CREATEOBJECT("wwHttp") for each new request. You don't need to release the library, that's not useful.

That said - you can re-use wwHTTP if you really think it's necessary. In low volume environments this is unlikely to ever be a problem, but the recommended approach is a new instance per request.

+++ Rick ---

+++ Rick ---

© 1996-2020