Sign in to follow this  
Followers 0
lionfaggot

problems with winhttp

12 posts in this topic

$oHTTP = ObjCreate("winhttp.winhttprequest.5.1")
Func _Get($host, $page, $referrer = 0, $headret = 0)
    $hostpage = "http://" & $host & $page
    ;MsgBox(0,"",$host)
    $gcookie = _getcookie($host)
    $oHTTP.Open("GET", $hostpage, False)
    $oHTTP.SetRequestHeader("User-Agent", "Mozilla/5.0 (Windows NT 6.1; rv:27.0) Gecko/20100101 Firefox/27.0")
    If $referrer <> 0 Then
        $oHTTP.SetRequestHeader("Referer", $referrer)
    EndIf
    If $gcookie <> -1 Then
        $cookm = StringReplace($gcookie, "Cookie: ", "")
        $oHTTP.SetRequestHeader("Cookie", $cookm)
    EndIf
    $oHTTP.Send()
    $HeaderResponses = $oHTTP.GetAllResponseHeaders()
    _cookie($host, $HeaderResponses)
    If $headret = 0 Then
        Return $oHTTP.ResponseText
    Else
        Return $HeaderResponses
    EndIf
EndFunc   ;==>_Get

Func _Post($host, $page, $pdat, $referrer = 0, $headret = 0)
    $hostpage = "http://" & $host & $page
    ;MsgBox(0,"",$host)
    $gcookie = _getcookie($host)
    $oHTTP.Open("POST", $hostpage, True)
    $oHTTP.SetRequestHeader("User-Agent", "Mozilla/5.0 (Windows NT 6.1; rv:27.0) Gecko/20100101 Firefox/27.0")
    If $referrer <> 0 Then
        $oHTTP.SetRequestHeader("Referer", $referrer)
    EndIf
    $oHTTP.SetRequestHeader("Content-Type", "application/x-www-form-urlencoded")
    If $gcookie <> -1 Then
        $cookm = StringReplace($gcookie, "Cookie: ", "")
        $oHTTP.SetRequestHeader("Cookie", $cookm)
    EndIf
    $oHTTP.Send($pdat)
    $HeaderResponses = $oHTTP.GetAllResponseHeaders()
    _cookie($host, $HeaderResponses)
    If $headret = 0 Then
        Return $oHTTP.ResponseText
    Else
        Return $HeaderResponses
    EndIf
EndFunc   ;==>_Post

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

everything works fine but i do not think it is sending the cookie with the request, and no, i do not want to use winhttp.au3. the functions surrounding that udf annoy me

Share this post


Link to post
Share on other sites



bump

Share this post


Link to post
Share on other sites

lionfaggot,

Please do not bump your own threads within 24 hours. :naughty:

Remember that this is not a 24/7 support forum - those who answer are only here because they like helping others and have some time to spare. Although the problem might be urgent to you, it is not to anyone else. You just have to wait until someone who knows something about your particular problem, and is willing to help, comes online. Be patient and someone will answer eventually. ;)

M23


Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind._______My UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Share this post


Link to post
Share on other sites

#4 ·  Posted (edited)

i think winhttp is just broken, i got this example from other people. this is in the msdn documentation. im about to ditch windows iif i cant get this working, like straight up. same set of events more ore less in perl on linux with curl works 100% with no frills. and yes i know you can use curl on windows, but it does not come with windows so its not optimal

Edited by lionfaggot

Share this post


Link to post
Share on other sites

so uh, its been days so im gonna bump this again. surely someone knows why winhttp just refuses to handle cookies

Share this post


Link to post
Share on other sites

#6 ·  Posted (edited)

I'm pretty sure WinHTTP handles cookies -- it's just a matter of finding out what it

expects for a particular request.

I used your script in the OP, and set a request with a cookie, and the server returned

with the Same cookie. So, That seems to be working okay.

I'm not sure what it is you're trying to do -- or where/what point it is failing.

If you could be a bit more specific with details...

.

Edited by ripdad

"The mediocre teacher tells. The Good teacher explains. The superior teacher demonstrates. The great teacher inspires." -William Arthur Ward

Share this post


Link to post
Share on other sites

I'm pretty sure WinHTTP handles cookies -- it's just a matter of finding out what it

expects for a particular request.

I used your script in the OP, and set a request with a cookie, and the server returned

with the Same cookie. So, That seems to be working okay.

I'm not sure what it is you're trying to do -- or where/what point it is failing.

If you could be a bit more specific with details...

.

yes, the cookie sets to file, but does not carry over in the request. if winhttp autohandles cookies then why am i having such a hard time carrying through with those provisions? i have no problems setting the cookie, the issue im having deals with using the set cookie

Share this post


Link to post
Share on other sites

In that case, WinHTTP does not "autohandle" cookies. You have to code that part yourself.

Just keep using the same cookie, until you're done with it.

If you get stuck - come back with a snippet and we'll try to figure it out.


"The mediocre teacher tells. The Good teacher explains. The superior teacher demonstrates. The great teacher inspires." -William Arthur Ward

Share this post


Link to post
Share on other sites

In that case, WinHTTP does not "autohandle" cookies. You have to code that part yourself.

Just keep using the same cookie, until you're done with it.

If you get stuck - come back with a snippet and we'll try to figure it out.

ok, great and all, but why is the request not using the cookie i specify? i can specify any value and it will not set the supplied cookie.

Share this post


Link to post
Share on other sites

I have not tried anything with "cookie handling" and WinHTTP, but I would suppose the first step is to save the cookie to a Global variable, once you have it, then call a specific function that does what you expect.

One thing about WinHTTP, you can only call each WinHTTP function one at a time with Send. So, you might have to call one or more functions to get what you want.

I'm still not clear on what you're trying to achieve.


"The mediocre teacher tells. The Good teacher explains. The superior teacher demonstrates. The great teacher inspires." -William Arthur Ward

Share this post


Link to post
Share on other sites

I have not tried anything with "cookie handling" and WinHTTP, but I would suppose the first step is to save the cookie to a Global variable, once you have it, then call a specific function that does what you expect.

One thing about WinHTTP, you can only call each WinHTTP function one at a time with Send. So, you might have to call one or more functions to get what you want.

I'm still not clear on what you're trying to achieve.

ok dude, as you can PLAINLY SEE, my function has no issues recording the cookie. but it wont USE the cookies. which makes no sense at all, like literally its fairy unicorn logic, and the reason no one can help me is because i can p much guarantee no one has gotten this to function, ever. not even once

Share this post


Link to post
Share on other sites

I don't mind a merry-go-round two or three times. After that, I get dizzy.

You haven't provided enough information to latch onto.

Good luck to you!


"The mediocre teacher tells. The Good teacher explains. The superior teacher demonstrates. The great teacher inspires." -William Arthur Ward

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