Juno_okyo

HTTP Request UDF

Dec 17th, 2014
3,026
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
AutoIt 3.36 KB | None | 0 0
  1. Global $cookies[1][2]
  2. Func makeHttpRequest($lnk, $cookie = '', $refer = '', $ignorecookies = 0, $post = 0, $redir = 0)
  3.     $method = "GET"
  4.     $weblink = StringSplit($lnk, '?')
  5.     ;addText($weblink[2],$hListBox)
  6.     If $post = 1 Then
  7.         $method = "POST"
  8.         $linklink = $weblink[1]
  9.         $poststring = $weblink[2]
  10.     Else
  11.         $linklink = $lnk
  12.     EndIf
  13.     $oHTTP = ObjCreate("WinHttp.WinHttpRequest.5.1")
  14.     If $redir = 0 Then
  15.         $oHTTP.Option(6) = False
  16.     EndIf
  17.     $oHTTP.Open($method, $linklink, False)
  18.     $oHTTP.SetRequestHeader("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:19.0) Gecko/20100101 Firefox/19.0")
  19.     If StringLen($refer) > 1 Then
  20.         $oHTTP.SetRequestHeader("Referer", $refer)
  21.     EndIf
  22.     If StringLen($cookie) > 1 Then
  23.         $oHTTP.SetRequestHeader("Cookie", $cookie)
  24.     EndIf
  25.     If $post = 1 Then
  26.         $oHTTP.SetRequestHeader("Content-Type", "application/x-www-form-urlencoded")
  27.         $oHTTP.SetRequestHeader("Content-Length", StringLen($poststring))
  28.         $oHTTP.Send($poststring)
  29.     Else
  30.         $oHTTP.Send()
  31.     EndIf
  32.     $oHTTP.WaitForResponse
  33.     $HeaderResponses = $oHTTP.GetAllResponseHeaders()
  34.     If $ignorecookies = 0 Then
  35.         ; Handle Cookies
  36.         $array = StringRegExp($HeaderResponses, 'Set-Cookie: (.+)\r\n', 3)
  37.         ;$cookies = ''
  38.         For $i = 0 To UBound($array) - 1
  39.             ; Add all cookies to a single string, and then clean it up.
  40.             $cook = $array[$i] & ';'
  41.             ; Removing parts we do not use..
  42.             $cook = StringRegExpReplace($cook, "( path| domain| expires)=[^;]+", "")
  43.             $cook = StringRegExpReplace($cook, " HttpOnly", "")
  44.             $cook = StringRegExpReplace($cook, "[;]{2,}", ";")
  45.             $cook1 = StringSplit($cook, ";")
  46.             For $k = 1 To $cook1[0]
  47.                 If StringInStr($cook1[$k], "=") Then
  48.                     $cook2 = StringSplit(StringReplace($cook1[$k], " ", ""), "=")
  49.                     If $cookies[0][0] == "" Then
  50.                         $cookies[0][0] = $cook2[1]
  51.                         $cookies[0][1] = $cook2[2]
  52.                     Else
  53.                         $inserted = 0
  54.                         For $j = 0 To UBound($cookies) - 1
  55.                             If $cook2[1] == $cookies[$j][0] Then
  56.                                 $cookies[$j][1] = $cook2[2]
  57.                                 $inserted = 1
  58.                             ElseIf ($j == (UBound($cookies) - 1)) And ($inserted == 0) Then
  59.                                 ReDim $cookies[UBound($cookies) + 1][2]
  60.                                 $cookies[UBound($cookies) - 1][0] = $cook2[1]
  61.                                 $cookies[UBound($cookies) - 1][1] = $cook2[2]
  62.                             EndIf
  63.                         Next
  64.                     EndIf
  65.                 EndIf
  66.             Next
  67.         Next
  68.     EndIf
  69.     Dim $ret[4]
  70.     If StringInStr($HeaderResponses, "Location:") <> 0 Then
  71.         $ret["0"] = $oHTTP.GetResponseHeader("Location")
  72.         $ret["1"] = 1
  73.     Else
  74.         $ret["0"] = ""
  75.         $ret["1"] = 0
  76.     EndIf
  77.     $ret["2"] = $oHTTP.Responsetext
  78.     ;$ret["method"] = $method
  79.     $ret["3"] = $oHTTP.GetAllResponseHeaders()
  80.     Return $ret
  81. EndFunc   ;==>getHTTP
  82. ;function to encode url for websurfing
  83. Func URLEncode($urlText)
  84.     $url = ""
  85.     For $i = 1 To StringLen($urlText)
  86.         $acode = Asc(StringMid($urlText, $i, 1))
  87.         Select
  88.             Case ($acode >= 48 And $acode <= 57) Or _
  89.                     ($acode >= 65 And $acode <= 90) Or _
  90.                     ($acode >= 97 And $acode <= 122)
  91.                 $url = $url & StringMid($urlText, $i, 1)
  92.             Case $acode = 32
  93.                 $url = $url & "+"
  94.             Case Else
  95.                 $url = $url & "%" & Hex($acode, 2)
  96.         EndSelect
  97.     Next
  98.     Return $url
  99. EndFunc   ;==>URLEncode
  100. ;function to make cookies from array
  101. Func mkCookies()
  102.     $rt = ""
  103.     For $j = 0 To UBound($cookies) - 1
  104.         $rt = $rt & $cookies[$j][0] & "=" & $cookies[$j][1]
  105.         If $j <> (UBound($cookies) - 1) Then
  106.             $rt = $rt & "; "
  107.         EndIf
  108.     Next
  109.     Return $rt
  110. EndFunc   ;==>mkCookies
Add Comment
Please, Sign In to add comment