Connecting to a website via tcp socket

Several years ago I wrote a program to download quotes from the web, which worked well until the sites all changed from http to https format. So I have not looked at the program for about 5 years. I decided to revisit the code and try using ssl to rewrite the app. Before I started, I ran the old program on my website (which is http) to get a feel for how the program ran again, but when I did, it no longer worked. Below is the connection request, and response:

GET / HTTP/1.1
User-Agent: Comitor 7.0
Accept: text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,/;q=0.5
Accept-Language: en-us,en;q=0.5
Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7
Keep-Alive: 300
Connection: Keep-Alive

HTTP/1.1 400 Bad Request
date= Wed, 26 May 2021 19:02:57 GMT
server= Apache
content-length= 347
connection= close
content-type= text/html; charset=iso-8859-1

400 Bad Request

Bad Request

Your browser sent a request that this server could not understand.

Additionally, a 400 Bad Request error was encountered while trying to use an ErrorDocument to handle the request.

Comidor 7.0 was the name of my app, so I thought maybe the server didn’t like that. I changed the user agent the emulate Firefox, again which worked before, but again got the same message. It simply won’t connect. I used a sniffer packet to make sure my requests were exactly the same as Firefox, and they are, but no connection. Also made sure the app was using port 80, and it was. The last little morsel of error seems to indicate it got to the site? So I am stumped? Obviously can’t work on an https upgrade if I can’t get the old http app to work. Anyone have any ideas? It all worked perfectly when I quit using it 5 years ago.

did you make a simple error like wrong line endings.
CRLF may work, e.g. endOfLine.Windows with two line endings on the end.

Nope, same code as 5 years ago, eg for comitor 7.0 as my browser:

Rem this says I am comitor
if BrowserType=“Comitor 7.0” then
wr=wr+ "Host: "+server+crlf
wr=wr+ “User-Agent: Comitor 7.0”+crlf
wr=wr+“Accept: text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,/;q=0.5”+crlf
wr=wr+“Accept-Language: en-us,en;q=0.5”+crlf
wr=wr+“Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7”+crlf
end if

Monitor.MonitorSocket.connect rem MonitorSocket is my name for theTCP socket.
Monitor.MonitorSocket.write wr

If you’re simply doing HTTP/1.1 requests and using a recent version of Xojo, you should really look at using URLConnection which should greatly simplify your code.

That said, make sure you are making the request against the secure version of the site in your code, that is the https:// version because many sites just don’t allow insecure connections any more.

The other thing is to make sure you have permission to request the quotes. It may be that they now require a login or that requests come from the same domain as your site.

1 Like

Those are concerns I will address when I try to upgrade the app to https.

However, the web site I am testing this old app on is my own website and it is strictly http; no logins or permissions required. So none of this should prevent the old app from working on my site.

Two crlf on the end!

Yes, my code does stick an extra crlf before connecting; sorry I should have put that in the code listed, but I was more concerned about what the requests were, in case there might be errors there.

Perhaps, in another response to Greg, maybe my website server only accepts secure requests, even though my site is strictly written in http. I will call the server about that when I get a chance.

Yeah you don’t write a site in http per se. Many website providers will now automatically upgrade connections to https these days just because it adds a level of security.

Anyway, don’t waste your time reinventing the wheel.
Check URLConnection in Xojo. If you need more, we have a few classes available in MBS Xojo Plugins like the cURLSMBS class.

Thanks, I will.

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.