A funny observation related to the connection object

Just for fun, I tried to connect to a server using a raw TCP socket connection.
While creating the socket I include tls handshaking into the tuning (at least that’s what I thought). But when testing and trying fine-tune to make it working I found that this might be too hard nut to bite just using pure Basic. Okay, things like SSL/TLS are not particularly easy to understand and especially to implement. Anyhow I couldn’t make it work. When I send a GET request the CloudFront responses:

HTTP/1.1 400 Bad Request
Server: CloudFront
Date: Mon, 05 May 2025 17:35:03 GMT
Content-Type: text/html
Content-Length: 915
Connection: close
X-Cache: Error from cloudfront
Via: 1.1 837bfbe95037e42cdc86bcbd263354ea.cloudfront.net (CloudFront)
X-Amz-Cf-Pop: HEL51-P2
X-Amz-Cf-Id: yVqAjzJX7AXHSr0gTvuj_lwzLF7NUOY0jFl-u123Mk4b-xi6m3CKtg==

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<HTML><HEAD><META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=iso-8859-1">
<TITLE>ERROR: The request could not be satisfied</TITLE>
</HEAD><BODY>
<H1>400 ERROR</H1>
<H2>The request could not be satisfied.</H2>
<HR noshade size="1px">
Bad request.
We can't connect to the server for this app or website at this time. There might be too much traffic or a configuration error. Try again later, or contact the app or website owner.
<BR clear="all">
If you provide content to customers through CloudFront, you can find steps to troubleshoot and help prevent this error by reviewing the CloudFront documentation.
<BR clear="all">
<HR noshade size="1px">
<PRE>
Generated by cloudfront (CloudFront)
Request ID: yVqAjzJX7AXHSr0gTvuj_lwzLF7NUOY0jFl-u123Mk4b-xi6m3CKtg==
</PRE>
<ADDRESS>
</ADDRESS>
</BODY></HTML>

So I know that at least SSL/TLS handshaking does not work.

Here’s my code:

Sub HttpGet()

    Dim socket As Object
    Dim connection As Object
    Dim server As String
    Dim port As Integer
    Dim request As String
    Dim response As String
    Dim tls()

    server = "www.arvopaperi.fi"
    port = 443
    tls = Array(&H16,&H03,&H03,&H00,&HD5,&H01,&H00,&H00,&HD1,&H03,&H03,&H5B,&H8C,&H5A,&H1E,&H7A,&H2D)
    socket = createUnoService("com.sun.star.connection.Connector")
    connection = socket.connect("socket,host=" & server & ",port=" & port, tls)
    Erase tls
	
    request = "GET /api/pages/stocklist/XHEL HTTP/1.1" & chr(10)
    request = request & "Host: www.arvopaperi.fi" & chr(10)
    request = request & "Accept: application/json" & chr(10)
    request = request & "Accept-Encoding: *" & chr(10)
    request = request & "User-Agent: Mozilla/5.0 AppleWebKit/537.36 Chrome/83.0.4103.116" & chr(10)
    request = request & "Connection: keep-alive" & chr(10)
    request = request & "Keep-Alive: timeout=5, max=100" & chr(10)
    request = request & "Connection: close"

    Dim requestBytes() As Byte
    requestBytes = StrConv(request, vbUnicode)
    connection.Write(requestBytes)
    connection.flush()
    Erase requestBytes

    Dim responseBytes() As String
    Dim size As Long
    size = 50000
    connection.Read(responseBytes, size)	
    connection.Close()
	
    Dim i& :For i = 0 To Ubound(responseBytes)
        response = response & chr(responseBytes(i))
        If responseBytes(i) = 0 Then Exit For
    Next i
	
    MsgBox response
    Erase response : rsponse = ""
    connection =  Nothing
    socket = Nothing
	
End Sub

Then I recalled an earlier tuning based on old VBA-tuning that I made a long ago. In the original I used MSXML2.serverXMLHTTP.6.0 object for HTTP request. But I wanted to implement it with LO Basic without any external stuff. I succeed and it was so simple that I couldn’t almost understand how easy it actually was.

Here’s the code that I used to retrieve a json string from the server containing the Helsinki Stock Exchange stock list

    Dim URL As String
    URL = "https://www.arvopaperi.fi/api/pages/stocklist/XHEL"
    Dim response As String
    ReDim StrArray() As String
    
    GlobalScope.BasicLibraries.LoadLibrary("ScriptForge")
    Set FSO = CreateScriptService("FileSystem")
   	Dim inputFile As Object        
	Set inputFile = FSO.OpenTextFile(URL)
	response = inputFile.ReadAll()
	inputFile.CloseFile()
	Set FSO = Nothing

Trying to get that json string using socket is really hard because of any kind of security layers, and the simplest macro you can imagine shows the middle finger to all that’s related to the security of HTTP communication. Think about that! :woozy_face: