i made two functions: _Get() and _Post() which both have an ssl option parameter, both return the body of the response. and all of that works, however i added in automatic cookie handling which is failing on some sites. anyone want to help me out? these functions will make things quite a bit simpler with much less typing if we can make it work. heres my code:
#include <WinHttp.au3>
Func _Get($host, $page, $ssl = 0)
Local $hOpen = _WinHttpOpen()
If $ssl = 0 Then
Local $hConnect = _WinHttpConnect($hOpen, $host)
Local $req = _WinHttpOpenRequest($hConnect, "GET", $page)
Else
Local $hConnect = _WinHttpConnect($hOpen, $host, $INTERNET_DEFAULT_HTTPS_PORT)
Local $req = _WinHttpOpenRequest($hConnect, "GET", $page, Default, Default, Default, $WINHTTP_FLAG_SECURE)
EndIf
_WinHttpAddRequestHeaders($hConnect, "User-Agent: Mozilla/5.0 (Windows NT 6.1; rv:25.0) Gecko/20100101 Firefox/25.0")
_WinHttpAddRequestHeaders($hConnect, "Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8")
_WinHttpAddRequestHeaders($hConnect, "Accept-Language: en-US,en;q=0.5")
_WinHttpAddRequestHeaders($hConnect, "Accept-Encoding: gzip, deflate")
_WinHttpAddRequestHeaders($hConnect, "Referer: http://www.google.com/")
$gcookie = _getcookie($host)
If $gcookie <> -1 Then
_WinHttpAddRequestHeaders($hConnect, $gcookie)
EndIf
_WinHttpAddRequestHeaders($hConnect, "Connection: keep-alive" & @CRLF)
_WinHttpSendRequest($req)
Local $resp = _WinHttpReceiveResponse($req)
If $resp = 1 Then
$headers = _WinHttpQueryHeaders($req)
If StringInStr($headers, "Set-Cookie:") Then
_cookie($host, $headers)
EndIf
Local $sresp = _WinHttpReadData($req)
Return $sresp
EndIf
EndFunc ;==>_Get
Func _Post($host, $page, $pdata, $ssl = 0)
Local $hOpen = _WinHttpOpen()
If $ssl = 0 Then
Local $hConnect = _WinHttpConnect($hOpen, $host)
Local $req = _WinHttpOpenRequest($hConnect, "POST", $page)
Else
Local $hConnect = _WinHttpConnect($hOpen, $host, $INTERNET_DEFAULT_HTTPS_PORT)
Local $req = _WinHttpOpenRequest($hConnect, "POST", $page, Default, Default, Default, $WINHTTP_FLAG_SECURE)
EndIf
_WinHttpAddRequestHeaders($hConnect, "User-Agent: Mozilla/5.0 (Windows NT 6.1; rv:25.0) Gecko/20100101 Firefox/25.0")
_WinHttpAddRequestHeaders($hConnect, "Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8")
_WinHttpAddRequestHeaders($hConnect, "Accept-Language: en-US,en;q=0.5")
_WinHttpAddRequestHeaders($hConnect, "Accept-Encoding: gzip, deflate")
_WinHttpAddRequestHeaders($hConnect, "Referer: http://www.google.com/")
$gcookie = _getcookie($host)
If $gcookie <> -1 Then
_WinHttpAddRequestHeaders($hConnect, $gcookie)
EndIf
_WinHttpAddRequestHeaders($hConnect, "Connection: keep-alive")
_WinHttpAddRequestHeaders($hConnect, "Content-Type: application/x-www-form-urlencoded")
_WinHttpAddRequestHeaders($hConnect, "Content-Length: "&StringLen($pdata))
_WinHttpAddRequestHeaders($hConnect, @CRLF & $pdata)
_WinHttpSendRequest($req)
Local $resp = _WinHttpReceiveResponse($req)
If $resp = 1 Then
$headers = _WinHttpQueryHeaders($req)
If StringInStr($headers, "Set-Cookie:") Then
_cookie($host, $headers)
EndIf
Local $sresp = _WinHttpReadData($req)
Return $sresp
EndIf
EndFunc ;==>_Post
Func _cookie($host, $retcook)
DirCreate("Cookies" & $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,}", ";")
FileDelete("Cookies" & $host & "\" & $cookname & ".txt")
FileWrite("Cookies" & $host & "\" & $cookname & ".txt", $cookies)
Next
EndFunc ;==>_cookie
Func _getcookie($host)
Local $noret = 0
$cookrr = "Cookie:"
$search = FileFindFirstFile("Cookies" & $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("Cookies" & $host & "\" & $file)
EndIf
WEnd
EndIf
$noret = 0
EndFunc ;==>_getcookie
Get example: MsgBox(0, "", _Get("www.somesite.com", "/page"))
Get example (ssl): MsgBox(0, "", _Get("www.somesite.com", "/page", 1))
Post example: MsgBox(0, "", _Post("www.somesite.com", "/login", "user=fork&pass=lolfork"))
Post example (ssl): MsgBox(0, "", _Post("www.somesite.com", "/login", "user=fork&pass=lolfork", 1))