Jump to content

HTTP functions(get, post, urlencode) that autohandles cookies


lionfaggot
 Share

Recommended Posts

TCPStartup()
Func _Get($host, $page)
    $gcookie = _getcookie($host)
    $req = "GET " & $page & " HTTP/1.1" & @CRLF
    $req &= "Host: " & $host & @CRLF
    $req &= "User-Agent: Mozilla/5.0 (Windows NT 6.1; rv:27.0) Gecko/20100101 Firefox/27.0" & @CRLF
    $req &= "Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8" & @CRLF
    $req &= "Accept-Language: en-US,en;q=0.5" & @CRLF
    $req &= "Accept-Encoding: deflate" & @CRLF
    $req &= "Referer: http://www.google.com/" & @CRLF
    If $gcookie <> -1 Then
        $req &= $gcookie & @CRLF
    EndIf
    $req &= "Connection: close" & @CRLF & @CRLF
    $socket = TCPConnect(TCPNameToIP($host), 80)
    If @error Then
        Return 0
    Else
        TCPSend($socket, $req)
        If @error Then
            Return 0
        Else
            Return _httprecv($host, $socket)
        EndIf
    EndIf
EndFunc   ;==>_Get

Func _Post($host, $page, $pdat)
    $gcookie = _getcookie($host)
    $req = "POST " & $page & " HTTP/1.1" & @CRLF
    $req &= "Host: " & $host & @CRLF
    $req &= "User-Agent: Mozilla/5.0 (Windows NT 6.1; rv:27.0) Gecko/20100101 Firefox/27.0" & @CRLF
    $req &= "Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8" & @CRLF
    $req &= "Accept-Language: en-US,en;q=0.5" & @CRLF
    $req &= "Accept-Encoding: deflate" & @CRLF
    $req &= "Content-Type: application/x-www-form-urlencoded; charset=UTF-8" & @CRLF
    $req &= "Referer: http://www.google.com/" & @CRLF
    $req &= "Content-Length: " & StringLen($pdat) & @CRLF
    If $gcookie <> -1 Then
        $req &= $gcookie & @CRLF
    EndIf
    $req &= "Connection: close" & @CRLF & @CRLF
    $req &= $pdat & @CRLF
    $socket = TCPConnect(TCPNameToIP($host), 80)
    If @error Then
        Return 0
    Else
        TCPSend($socket, $req)
        If @error Then
            Return 0
        Else
            Return _httprecv($host, $socket)
        EndIf
    EndIf
EndFunc   ;==>_Post

Func _httprecv($host, $hsock)
    Local $ret
    While 1
        $data = TCPRecv($hsock, 2048)
        If @error Then
            TCPCloseSocket($hsock)
            ExitLoop
        Else
            If $data <> "" Then
                $ret &= $data
                If StringInStr($data, "Set-Cookie:") Then
                    DirCreate(@UserProfileDir & "\" & $host & "\")
                    _cookie($host, $ret)
                EndIf
            EndIf
        EndIf
    WEnd
    Return $ret
EndFunc   ;==>_httprecv

Func _cookie($host, $retcook)
    DirCreate(@UserProfileDir & "\" & $host & "\")
    $array = StringRegExp($retcook, "Set-Cookie: (.+)\r\n", 3)
    $cookies = ""
    For $i = 0 To UBound($array) - 1
        $cookies = $array[$i] & ";"
        $csplit = StringSplit($cookies, "=")
        $cookname = $csplit[1]
        $cookies = StringRegExpReplace($cookies, "( path| domain| expires)=[^;]+", "")
        $cookies = StringRegExpReplace($cookies, " HttpOnly", "")
        $cookies = StringRegExpReplace($cookies, "[;]{2,}", ";")
        DirCreate(@UserProfileDir & "\" & $host & "\")
        FileDelete(@UserProfileDir & "\" & $host & "\" & $cookname & ".txt")
        FileWrite(@UserProfileDir & "\" & $host & "\" & $cookname & ".txt", $cookies)
    Next
EndFunc   ;==>_cookie

Func _getcookie($host)
    Local $noret = 0
    $cookrr = "Cookie:"
    $search = FileFindFirstFile(@UserProfileDir & "\" & $host & "\*.txt")
    If @error Then
        Return -1
        $noret = 1
    EndIf
    If $noret <> 1 Then
        While 1
            $file = FileFindNextFile($search)
            If @error Then
                Return $cookrr
                ExitLoop
            Else
                $cookrr &= " " & FileRead(@UserProfileDir & "\" & $host & "\" & $file)
            EndIf
        WEnd
    EndIf
    $noret = 0
EndFunc   ;==>_getcookie

Func _URLEncode($toEncode, $encodeType = 0)
    Local $strHex = "", $iDec
    Local $aryChar = StringSplit($toEncode, "")
    If $encodeType = 1 Then;;Encode EVERYTHING
        For $i = 1 To $aryChar[0]
            $strHex = $strHex & "%" & Hex(Asc($aryChar[$i]), 2)
        Next
        Return $strHex
    ElseIf $encodeType = 0 Then;;Practical Encoding
        For $i = 1 To $aryChar[0]
            $iDec = Asc($aryChar[$i])
            If $iDec <= 32 Or $iDec = 37 Then
                $strHex = $strHex & "%" & Hex($iDec, 2)
            Else
                $strHex = $strHex & $aryChar[$i]
            EndIf
        Next
        Return $strHex
    ElseIf $encodeType = 2 Then;;RFC 1738 Encoding
        For $i = 1 To $aryChar[0]
            If Not StringInStr("$-_.+!*'(),;/?:@=&abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890", $aryChar[$i]) Then
                $strHex = $strHex & "%" & Hex(Asc($aryChar[$i]), 2)
            Else
                $strHex = $strHex & $aryChar[$i]
            EndIf
        Next
        Return $strHex
    EndIf
EndFunc   ;==>_URLEncode

_Get("www.yahoo.com", "/") - Get

_Post("www.somesite.com", "/login.php", "user=lol&pass=lol") - Post

both return a full http response from the host

_URLEncode("somestring")

----------------

hopefully some folks will find this useful, it requires less code to use than the HTTP UDF and autohandles cookies. enjoy!

Link to comment
Share on other sites

this is probably the most complete set of http functions. will be updating soon to include header stripping to return body and headers seperately. also, im noting that lots of websites these days set the cookie using javascript. seems like a daunting task to take on. however i think it is necessary, i know quite a bit about javascript but im not sure how to tackle interpreting cookie events from it. any input appreciated!

Link to comment
Share on other sites

this is probably the most complete set of http functions. will be updating soon to include header stripping to return body and headers seperately. also, im noting that lots of websites these days set the cookie using javascript. seems like a daunting task to take on. however i think it is necessary, i know quite a bit about javascript but im not sure how to tackle interpreting cookie events from it. any input appreciated!

You can't track javascript events(at least not without making a browser window), what you have to do is interpret the javascript, the same way the browser does.

Edited by nullschritt
Link to comment
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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...