Sign in to follow this  
Followers 0
Yuraj

GetServerHTTPHeader

2 posts in this topic

Hi all,

I have written this script - it's for getting HTTP header entry of host, it doesn't read entire HTTP response, only header and if it found that entry then stops receiving datas.

Func GetServerHTTPHeader($host,$httpEntry,$port=80)
    ; INITIALIZE TCP
    TCPStartup()

    ; CONNECT
    $ip = TCPNameToIP($host)
    $socket = TCPConnect($ip,$port)

    ; SEND HTTP GET
    $sent = TCPSend($socket,"GET / HTTP/1.1"&@CRLF&"Host: "&$host&@CRLF&"User-agent: none"&@CRLF&"Connection: close"&@CRLF&@CRLF)

    ; RECIEVE DATA
    Local $recData = ""
    Local $data = ""
    Local $lastData = ""
    Local $date = ""

    While 1
        $recData = TCPRecv($socket,1) ; Recieve byte
        
        ; Check if is end of HTTP header or @error
        If @error Or ($lastData & $data) == @CRLF Then ExitLoop

        ; Join last recieved char
        If $recData <> "" Then
            $data &= $recData
        EndIf
        
        ; If is current char end of line, get current line
        If $recData == @LF Then
            $ln = StringStripWS(StringTrimRight($data,2),2)
            ; Check for date HTTP header section
            If StringLeft(StringLower($ln),StringLen($httpEntry)) == $httpEntry Then
                $date = $ln
                ExitLoop
            Else ; if not found - continue
                $lastData = $data ; Set last data
                $data = "" ; Clear current line
            EndIf
        EndIf
        
    WEnd

    ; CLOSE SOCKET
    TCPCloseSocket($socket)
    TCPShutdown()
    
    If $date == "" Then  SetError(1) ; Set error cuz not found date
    Return $date
EndFunc

//Sorry for my bad english


ShellExecute("http://www.yuraj.ucoz.com")ProcessClose(@AutoItPID)

Share this post


Link to post
Share on other sites



TODO: Add feature for adding multiple http header entries and function will return array of results, for example: GetServerHTTPHeader("www.google.com","date;server")


ShellExecute("http://www.yuraj.ucoz.com")ProcessClose(@AutoItPID)

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0