Jump to content

WinHTTP functions


trancexx
 Share

Recommended Posts

  • 1 month later...

Hi :)

Very nice script!

One question though:

When there's more than one response header with the same name, how do I get the second?

This returns only the first cookie in "Set-Cookie" return header:

$cookieResp = _WinHttpQueryHeaders($h_openRequest, $WINHTTP_QUERY_SET_COOKIE)

The case is that I have a server returning 2 cookies and I need them both. Am trying to avoid parsing the full header. Was hoping the _WinHttpQueryHeaders could do it.

Thanks!

Link to comment
Share on other sites

Hi :)

Very nice script!

One question though:

When there's more than one response header with the same name, how do I get the second?

This returns only the first cookie in "Set-Cookie" return header:

$cookieResp = _WinHttpQueryHeaders($h_openRequest, $WINHTTP_QUERY_SET_COOKIE)

The case is that I have a server returning 2 cookies and I need them both. Am trying to avoid parsing the full header. Was hoping the _WinHttpQueryHeaders could do it.

Thanks!

No, just one. You have to read it all if you want more.
Link to comment
Share on other sites

Thank you trancexx.

So this is not related to what I'm trying to achieve?

http://msdn.microsoft.com/en-us/library/aa384102(VS.85).aspx

Quoting from that page: "lpdwIndex [in, out] - Pointer to a zero-based header index used to enumerate multiple headers with the same name."

Thanks :)

Yes that's it. In WinHTTP.au3 WINHTTP_NO_HEADER_INDEX is used for that parameter.

I didn't thought it would be needed to have it changable. My thinking was - why call function five (e.g.) times to get all cookies (e.g. again) when you can regexp it from the full header.

But I guess you could have need to target the second one.

In any case it was me that set the limit. Maybe I shouldn't have.

Link to comment
Share on other sites

Yes that's it. In WinHTTP.au3 WINHTTP_NO_HEADER_INDEX is used for that parameter.

I didn't thought it would be needed to have it changable. My thinking was - why call function five (e.g.) times to get all cookies (e.g. again) when you can regexp it from the full header.

But I guess you could have need to target the second one.

In any case it was me that set the limit. Maybe I shouldn't have.

It's no biggie. The reason why I asked is because I don't know enough AutoIT to modify your _WinHttpQueryHeaders so that it returns multiple values of cookies (msdn page is enough but not for me :) )

Parsing the full string (delimited with CRLF) is causing me codepage issues because, for ex, _ArrayFindAll (after StringSplit) is not finding the string "Set-Cookie" (I just tested and, for ex, my "S", is not the same as the "S" that I'm getting returned).

Is it complicated to adapt your function?

RegExp is also not my forté..

Thanks in advance

Link to comment
Share on other sites

Try this:

#include <Array.au3>

$aCookies = StringRegExp($sFullHeader, "Set-Cookie: (.*?)" & @CRLF, 3)

_ArrayDisplay($aCookies)

Awesome. thanks again trancexx.

Look what I was doing (rookie style):

$headersArray = StringSplit($headers, @CRLF, 1)

For $ha = 1 To UBound($headersarray)-1

If stringleft($headersarray[$ha],20) = "Set-Cookie: .EMCAUTH" Then

$Cookie1 = StringMid($headersarray[$ha], 5)

ElseIf stringleft($headersarray[$ha],29) = "Set-Cookie: ASP.NET_SessionId" Then

$Cookie2 = StringMid($headersarray[$ha], 5)

EndIf

Next

:)

Link to comment
Share on other sites

ok I am haveing troubles getting all the data off the server. the code is messy from me experamenting trying to get things to work.

#include "WinHTTP.au3"
#include <EditConstants.au3>
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#Region ### START Koda GUI section ### Form=
$Form4 = GUICreate("Form1", 405, 482, 302, 218)
$Edit1 = GUICtrlCreateEdit("", 8, 8, 385, 465)
GUICtrlSetData(-1, "Edit1")
GUISetState(@SW_SHOW)
#EndRegion ### END Koda GUI section ###
$apikey = "wD8TLIF9ATuNxxm6iD67QpM8JbmVRnkDgVKxjKzo6I7O1AM3JseNkesCAyrbKNN6"
$userid = "3084871"
$charid = "1753893327"
$address = "api.eve-online.com"
While 1
    $nMsg = GUIGetMsg()
    Dim $output
$hw_open = _WinHttpOpen()
$hw_connect = _WinHttpConnect($hw_open, $address)
$h_openRequest = _WinHttpOpenRequest($hw_connect, "POST ", "/eve/SkillTree.xml.aspx?"&"apiKey="&$apikey&"&userID="&$userid&"&characterID="&$charid)


_WinHttpSendRequest($h_openRequest)
_WinHttpReceiveResponse($h_openRequest)
If _WinHttpQueryDataAvailable($h_openRequest) Then
    $header = _WinHttpQueryHeaders($h_openRequest)
    $header = _WinHttpReadData($h_openRequest,1000000)
    FileWrite("Skills.xml", @CRLF&$header)
        GUICtrlSetData($Edit1, $header)
EndIf
_WinHttpCloseHandle($h_openRequest)
_WinHttpCloseHandle($hw_connect)
_WinHttpCloseHandle($hw_open)
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Exit
    EndSwitch
WEnd

this is supposed to download an XML documents contents off the API server for EVE-online. I am only recieveing mayby a 1/20th of the file can someone help me to be able to get the whole file?

Link to comment
Share on other sites

ok I am haveing troubles getting all the data off the server. the code is messy from me experamenting trying to get things to work.

#include "WinHTTP.au3"
#include <EditConstants.au3>
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#Region ### START Koda GUI section ### Form=
$Form4 = GUICreate("Form1", 405, 482, 302, 218)
$Edit1 = GUICtrlCreateEdit("", 8, 8, 385, 465)
GUICtrlSetData(-1, "Edit1")
GUISetState(@SW_SHOW)
#EndRegion ### END Koda GUI section ###
$apikey = "wD8TLIF9ATuNxxm6iD67QpM8JbmVRnkDgVKxjKzo6I7O1AM3JseNkesCAyrbKNN6"
$userid = "3084871"
$charid = "1753893327"
$address = "api.eve-online.com"
While 1
    $nMsg = GUIGetMsg()
    Dim $output
$hw_open = _WinHttpOpen()
$hw_connect = _WinHttpConnect($hw_open, $address)
$h_openRequest = _WinHttpOpenRequest($hw_connect, "POST ", "/eve/SkillTree.xml.aspx?"&"apiKey="&$apikey&"&userID="&$userid&"&characterID="&$charid)


_WinHttpSendRequest($h_openRequest)
_WinHttpReceiveResponse($h_openRequest)
If _WinHttpQueryDataAvailable($h_openRequest) Then
    $header = _WinHttpQueryHeaders($h_openRequest)
    $header = _WinHttpReadData($h_openRequest,1000000)
    FileWrite("Skills.xml", @CRLF&$header)
        GUICtrlSetData($Edit1, $header)
EndIf
_WinHttpCloseHandle($h_openRequest)
_WinHttpCloseHandle($hw_connect)
_WinHttpCloseHandle($hw_open)
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Exit
    EndSwitch
WEnd

this is supposed to download an XML documents contents off the API server for EVE-online. I am only recieveing mayby a 1/20th of the file can someone help me to be able to get the whole file?

Try this:

#include "WinHTTP.au3"

Global $sApiKey = "wD8TLIF9ATuNxxm6iD67QpM8JbmVRnkDgVKxjKzo6I7O1AM3JseNkesCAyrbKNN6"
Global $sUserId = "3084871"
Global $sCharId = "1753893327"
Global $sAddress = "api.eve-online.com"
Global $hOpen = _WinHttpOpen()

If @error Then
    MsgBox(48, "Error", "Error initializing the usage of WinHTTP functions.")
    Exit
EndIf

_WinHttpSetOption($hOpen, $WINHTTP_OPTION_USER_AGENT, "Who the fuc*k is Alice???")

Global $hConnect = _WinHttpConnect($hOpen, $sAddress)
If @error Then
    MsgBox(48, "Error", "Error specifying the initial target server of an HTTP request.")
    _WinHttpCloseHandle($hOpen)
    Exit
EndIf

Global $hRequest = _WinHttpOpenRequest($hConnect, "POST ", "/eve/SkillTree.xml.aspx?" & "apiKey=" & $sApiKey & "&userID=" & $sUserId & "&characterID=" & $sCharId)
If @error Then
    MsgBox(48, "Error", "Error creating request handle.")
    _WinHttpCloseHandle($hConnect)
    _WinHttpCloseHandle($hOpen)
    Exit
EndIf

_WinHttpSendRequest($hRequest)
If @error Then
    MsgBox(48, "Error", "Error sending specified request.")
    _WinHttpCloseHandle($hConnect)
    _WinHttpCloseHandle($hOpen)
    Exit
EndIf

_WinHttpReceiveResponse($hRequest)

If _WinHttpQueryDataAvailable($hRequest) Then
    Global $sHeader = _WinHttpQueryHeaders($hRequest)
    ConsoleWrite($sHeader & @CRLF & @CRLF)
    Global $sChunk, $sData
    While 1
        $sChunk = _WinHttpReadData($hRequest)
        If @error Then ExitLoop
        $sData &= $sChunk
    WEnd

    ;ConsoleWrite($sData & @CRLF)

    Global $sFile = @DesktopDir & "\Skills.xml"
    Global $hFile = FileOpen($sFile, 2)
    FileWrite($hFile, $sData)
    FileClose($hFile)

Else
    MsgBox(48, "Error", "Site is experiencing problems.")
EndIf

_WinHttpCloseHandle($hRequest)
_WinHttpCloseHandle($hConnect)
_WinHttpCloseHandle($hOpen)
Link to comment
Share on other sites

  • 4 weeks later...

Hello,

I want get cookies returned by a web page.

This web page should return a code 302 (moved temporarly) but I get a 200 code when I use _WinHttpQueryHeaders.

When I look in wireshark (network protocol analyzer) I see the code 302 first followed by 200.

How to get the first header ?

Thx

Link to comment
Share on other sites

Hello,

I want get cookies returned by a web page.

This web page should return a code 302 (moved temporarly) but I get a 200 code when I use _WinHttpQueryHeaders.

When I look in wireshark (network protocol analyzer) I see the code 302 first followed by 200.

How to get the first header ?

Thx

I found the answer : _WinHttpSetOption($h_openRequest,$WINHTTP_OPTION_DISABLE_FEATURE,$WINHTTP_DISABLE_REDIRECTS)
Link to comment
Share on other sites

  • 1 month later...

Uh, yes, it's not a support forum.

But I'll post my question here hoping someone can give me a helping hand.

The tcp-solution gives me more info then the one using winhttp.

What parameters should I set to get these?

It seems im blindfolded.

Edit: Uh, the 302 output is a redirect, so I need to enable that...

Maybe you can tell me how I should adjust the call. ;)

So, here is the code:

; ==========================================
; TCP-solution (done by MrCreatoR)
; ==========================================

Global $g_HTTPUserAgent = 'AutoIt3/'&@AutoItVersion, $g_Limit_TimeOut = 5000
Global $g_HTTP_TCP_Def_Port = 80, $g_HTTP_TCP_Port = $g_HTTP_TCP_Def_Port, $g_LAST_SOCKET = -1

$Site = 'kerzenburg.baldurs-gate.eu'
$Page = 'downloads.php?do=downloadlatest&id=3'

MsgBox ( 0, 'Header', __HTTPGetResponse($Site, '/'&$Page, "HEAD"))

Func __HTTPClose($Socket = -1)
    TCPCloseSocket($Socket)
    TCPShutdown()
    Return 1
EndFunc   ;==>__HTTPClose

Func __HTTPConnect($Host)
    TCPStartup()
    Local $Name_To_IP = TCPNameToIP($Host)
    Local $Socket = TCPConnect($Name_To_IP, $g_HTTP_TCP_Port)
    If $Socket = -1 Then
        TCPCloseSocket($Socket)
        Return SetError(1, 0, "")
    EndIf
    $g_LAST_SOCKET = $Socket
    Return $Socket
EndFunc   ;==>__HTTPConnect

Func __HTTPGet($Host, $Page, $Socket, $sRequest = "GET", $sReferrer = "")
    Local $Command = $sRequest & " " & $Page & " HTTP/1.1" & @CRLF
    $Command &= "Host: " & $Host & @CRLF
    $Command &= "User-Agent: " & $g_HTTPUserAgent & @CRLF
    $Command &= "Referer: " & $sReferrer & @CRLF
    $Command &= "Connection: close" & @CRLF & @CRLF
    Local $BytesSent = TCPSend($Socket, $Command)
    If $BytesSent = 0 Then Return SetError(1, @error, 0)
    Return $BytesSent
EndFunc   ;==>__HTTPGet

Func __HTTPGetResponse($Host, $Page, $sRequest = "GET", $sReferrer = "")
    Local $Socket = __HTTPConnect($Host)
    If @error Then Return SetError(1, 0, "")
    __HTTPGet($Host, $Page, $Socket, $sRequest, $sReferrer)
    If @error Then
        __HTTPClose($Socket)
        Return SetError(2, 0, "")
    EndIf
    Local $Recv = "", $CurrentRecv
    Local $iTimer = TimerInit()
    While 1
        $CurrentRecv = TCPRecv($Socket, 100)
        If @error <> 0 Then ExitLoop
        If $CurrentRecv <> "" Then $Recv &= $CurrentRecv
        If TimerDiff($iTimer) >= $g_Limit_TimeOut Then
            $g_Limit_TimeOutOver = True
            ExitLoop
        EndIf
    WEnd
    __HTTPClose($Socket)
    Return $Recv
EndFunc   ;==>__HTTPGetResponse

; ==========================================
; WinHTTP-solution (done by trancexx)
; ==========================================

#include 'winhttp.au3'

$Site = 'kerzenburg.baldurs-gate.eu'
$Page = 'downloads.php?do=downloadlatest&id=3'

$hOpen = _WinHttpOpen()
_WinHttpSetOption($hOpen, $WINHTTP_OPTION_ENABLE_FEATURE, $WINHTTP_OPTION_REDIRECT_POLICY_ALWAYS)
$hConnect = _WinHttpConnect($hOpen, $Site)
$hRequest = _WinHttpOpenRequest($hConnect, "GET", $Page)
_WinHttpSendRequest($hRequest)
_WinHttpReceiveResponse($hRequest)
If _WinHttpQueryDataAvailable($hRequest) Then MsgBox(0, 'Header', _WinHttpQueryHeaders($hRequest))
_WinHttpCloseHandle($hRequest)
_WinHttpCloseHandle($hConnect)
_WinHttpCloseHandle($hOpen)
Edited by dabus
Link to comment
Share on other sites

  • 2 months later...

Hello Guys

Some 1 plz explane me why i am not getting the result with POST method?

php

<?php
$nver = 101;
$rver =  $_POST['v'];
$cver = (int)$rver;

if ($rver == ""){
    echo "Nothing Found";
}else if($nver == $cver){
    echo "same ver";
}else if($nver > $cver){
    echo "new ver: " . $nver;
}
?>

Au Code

#include "WinHTTP.au3"

$hOpen = _WinHttpOpen()

$hConnect = _WinHttpConnect($hOpen, "www.example.com")

$hRequest = _WinHttpOpenRequest($hConnect, "POST", "ver/123.php")

$data = "v=101"

_WinHttpSendRequest($hRequest,$WINHTTP_NO_ADDITIONAL_HEADERS, $WINHTTP_NO_REQUEST_DATA, StringLen($data))

_WinHttpWriteData($hRequest, $data)

_WinHttpReceiveResponse($hRequest)

If _WinHttpQueryDataAvailable($hRequest) Then
;~  $header = _WinHttpQueryHeaders($hRequest)
;~  MsgBox(0, "Header", $header)

    MsgBox(0,"RCVD",_WinHttpReadData($hRequest))
EndIf

_WinHttpCloseHandle($hRequest)

_WinHttpCloseHandle($hConnect)

_WinHttpCloseHandle($hOpen)

After execution its always return "Nothing Found" with POST method; & with GET method its return "same ver"

Edited by Digisoul

73 108 111 118 101 65 117 116 111 105 116

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...