You need my functions SaveCookies() and SetCookies() and provide a variable in which the cookies can be stored.
Let me explain it by this example:
Code: Select all
cookies := Object() ;the variable in which the cookies are going to be stored in
WebRequest := ComObjCreate("WinHttp.WinHttpRequest.5.1")
WebRequest.Open("GET", "http://google.com") ;If you want to send the cookies from this domain that are in the cookies variable you have to call SetCookies now:
SetCookies(WebRequest, cookies) ;The first parameter is the variable to which we intialized the WinHttpRequest object, the second parameter is the variable from which we will read our cookies.
WebRequest.Send() ;If you want to save the cookies for this request, you have to call SaveCookies now:
SaveCookies(WebRequest, cookies) ;The first parameter is the variable to which we intialized the WinHttpRequest object, the second parameter is the variable to which we will add our new cookies.
Code: Select all
SaveCookies(ByRef WebRequest, ByRef cookies) {
While (p := RegexMatch(WebRequest.GetAllResponseHeaders, "U)(^|\R)Set-Cookie:\s(.+)=(.+);.+domain=(.+)(\R|;|$)", match, p?p+1:1))
cookies[match4, match2] := match3
}
SetCookies(ByRef WebRequest, ByRef cookies) {
url := WebRequest.Option(1) ;the url that we are going to send our request to
If (p := InStr(url,"://"))
url := SubStr(url, p+3)
If (p := InStr(url,"/"))
url := SubStr(url, 1, p-1)
If (p := InStr(url,"@"))
url := SubStr(url, p+1)
If (p := InStr(url,":"))
url := SubStr(url, 1, p-1)
StringSplit, a, url, .
b := a0-1
domain := a%b%
ext := a%a0%
url := "." . domain . "." . ext
cookieString := ""
For id, value in cookies[url]
cookieString .= id . "=" . value . "; "
If (cookieString) ;if there are any cookies
WebRequest.SetRequestHeader("Cookie", cookieString)
}
Automatically remove cookies that expired.