With Xojo v2018r4, I’ve been using this code to get the status (As Integer) of a particular URL:
Dim s As New HTTPSecureSocket
s.ConnectionType = SSLSocket.SSLv23
s.Secure = True
Dim x As String = s.SendRequest("HEAD", <url As String>, 5)
Return s.HTTPStatusCode
That code is run hourly. Now I’m wondering if it’s creating (and not destroying) a socket hourly. Should I add these two lines to the end of that code:
s.Close
s = Nil
And if I finally upgrade to Xojo 2019r1.1, can I change all that to this:
Dim u As New URLConnection
Dim x As String = u.SendSync("HEAD", <url As String>, 5)
Return u.HTTPStatusCode
u.Disconnect
u = Nil
The web server would likely close their end of the connection in a minute or two, assuming it leaves the connection open at all. You can instruct the server to close the connection right away by including the Connection: close
header in the request.
Setting a local variable to Nil is usually superfluous. If the socket is not connected then it will be destroyed automatically when the method exits.
[quote]
Dim u As New URLConnection
Dim x As String = u.SendSync("HEAD", <url As String>, 5)
Return u.HTTPStatusCode
u.Disconnect
u = Nil
[/quote]
The return statement prevents the following lines from being executed.
I would do something like this:
If s.IsConnected Then s.Close
Return s.HTTPStatusCode
So it sounds like if I’m running this only once an hour, I can forget about closing this connection explicitly. Even if I later use URLConnection.
Hmm…thinking this over again. What if the connection isn’t closed until shortly after the method ends (because it might take a minute or two for the connection to close)? Won’t the socket fail to be destroyed when the method exits in such a situation, and stay in existence after that?
A connected socket with zero references will continue to exist until the other side disconnects, at which point it will be destroyed.
Thanks for your patience on this, Andrew.