Code: Select all
HTTP/1.1 301 Moved Permanently
Server: nginx/1.18.0 (Ubuntu)
Date: Thu, 18 Mar 2021 13:58:56 GMT
Content-Type: text/html
Content-Length: 178
Connection: close
Location: https://remix.kwed.org/
...
Code: Select all
'#Include "win\winsock2.bi"
#Include "win\ws2tcpip.bi"
'#Inclib "ws2_32.lib"
Dim As String url = "remix.kwed.org"
'Initialize Dependencies to the Windows Socket.
Dim As WSADATA wsaData
If WSAStartup(MAKEWORD(2, 2), @wsaData) Then
? "WSAStartup failed"
End -1
End If
'We first prepare some "hints" for the "getaddrinfo" function
'to tell it, that we are looking for a IPv4 TCP Connection.
Dim As ADDRINFO hints
hints.ai_family = AF_INET 'targeting IPv4
hints.ai_protocol = IPPROTO_TCP 'targeting TCP
hints.ai_socktype = SOCK_STREAM 'targeting TCP so its SOCK_STREAM
'Aquiring of the IPv4 address of a host using the newer
'"getaddrinfo" function which outdated "gethostbyname".
'It will search for IPv4 addresses using the TCP-Protocol.
Dim As ADDRINFO Ptr targetAdressInfo
Dim As DWORD getAddrRes
getAddrRes = getaddrinfo(Strptr(url), 0, @hints, @targetAdressInfo) 'https://docs.microsoft.com/en-us/windows/win32/api/ws2tcpip/nf-ws2tcpip-getaddrinfo
If getAddrRes <> 0 Or targetAdressInfo = 0 Then
? "Could not resolve the Host Name"
WSACleanup()
End -2
End If
'Create the Socket Address Informations, using IPv4
'We dont have to take care of sin_zero, it is only used to extend the length of SOCKADDR_IN to the size of SOCKADDR
Dim As SOCKADDR_IN sockAddr
sockAddr.sin_addr = Cptr(sockaddr_in Ptr, targetAdressInfo->ai_addr)->sin_addr
sockAddr.sin_family = AF_INET
sockAddr.sin_port = htons(80)
'We have to free the Address-Information from getaddrinfo again
freeaddrinfo(targetAdressInfo)
'Creation of a socket for the communication with the Web Server,
'using IPv4 and the TCP-Protocol
Dim As SOCKET webSocket = socket_(AF_INET, SOCK_STREAM, IPPROTO_TCP)
If (webSocket = INVALID_SOCKET) Then
? "Creation of the Socket Failed"
WSACleanup()
End -3
End If
'Establishing a connection to the web Socket
? "Connecting...";
If connect(webSocket, Cptr(SOCKADDR Ptr, @sockAddr), Sizeof(SOCKADDR_IN)) <> 0 Then
? "Could not connect"
closesocket(webSocket)
WSACleanup()
End -3
End If
? "Connected"
#Define CRLF Chr(13, 10) '!"\r\n"
'Sending a HTTP-GET-Request to the Web Server "GET / HTTP/1.1\r\nHost: www.google.com\r\nConnection: close\r\n\r\n";
Dim As String httpRequest = "GET / HTTP/1.1" & CRLF & "Host: " & url & CRLF & "Accept: text/html;text/plain" & CRLF & "Connection: close" & CRLF & CRLF
Dim As Integer sentBytes = send(webSocket, Strptr(httpRequest), Len(httpRequest), 0)
If sentBytes < Len(httpRequest) Or sentBytes = SOCKET_ERROR Then
? "Could not send the request to the Server"
closesocket(webSocket)
WSACleanup()
End -4
End If
'Receiving and Displaying an answer from the Web Server
Dim As Ubyte buffer(500000)
Dim As Integer dataLen, i
Dim As String website_HTML
While True
dataLen = recv(webSocket, @buffer(0), Ubound(buffer), 0)
If dataLen = 0 Then Exit While
i = 0
While (buffer(i) > 31) Or (buffer(i) = 13) Or (buffer(i) = 10)
website_HTML &= Chr(buffer(i))
i += 1
Wend
Wend
'Cleaning up Windows Socket Dependencies
closesocket(webSocket)
WSACleanup()
?
? website_HTML
?
? "Press any key to exit"
Sleep
Thx.