can't have socket.GetHeaders to work

Hi All.

Yet another pretty noob question… sorry in advance.

I have an HTMLViewer (so I can see the text version of a web page), and that is working fine.
However, sometimes I get a 301 Permanently moved error. Research tells me that I need to do a get headers to find out where the new Location is.
No problem, says I.

Looking in the documentation, I found HTTPSocket.GetHeader.
It never works saying either syntax error, too many variables, wants a string, and the like.

I copied a line from the docs and only changed textArea and socket names and I still get the same errors.
I never get anything to redirect to.

Here is what I have in code for the pushbutton. Don’t laugh…

[code]//works fine
Dim socket As New HTTPSocket
Dim html As String = socket.Get(""+TextField1.text+"/", 30)
TextArea1.Text = html

//doesn’t work

Dim relocatedSocket as new HTTPSocket
Dim relocatedString as InternetHeaders = relocatedSocket.GetHeaders(""+Textfield1.text+"",30)
TextArea2.text = str(relocatedString)[/code]

I want to see where the page is redirected to in TextArea2.text so i can see how I want to deal with what the page thows me.

Regards

Your basic problem here is GetHeaders() returns a class object InternetHeaders and not just a string. And you correctly declare relocatedString as InternetHeaders, but then try to get a str() from it.

If you want to see all the headers, InternetHeaders class has a method Source which gives them all in one multiline string:

Dim relocatedSocket as new HTTPSocket Dim headers as InternetHeaders = relocatedSocket.GetHeaders(""+Textfield1.text+"",30) TextArea2.text = headers.source()

but since the HTTP status 301 returns the redirect in header Location, you can instead dump out just that header like this:

Dim relocatedSocket as new HTTPSocket Dim headers as InternetHeaders = relocatedSocket.GetHeaders(""+Textfield1.text+"",30) TextArea2.text = headers.Value("Location")

Or better yet, use the new URLConnection class added in 2018R4 instead of HTTPSocket. That gets you HTTP 1.1 support and also will follow redirects for you automatically. It also supports HTTP and HTTPS at the same time. So you don’t have to examine the 301’s Location header to see if your next call need HTTPSocket or HTTPSecureSocket.

However, it does not directly return the html contents as a string on the request. Instead you get the data from the ContentReceived event. Take a look at the sample project Xojo provides at Examples > Communication > Internet > URLConnection > URLConnectionGET (use the Xojo menu File > New > Examples )

EDIT: Actually it does have an option to do that, using SendSync so try this:

Dim socket As New URLConnection
Dim html As String = socket.SendSync("GET",TextField1.text,30)
TextArea1.Text = html

Hi All.

Thanks for all of the help. Your assistance is greatly appreciated.

Everything is working now, save for one small thing (actually a little larger…)

When I hit a site that throws a 301 error I can now see the new location. Great.
But when I go to pull a text version of the webpage, my other textArea does not show the HTML version of the webpage. It only seems to duplicate what was in textArea1.

Here is the code:

[code]Dim socket As New HTTPSocket
Dim html As String
Dim newURLHTML as string
Dim relocatedSocket as new HTTPSocket
Dim headers as InternetHeaders

//see what the html we can get when we go to the URL. It might be a redirect…
html = socket.Get(""+TextField1.text+"/", 30)
TextArea1.Text = html

//show where the real location is.

headers = relocatedSocket.GetHeaders(""+Textfield1.text+"",30)

TextArea2.text = headers.Value(“Location”)
TextArea3.text = socket.Get(""+TextArea2.text+"/", 30)
// for testing purposes only
HTMLViewer1.LoadURL (TextArea2.text)[/code]

Going to torontsun.com

TextArea1:
[b]

301 Moved Permanently

301 Moved Permanently


nginx [/b]

TextArea2:
https://torontosun.com

TextArea3:
[b]

301 Moved Permanently

301 Moved Permanently


nginx [/b]

Any ideas?

Regards

This is why I suggested switching from HTTPSocket to URLConnection instead. Note that the 301 redirect is from your original HTTP:// site to a HTTPS:// site. This will get increasingly common as websites transition to nearly always being HTTPS instead of HTTP.

If you are manually handling a redirect like this, you need to test if the new location starts HTTPS instead of HTTP, and if so, use HTTPSecureSocket. But I’d suggest you just switch to URLConnection(), which will follow redirects for you, handles both HTTP and HTTPS connections, and has HTTP 1.1 support instead of HTTP 1.0 support.

From my last reply, try this:

Dim socket As New URLConnection
Dim html As String = socket.SendSync("GET",TextField1.text,30)
TextArea1.Text = html

Hi Douglas:

I did try this example much earlier in my adventure to do this.

However, I always got an error when I run the program. Maybe it was because I was on 2019r1.1… dunno. But it worked this time.

Thanks for putting up with me.

Regards