URLConnection equivalent for HTTPSocket

I have long established code:

dim h as new HTTPSocket
h.yield = true
x= h.get ("https://mysite.co.uk/test/" + "Somefile", 2)
if h.HTTPStatusCode = 200  then
 //use the returned data
  
end if

It used to work, and still does on one site
On another site, I get Error 301 permanent relocated, even though the file exists.

Is this a bug?
Something I can fix on the website?
Something I can fix by switching to URLConnection?

(If URLConnection, what is the equivalent? - all the examples I can find discuss GET/POST commands, and I cant seem to guess what the magic words are…)

AFAIK for URLConnection it is unnecessary, it should follow to the relocated address by itself in just one call.

The working solution (for my purposes), supplied by Norm is to assume a GET, and ignore the (unavailable) status
I check the contents of the returned document instead

dim h as new Urlconnection 
x = h.SendSync( "GET", "https://mysite.co.uk/test/" + "Somefile", 2)

In the URLConnection’s ContentReceived event, I have this:

Var  msg As String

if  (me.threadPtr=Nil)  then Return

if  (HTTPStatus<>200 or content="")  then
  msg = "error code: " + HTTPStatus.ToString + ", content length: " + content.Length.ToString
  writeLog (msg, "Checker")
  me.errorCode = 1
  Return
end if

me.content = content
me.threadPtr.Resume ()

I have subclassed URLConnection and added those properties. The thread does the actual work.

I do this to get data. I’m asking for the contents of a file - YMMV:

function getData (extends skt As CheckerURLConnection, fname As String, byref content As String)

// Dialogs with the website

Var  rqststr As String

rqststr = "fstr=" + fname + "&zimp=" + app.safety    // Some attempt at security

skt.ClearRequestHeaders ()
skt.SetRequestContent (rqststr, "application/x-www-form-urlencoded")
skt.Send ("POST", "https://www.example.com/drivel.php")

skt.threadPtr.Pause ()                               // To be restarted by error or data received

if  (skt.errorCode>0)  then Return False

content = skt.content

Return True
1 Like

As I said, 301 auto-follows the redirection and goes to the destination, and ends with 200.
I confirmed it doing it now.

1 Like