Notifications
Clear all

[Closed] dotNet webClient

 JHN

Have you tried it on another website?
I have seen the timeout too, but once I it with some new variable names and a new website it worked everytime and pretty fast too. I think that an not properly closed socket is too blame for the timeouts. The resources need to be released, that’s what the close method is for.


(
	total = 0
	for i = 1 to 10 do
	(
	start = timeStamp()
	request = (dotNetClass "system.Net.WebRequest").create "http://www.penproductions.ca"
	request.Credentials = (dotnetClass "System.Net.CredentialCache").DefaultCredentials
	response = request.GetResponse()

	result_URI = response.ResponseUri.ToString()
	result_HEADER = response.Headers.ToString()

	response.Close()
	end = timeStamp()
	total += end - start
	format "%
%
" (end-start) result_URI
	)
	format "
totalTime of 10 runs : %
" (total/10)
)

[color=SeaGreen]-- 844
--  http://www.penproductions.ca/ 
-- 359
--  http://www.penproductions.ca/ 
-- 266
--  http://www.penproductions.ca/ 
-- 266
--  http://www.penproductions.ca/ 
-- 265
--  http://www.penproductions.ca/ 
-- 250
--  http://www.penproductions.ca/ 
-- 250
--  http://www.penproductions.ca/ 
-- 266
--  http://www.penproductions.ca/ 
-- 250
--  http://www.penproductions.ca/ 
-- 265
--  http://www.penproductions.ca/ 

-- totalTime of 10 runs : 328
-- OK[/color]


Average of 328ms, that’s not bad.

-Johan

 JHN

request.Timeout = 1000

Sets the max timeout to 1 sec. for example. Still you’d have to handle the timeout exception, could be in a simple try catch.

-Johan

To see if there’s an internet connection you can also use this example, but always using the try catch method…

(	
	---Check if internet connection is available
	host = dotnetClass "System.Net.Dns"
	try
	(
		hostInfo = host.GetHostByName("www.google.com")	
	)
	catch
	(
		print (getCurrentException()) 
	)
)

Also if you had this to JHN’s code after request.Credentials it’s faster because it only downloads the metainformation contained in the HEADERS and not the whole entity-body (as explained in HTTP/1.1 RFC2616 Protocol)

...
request.Credentials = (dotnetClass "System.Net.CredentialCache").DefaultCredentials
[B]request.Method = "HEAD"[/B]
response = request.GetResponse()
...

Without request.Method = “HEAD”

1265
 http://www.penproductions.ca/ 
526
 http://www.penproductions.ca/ 
370
 http://www.penproductions.ca/ 
374
 http://www.penproductions.ca/ 
363
 http://www.penproductions.ca/ 
364
 http://www.penproductions.ca/ 
362
 http://www.penproductions.ca/ 
368
 http://www.penproductions.ca/ 
378
 http://www.penproductions.ca/ 
358
 http://www.penproductions.ca/ 

totalTime of 10 runs : 472

With request.Method = “HEAD”

348
 http://www.penproductions.ca/ 
173
 http://www.penproductions.ca/ 
176
 http://www.penproductions.ca/ 
176
 http://www.penproductions.ca/ 
177
 http://www.penproductions.ca/ 
178
 http://www.penproductions.ca/ 
175
 http://www.penproductions.ca/ 
177
 http://www.penproductions.ca/ 
175
 http://www.penproductions.ca/ 
176
 http://www.penproductions.ca/ 

totalTime of 10 runs : 193
 PEN

JHN, the problem isn’t with timing out I don’t think, I think it is with not having a connection in the first place. When I have a connection it works great.

Kameleon, thanks, I’m using a try catch now to get around the problem, I was just hoping that there was a cleaner way.

 JHN

Maybe it’s good to mention that try catch is a perfectly legal way in c# to catch exceptions, I even think it’s the only way. I know it’s not considered great practice in max, but in this instance where you set a timeout like 1 second and a try-catch to capture the timeout exception, looks perfectly okay to me!

-Johan

 PEN

I guess I’m a bit anal and want to keep it really clean. TryCatch is working fine.

Page 2 / 2